I'm trying to create custom annotationviews for the annotations on my map. I'm doing that by adapting the protocol MKMapViewDelegate and overwriting the function mapView:viewForAnnotation:. It all works, the only problem is that I also have showsUserLocation set to TRUE, which means that one "Annotation" I get in my mapView:viewForAnnotation: method is of the class MKUserLocation.
I don't want the userlocation annotation to have my custom annotationview, I want that one to show the default userlocation annotationview! How do I return the default userlocation annotationview for the userlocation or exclude it from the annotations (that come in mapView:viewForAnnotation:)?
I have tried to catch the UserLocation in the mapView:viewForAnnotation: method, but I don't know what to return! (In this example I'm returning a standard MKAnnotationView, but that doesn't look like the default UserLocation Annotation (obviously).)
if (![[annotation class] isEqual:[MKUserLocation class]]) {
MKAnnotationView *view = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"customAnnotation"];
// edit the custom view
return view;
}
MKAnnotationView *view = [[MKAnnotationView alloc] init];
return view;
to show the default annotation for user location just return nil for that case, I did it this way:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
// use your custom annotation
if ([annotation isKindOfClass:[MyAnnotationClass class]]) {
...
return annotationView;
}
// use default annotation
return nil;
}
Inside your viewForAnnotation method write this piece of code. Here the var 'map' is the outlet for your MKMapview;
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation
{
//Annoation View for current Location
if(map.userLocation != annotation)
{
UIImage *image = [UIImage imageNamed:@"image.png"];
annotation.image = image;
return annotation;
}
//Annotation View for current location
return nil;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With