Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: How do you convert CGPoint to CLLocationCoordinate2D

Tags:

ios

swift

I have the below code, and am trying to add annotation for a longpress on the map. The locationInView is of type 'CGPoint' whereas the 'annotation.coordinate' expects variable of type CLLocationCoordinate2D. whats the right method to convert CGPoint to CLLocationCoordinate2D ?

 func myGestureFunc(thegesture: UIGestureRecognizer) {

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


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


        theMap.addAnnotation(annotation)

}
like image 613
Naishta Avatar asked Dec 07 '25 06:12

Naishta


1 Answers

You should use convertPoint along the lines of:

let touchPoint = thegesture.locationInView(self.theMap)
let newCoordinate = self.theMap.convertPoint(touchPoint, toCoordinateFromView:self.theMap)
let annotation = MKPointAnnotation()
annotation.coordinate = newCoordinate
theMap.addAnnotation(annotation)
like image 78
Michael Avatar answered Dec 09 '25 19:12

Michael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!