15

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?

Shujito
  • 3,083
  • 2
  • 17
  • 18
Nic Hubbard
  • 41,587
  • 63
  • 251
  • 412

3 Answers3

41

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.

RedBlueThing
  • 42,006
  • 17
  • 96
  • 122
  • This is just what I needed. Where would I put the - (id) initWithCoordinate:(CLLocationCoordinate2D)coord; method? In my .h file or .m? – Nic Hubbard May 21 '10 at 00:44
  • No problem. I have updated the answer with some more detail :) – RedBlueThing May 21 '10 at 01:01
  • For some reason, I get no '-initWithCoord:' method found when I try to build and run. – Nic Hubbard May 21 '10 at 01:34
  • Sorry. My sample code was initWithCoord, it should be initWithCoordinate. – RedBlueThing May 21 '10 at 02:06
  • How to add image for annotation? – Nilesh Tupe Oct 05 '11 at 06:10
  • @NileshTupe Check out this question: http://stackoverflow.com/questions/1185611/mkpinannotationview-are-there-more-than-three-colors-available/1963460#1963460 – RedBlueThing Oct 05 '11 at 08:17
  • The whole point of protocols (and interfaces in Java) is to let you treat things as other things. So making a class called MyAnnotation, even in an example, is really not good. Whatever you are putting on the map should be a domain object, for instance, a Restaurant. Then you have that domain class implement this protocol, then you are mapping this positionable behavior onto it. – Rob Dec 16 '12 at 06:34
33

In iPhone OS 4 there is a new class MKPointAnnotation which is a concrete implementation of the MKAnnotation protocol.

joshrl
  • 3,527
  • 1
  • 22
  • 12
  • 1
    This is super handy if you only need basic annotations. Thanks! – radven Feb 03 '12 at 17:34
  • 4
    MKPointAnnotation* pointAnnotation = [[MKPointAnnotation alloc] init]; pointAnnotation.coordinate = location.coordinate; [mkMapView addAnnotation:pointAnnotation]; – Dickey Singh Jun 21 '13 at 17:07
9

Check apple MapCallouts project. Everything you need is in that file: http://developer.apple.com/iphone/library/samplecode/MapCallouts/Introduction/Intro.html

alecnash
  • 1,750
  • 1
  • 18
  • 42