17

Is there a simple example project for MKAnnotation? I don't know what to pass at "addAnnotation", as it wants some "id<Annotation>". The examples at the developer site are all with arrays or parsers or so, I just need a very easy example to first understand, how the whole thing works.

Thanks in advance..

EDIT:

I set up a class Pin. In the .h it writes

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface Pin : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subTitle;
}

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, readonly) NSString *title;
@property (nonatomic, readonly) NSString *subTitle;

- (id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:(NSString *)placeName description:(NSString *)description;
@end

according to what onnoweb answered.

In the Pin.m, I implemented like this, according to different examples:

#import "Pin.h"

@implementation Pin

@synthesize title, subTitle;

- (CLLocationCoordinate2D) coordinate {
CLLocationCoordinate2D coords;
coords.latitude = coordinate.latitude; //i want it that way
coords.longitude =  7.1352260; //this way it works

return coords;

- (NSString *) title {
return @"Thats the title";
}

- (NSString *) subtitle {
return @"Thats the sub";
}

- (id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:(NSString *)placeName description:(NSString *)description {

return self;
}

- (void) dealloc {
[super dealloc];
}

@end

So if I set the coordinates by hand, as pointed in the comment, it works. But I'd like to be able to set the value dynamically like in the first comment. I tried various ways, but none worked out. As you don't specify a - (CLLocationCoordinate2D) coordinate I tried to just synthesize coordinate, but like that it won't work either.

Could you show me your MapPin.m?

EDIT 2:

I set mapCenter like

- (void)viewWillAppear:(BOOL)animated {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];

CLLocation *curPos = locationManager.location;
location = curPos.coordinate;

CLLocationCoordinate2D mapCenter;
mapCenter.latitude =  location.latitude;
mapCenter.longitude = location.longitude;

MKCoordinateSpan mapSpan;
mapSpan.latitudeDelta = 0.005;
mapSpan.longitudeDelta = 0.005;

MKCoordinateRegion mapRegion;
mapRegion.center = mapCenter;
mapRegion.span = mapSpan;

mapKitView.region = mapRegion;
mapKitView.mapType = MKMapTypeStandard;
mapKitView.showsUserLocation=TRUE;
}

Anything wrong with that?

Sujay
  • 2,510
  • 2
  • 27
  • 47
Nareille
  • 811
  • 2
  • 11
  • 30

1 Answers1

36

You need to have a class implementing the MKAnnotation protocol. Here is a snippet from one of my projects:

@interface MapPin : NSObject<MKAnnotation> {
    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;
}

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, readonly) NSString *title;
@property (nonatomic, readonly) NSString *subtitle;

- (id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:(NSString *)placeName description:(NSString *)description;

@end

Then, where you create the map you'll call:

pin = [[MapPin alloc] initWithCoordinates:[track startCoordinates] placeName:@"Start" description:@""];
[map addAnnotation:pin];

EDIT:

Here is the implementation:

@implementation MapPin

@synthesize coordinate;
@synthesize title;
@synthesize subtitle;

- (id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:placeName description:description {
    self = [super init];
    if (self != nil) {
        coordinate = location;
        title = placeName;
        [title retain];
        subtitle = description;
        [subtitle retain];
    }
    return self;
}

- (void)dealloc {
    [title release];
    [subtitle release];
    [super dealloc];
}


@end

Hope this helps, Onno.

sarnold
  • 102,305
  • 22
  • 181
  • 238
onnoweb
  • 3,038
  • 22
  • 29
  • Thanks, that already helped quite a lot. Like that I new what to google for, and got it up running, but I don't quite get it. Please see my updated question for the code i have so far. I don't know what the .m should look like.. – Nareille Jun 28 '11 at 14:49
  • Thanks, that helped again :) One last question for some weird behaviour: I don't define something like "coordinate" in my .h, but Xcode tells me I need to synthesize it. If I do so, my mapCenter is shifted some hundred meters to the west. If I outcomment the synthesize, it centers proper again. How to get rid of the warning? – Nareille Jun 29 '11 at 09:04
  • Very important: don't write subTitle, but subtitle (see the non-capital t in title), otherwise it won't assign your subtitle! Than it works fine! Thanks a lot! – Nareille Jul 13 '11 at 08:13
  • @Nareille, is [tag:ios] / [tag:objective-c] _really_ that picky about requiring lowercase-only? That seems so _artificial_ and surprising, it'd sure be nice to have more details... – sarnold Jul 13 '11 at 08:21
  • @sarnold Well, at least it only worked with lowercase for me. If you look at the doc, subtitle is already declared as a property for the MKAnnotation class. I'm quite new to iOS, but if I got it right, my instance just sets a setter and getter to access exactly this variable. If the case is different (so subTitle), it doesn't assign your value to that property, and your value gets lost in nowhere.. Somebody else might be more into that, but as I said, just changing from subTitle to subtitle resolved the last issue, than the code is perfect as is :)(it _worked_ before, but didn't set the sub) – Nareille Jul 14 '11 at 13:12
  • @Nareille, excellent, thanks for the description, I've re-made the edit you suggested (and appears has since been dropped). I also stuffed the MKAnnotation class documentation URL into the changelog, in case it looks too spurious a change. :) – sarnold Jul 14 '11 at 23:17