Swift:如何将CGPoint转换为CLLocationCoordinate2D

时间:2016-02-24 00:24:52

标签: ios swift

我有以下代码,我正在尝试在地图上为longpress添加注释。 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)

}

2 个答案:

答案 0 :(得分:2)

你应该使用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)

答案 1 :(得分:0)

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

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