Instance Method

mapView:viewForAnnotation:

Returns the view associated with the specified annotation object.

Declaration

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

Parameters

mapView

The map view that requested the annotation view.

annotation

The object representing the annotation that is about to be displayed. In addition to your custom annotations, this object could be an MKUserLocation object representing the user’s current location.

Return Value

The annotation view to display for the specified annotation or nil if you want to display a standard annotation view.

Discussion

Rather than create a new view each time this method is called, call the dequeueReusableAnnotationViewWithIdentifier: method of the MKMapView class to see if an existing annotation view of the desired type already exists. If one exists, update the returned view to reflect the attributes of the specified annotation and return it. If a view of the appropriate type does not exist, you should create one, configure it with the needed annotation data, and return it.

If the object in the annotation parameter is an instance of the MKUserLocation class, you can provide a custom view to denote the user’s location. To display the user’s location using the default system view, return nil.

If you do not implement this method, or if you return nil from your implementation for annotations other than the user location annotation, the map view uses a standard pin annotation view.

See Also

Managing Annotation Views

- mapView:didAddAnnotationViews:

Tells the delegate that one or more annotation views were added to the map.

- mapView:annotationView:calloutAccessoryControlTapped:

Tells the delegate that the user tapped one of the annotation view’s accessory buttons.

- mapView:clusterAnnotationForMemberAnnotations:

Asks the delegate to provide a cluster annotation object for the specified annotations.