user1454340
user1454340

Reputation: 65

CLLocationCoordinate2D to MKAnnotation

I have a MapView that I'm trying to add an Annotation at the current Coordinates.

My Code in viewWillAppear:

CLLocationCoordinate2D location;
location.latitude = [maps.barLat doubleValue];
location.longitude = [maps.barLong doubleValue];
[_mapView addAnnotation:location];

I'm getting an error on the addAnnotation that says

Sending CLLocationCoordinate2D to parameter of incompatible type MKAnnotation.

All the other examples I see have no problem with this code, what am I missing?

Upvotes: 2

Views: 1750

Answers (1)

Nate
Nate

Reputation: 31045

If you look at Apple's API docs, the addAnnotation: method takes an id<MKAnnotation>, not a CLLocationCoordinate2D. That's the reason for your error.

If you just want a simple annotation, without any fancy bells or whistles, just do this:

CLLocationCoordinate2D location;
location.latitude = [maps.barLat doubleValue];
location.longitude = [maps.barLong doubleValue];
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = location;
[_mapView addAnnotation: annotation];

Most people, however, wind up creating their own class that implements the MKAnnotation protocol, to provide custom annotations. But, if all you need is a pin, the above will work.

Upvotes: 4

Related Questions