1

I am using MapKit and I am having the exact problem.

This is my code:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    static NSString *identifier = @"MyLocation";
    if ([annotation isKindOfClass:[MyLocation class]]) {

        MKAnnotationView *annotationView = (MKAnnotationView *) [mymap_ios dequeueReusableAnnotationViewWithIdentifier:identifier];
        if (annotationView == nil) {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
            annotationView.enabled = YES;
            annotationView.canShowCallout = YES;

        } else {
            annotationView.annotation = annotation;
        }
        annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        return annotationView;
    }

    return nil;
}

In this code, I can see the pin but not the blue button next to it so as. It seems that I have forgotten to do this:

mymap_ios.delegate=self;

But when I add this, the marker is not shown at all.

Can you help me on that?

4

1 回答 1

2

When you don't set the map view's delegate, it doesn't call your viewForAnnotation and creates a default red pin without any accessory buttons.


When you set the delegate, it is calling your viewForAnnotation method but you are creating a plain MKAnnotationView which by default does not have any pre-set image or view (it's blank).


Either set the annotation view's image, add some content to the view, or simply create an MKPinAnnotationView instead of an MKAnnotationView:

MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [mymap_ios ...
    if (annotationView == nil) {
        annotationView = [[MKPinAnnotationView alloc] init...


Also make sure that the annotation objects you add are of type MyLocation otherwise they will appear as plain red pins without an accessory button.

于 2013-07-02T16:54:59.033 回答