Change MKMapType in MKMapView and keep custom pinImage for annotations

元气小坏坏 提交于 2019-12-22 10:29:49

问题


I have set a custom pinImage for my annotations and when I change the type toMKMapTypeHybrid it reverts the pinImage setting to the standard pins.

I'm setting the mapType in my viewWillAppear method of the map view controller. I'm setting my pinImage for the annotations like so (shortened for clarity):

- (MKAnnotationView *) mapView:(MKMapView *) mapView viewForAnnotation:(id ) annotation {

    MKPinAnnotationView *customAnnotationView=[[[MKPinAnnotationView alloc] 
                                            initWithAnnotation:annotation 
                                            reuseIdentifier:@"markerAnnotationView"] autorelease];
    UIImage *pinImage = [UIImage imageNamed:@"/pin-image"];
    [customAnnotationView setImage:pinImage];
    return customAnnotationView;
}

Is there any way to use setImage and set the mapType via code?


回答1:


The solution to this problem was to use an MKAnnotationView instead of the MKPinAnnotationView. I had previously been using the MKPinAnnotationView for good reason but later added a custom image to the pin without changing the instantiating class to MKAnnotationView.

Now, I think it is probably buggy framework behavior. If you're going to let an MKPinAnnotationView accept a setImage call then make sure that it handles it correctly when you change mapTypes. If you only want them to do pins then don't do anything with the call. Ah well.



来源:https://stackoverflow.com/questions/5136545/change-mkmaptype-in-mkmapview-and-keep-custom-pinimage-for-annotations

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