User Location and your own annotations in a MKMapView

I’ve spend a while working this one out.

For a long time I’ve had a MKMapView to display pins dropped in specific places on a map.

I’ve done this through the delegate method

-(MKAnnotationView *)mapView:(MKMapView *)mapView  viewForAnnotation:(id) annotation {
  MKPinAnnotationView *annotation = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
 // Some customisation goes here
 [annotation autorelease];
 return annotation;
}

That’s all very will but then I decided it’d be nice to show the user’s current location on the map as well, you know the lovely blue pin with accuracy circle around it.

Well enabling this looks very simple.

mapView.showsUserLocation = YES;

Now I have custom objects in my annotation subclasses so I can reference them easily later. With this, however, if you compile and try to run, when the view loads you’ll get a crash with this error to the debugger.

-[MKUserLocation getTagId]: unrecognized selectory sent to instance

It turns out the way to get this to work is to include at the top of the delegate method the following line

if ([annotation isMemberOfClass:[MKUserLocation class]]) return nil;

Compile and run and all will work fine

Leave a Reply