Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone: Create MKAnnotation

Can I create an MKAnnotation, or is it read only? I have coordinates, but I am not finding it easy to manually create an MKAnnotation with using setCoordinate.

Ideas?

like image 817
Nic Hubbard Avatar asked May 21 '10 00:05

Nic Hubbard


People also ask

What is MKAnnotation?

An interface for associating your content with a specific map location.

What is MapKit in IOS?

Overview. Use MapKit to give your app a sense of place with maps and location information. You can use the MapKit framework to: Embed maps directly into your app's windows and views. Add annotations and overlays to a map to call out points of interest.

What is mapview annotation?

Derived from the MKAnnotationView class, an annotation view draws the visual representation of the annotation on the map surface. Register annotation views with the MKMapView so the map view can create and efficiently reuse them.


1 Answers

MKAnnotation is a protocol. So you need to write your own annotation object that implements this protocol. So your MyAnnotation header looks like:

@interface MyAnnotation : NSObject<MKAnnotation> {
    CLLocationCoordinate2D coordinate;
}

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

// add an init method so you can set the coordinate property on startup
- (id) initWithCoordinate:(CLLocationCoordinate2D)coord;

and your implementation looks like (MyAnnotation.m):

- (id) initWithCoordinate:(CLLocationCoordinate2D)coord
{
    coordinate = coord;
    return self;
}

So to add your annotation to the map:

MyAnnotation * annotation = [[[MyAnnotation alloc] initWithCoordinate:coordinate] autorelease];
[self.mapView addAnnotation:annotation];

If you wan't a title and subtitle on the annotation callout, you need to add the title and subtitle properties.

like image 60
RedBlueThing Avatar answered Sep 27 '22 20:09

RedBlueThing