An annotation is a marker associated with a location on a map. To make an annotation appear on a map, two objects are needed:
- The object attached to the MKMapView
The annotation itself is attached to the MKMapView. It consists of any instance whose class adopts the MKAnnotation protocol, which specifies a coordinate, a title, and a subtitle for the annotation. You might have reason to define your own class to handle this task, or you can use the simple built-in MKPointAnnotation class. The annotation’s
coordinateis its most important property; this says where on earth the annotation should be drawn. The title and subtitle are optional, to be displayed in a callout.
- The object that draws the annotation
An annotation is drawn by an MKAnnotationView, a UIView subclass. This can be extremely simple. In fact, even a nil MKAnnotationView might be perfectly satisfactory: it draws a red pin. If red is not your favorite color, a built-in MKAnnotationView subclass, MKPinAnnotationView, displays a pin in red, green, or purple; by convention you are supposed to use these colors for different purposes (destination points, starting points, and user-specified points, respectively). For more flexibility, you can provide your own UIImage as the MKAnnotationView’s
imageproperty. And for even more flexibility, you can take over the drawing of an MKAnnotationView by overriding
drawRect:in a subclass.
Not only does an annotation require two separate objects, but in fact those objects ...