【问题标题】:Swift: How do you convert CGPoint to CLLocationCoordinate2DSwift:如何将 CGPoint 转换为 CLLocationCoordinate2D
【发布时间】:2016-02-24 00:24:52
【问题描述】:

我有以下代码,并试图在地图上为长按添加注释。 locationInView 是“CGPoint”类型,而“annotation.coordinate”需要 CLLocationCoordinate2D 类型的变量。 将 CGPoint 转换为 CLLocationCoordinate2D 的正确方法是什么?

 func myGestureFunc(thegesture: UIGestureRecognizer) {

        let pointOfInterest = thegesture.locationInView(self.theMap) //CGPoint


       let annotation = MKPointAnnotation()
        annotation.coordinate = //expects CLLocationCoordinate2D


        theMap.addAnnotation(annotation)

}

【问题讨论】:

标签: ios swift


【解决方案1】:

您应该按照以下方式使用convertPoint

let touchPoint = thegesture.locationInView(self.theMap)
let newCoordinate = self.theMap.convertPoint(touchPoint, toCoordinateFromView:self.theMap)
let annotation = MKPointAnnotation()
annotation.coordinate = newCoordinate
theMap.addAnnotation(annotation)

【讨论】:

  • 没有更多选项可以在相关的 google map sdk 中返回坐标,它总是返回 cgpoint / cgrect 在这种情况下我应该怎么做。
【解决方案2】:

由于convertPoint()方法不再可用,可以使用以下代码:

let coordinate = googleMapView.projection.coordinate(for: point)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-31
    • 2015-07-04
    • 2019-05-23
    • 2013-04-04
    • 2017-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多