2

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?

Nate
  • 31,017
  • 13
  • 83
  • 207

1 Answers1

4

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.

Nate
  • 31,017
  • 13
  • 83
  • 207
  • Thanks for the `MKPointAnnotation` as I only need to create one point and didn't need a separate class. I've added `annotation.title = maps.barName;` and it works, any idea how to make that appear as soon as the map is shown? – user1454340 Jul 21 '12 at 13:36
  • @user1454340, the annotation isn't appearing as soon as the map is shown? you are using the code above in `viewDidLoad` or `viewWillAppear`? or are you asking if you can make a callout with the title appear automatically? to do that, [see this stack overflow answer](http://stackoverflow.com/a/2339556/119114) – Nate Jul 21 '12 at 23:44