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?

1 Answer 1

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.

6
  • 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, 2011 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, 2011 at 9: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, 2011 at 8:13
  • @Nareille, is ios / 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, 2011 at 8: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, 2011 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.