iosswiftswift2cllocationcllocationcoordinate2d

Cannot assign CGPoint to CLLocationCoordinate2D


var touchPoint = gestuRerecognizer.locationInView(self.map)
var newCoordinate = map.convertPoint(touchPoint, fromCoordinateSpace: self.map)
var annotation = MKPointAnnotation()
annotation.coordinate = newCoordinate

This code gives me (Cannot assign a value of type 'CGPoint' to a value of type 'CLLocationCoordinate2D') error.


Solution

  • It should be:

    let newCoordinate = map.convertPoint(touchPoint, toCoordinateFromView:self.map)
    annotation.coordinate = newCoordinate
    

    Instead of:

    var newCoordinate = map.convertPoint(touchPoint, fromCoordinateSpace:self.map)
    

    because it is returning you a CGPoint, not a CLLocationCoordinate2D, which causes the error when you assign newCoordinate to annotation.coordinate.