Hudson Buddy Hudson Buddy - 1 year ago 51
iOS Question

How to stop the viewForAnnotation method from overriding the default user location blue beacon in iOS

Right now I am populating a map view with annotations, and also displaying the user's current location. With the viewForAnnotation method, it overrides all annotations with the default red pin, but I want to return the views for the other annotations, but keep the user location the default blue beacon. Is there a way to do this simply or do I have to create a new annotation view and return the right one depending on the annotation?

Right now I have something like:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
if (annotation.coordinate == locationManager.location.coordinate) {

return nil;

}else {

MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Beacon"];

// Button
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
button.frame = CGRectMake(0, 0, 23, 23);
annotationView.rightCalloutAccessoryView = button;

annotationView.canShowCallout = YES;

return annotationView;

But I can't equate the two because I can't get the coordinate property out of the annotation argument.

Anybody know any solutions to this?

Answer Source

Check out the documentation here:

As it states:

If the object in the annotation parameter is an instance of the MKUserLocation class, you can provide a custom view to denote the user’s location. To display the user’s location using the default system view, return nil.

So you can add a conditional to check for this:

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