Instance Method

mapView(_:viewFor:)

Returns the view associated with the specified annotation object.

Declaration

optional func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?

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 dequeueReusableAnnotationView(withIdentifier:) 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

func mapView(MKMapView, didAdd: [MKAnnotationView])

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

func mapView(MKMapView, annotationView: MKAnnotationView, calloutAccessoryControlTapped: UIControl)

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

func mapView(MKMapView, clusterAnnotationForMemberAnnotations: [MKAnnotation]) -> MKClusterAnnotation

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

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software