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

时光毁灭记忆、已成空白 提交于 2019-12-18 12:15:23

问题


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?


回答1:


Check out the documentation here:

http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapViewDelegate_Protocol/MKMapViewDelegate/MKMapViewDelegate.html

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;
}


来源:https://stackoverflow.com/questions/11767240/how-to-stop-the-viewforannotation-method-from-overriding-the-default-user-locati

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!