creating CLLocationCoordinate2D on the fly while conforming to MKAnnotation protocol

656 views Asked by At

I have MyClass that conforms to MKAnnotation protocol. according to documentation, class is required to implement coordinate property which should return CLLocationCoordinate2D instance. My first implementation was like this:

-(CLLocationCoordinate2D)coordinate{
   return MKCoordinateForMapPoint(MKMapPointMake(self.details.latitude, self.details.longitude));
}

but this did not worked: whenever I call [self.mapView addAnnotation:instanceOfMyClass]; it just simply wasn't added to the annotations array of the mapView!

So, the solution for me was to define an instance variable _coordinate in MyClass and implement protocol conformance like this:

-(CLLocationCoordinate2D)coordinate
{
    _coordinate.latitude = self.details.latitude;
    _coordinate.longitude = self.details.longitude;
    return _coordinate;
}

which now worked. So, the question here is WHY it is needed to have an instance variable and creating CLLocationCoordinate2D on-the-fly doesn't work?

1

There are 1 answers

0
yinkou On BEST ANSWER

Are your self.details.latitude/longitude values already true/valid geographic coordinates?
In that case you should use the CLLocationCoordinate2DMake(lat,lon) function instead of MKCoordinateForMapPoint(MKMapPointMake()) which is wrong.