开发者

MKAnnotationViews with image in satellite map mode

I successfully set a custom image on MKAnnotationView to replace pins (I used the function setImage). But it only works when the MKMapView type is "Standard". Satellite and Hybrid modes always display pins.

Does anyone h开发者_StackOverflowave an idea to solve this strange issue?


I had the exact same problem when I tried changing the image of an MKPinAnnotationView. It seems that a call to setImage isn't persistent since the mapView calls setImage with the default image now and then. The solution is to subclass MKPinAnnotationView and override setImage to always set your custom image:

- (void)setImage:(UIImage *)image {
    [super setImage:[UIImage imageNamed:@"MyCustomPinImage.png"]];
}


Strange, both subjects (custom MKAnnotationView and map mode) are not linked together.

The way you replace the pins seems strange (setImage ?), the "classic" way is to implement the

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

and return your custom annotation view in your delegate to fulfill the MKMapViewDelegate protocol.


To expand the answer from yonel, you can't just use the setImage of already created MKAnnotationView.image property to set a new image for it in the main part of your MapViewController.

The MapViewController must have implemented the MKMapViewDelgate method:

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation

In it, you will be notified from the map when an annotation is added.

So, to cutomize your annotationView, you should extend the MKAnnotation and make your own annotation class. You just add this custom annotation instance to let your mapView automatically show also a customized (extended, i.e. your own) annotationView for this annotation type (i.e. class).

That is, the delegate method notifies you that the custom annotation is added, so in there you check the annotation class and if it is your annotation class, you create a new instance of your custom annotationView.

That is, to use the init method of this customAnnotation class as:

- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)identifier

You must implement this method yourself.

In this method, you can use setImage to set any image you like. That will be used for this annotationView.

I guess that in your usage, the set image was wiped out when the MapType was changed, presumablly this was because it was creating a new instance of an annotationView for the map (which is a new map and adding annotations again and re-creating annnotationViews from the delegate method).

If you set the image in the init method, it won't be lost (as it is always set for the instance).

Does this help?


The answer is to use MKAnnotationView instead of MKPinAnnotationView.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜