2016-02-24 94 views
1

我有下面的代碼,並且試圖在地圖上爲longpress添加註釋。 locationInView的類型是'CGPoint',而'annotation.coordinate'需要CLLocationCoordinate2D類型的變量。 什麼是正確的方法將CGPoint轉換爲CLLocationCoordinate2D?Swift:你如何將CGPoint轉換爲CLLocationCoordinate2D

func myGestureFunc(thegesture: UIGestureRecognizer) { 

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


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


     theMap.addAnnotation(annotation) 

} 
+0

http://stackoverflow.com/a/34756033/2303865 –

回答

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) 
相關問題