iOS MapKit Changing mapview maptype causes annotation image to change to pin?

百般思念 提交于 2019-12-10 02:25:01

问题


Any suggestions on what is wrong with the following would be appreciated.

I am adding a custom image to an annotation using the following code.

- (MKAnnotationView *)mapView:(MKMapView *)mapView 
            viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isMemberOfClass:[MKUserLocation class]]) 
    { 
        return nil; 
    } 


    if ([annotation isMemberOfClass:[SpectatorPin class]]) 
    { 
        SpectatorPin *sp = (SpectatorPin *)annotation;
        MKAnnotationView *view = [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"specPin"];
        if (view == nil) {
            view = [[[MKPinAnnotationView alloc] initWithAnnotation:sp reuseIdentifier:@"specPin"] autorelease];
        }
        view.image = [UIImage imageNamed:@"mylocation20x20.png"]; 
        view.canShowCallout = YES;
        view.annotation=annotation;
        return view;
    }

    //Should not get here
    return nil;

}

The image is displayed properly initially.

I have a segment control which changes the map type (standard, satellite, hybrid). Standard is the default. As soon as I select satellite the image immediately changes to a pin. The mapView:viewforAnnotation method is not called again.

Regards,

Jim


回答1:


For custom image, you might use MKAnnotationView instead of MKPinAnnotationView.

MKAnnotationView *view = [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"specPin"];
        if (view == nil) {
            view = [[[MKAnnotationView alloc] initWithAnnotation:sp reuseIdentifier:@"specPin"] autorelease];
        }



回答2:


Found this: iPhone Core Location: Custom pin image disappears when map type changes

which linked to this: Why does a custom MKMapView annotation image disappear on touch?




回答3:


Try this:

MKAnnotationView *view = [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"specPin"];
        if (view == nil) {
            view = [[[MKAnnotationView alloc] initWithAnnotation:sp reuseIdentifier:@"specPin"] autorelease];
        }


来源:https://stackoverflow.com/questions/5151378/ios-mapkit-changing-mapview-maptype-causes-annotation-image-to-change-to-pin

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