views:

574

answers:

2

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

Has someone got an idea to solve this strange issue ?

Thanks a lot.

Tom.

A: 

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.

yonel
A: 

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?

Yoichi