Brandon
Brandon

Reputation: 2171

Add Custom Property to MKAnnotation

I am trying to add a custom value to my MKAnnotation. I would like to have it store the unique ID of the location. My code for setting up the annotation with a title and subtitle works like this:

location.latitude = [dictionary[@"placeLatitude"] doubleValue];
location.longitude = [dictionary[@"placeLongitude"] doubleValue];

newAnnotation = [[MapViewAnnotation alloc] initWithTitle:dictionary[@"placeName"]
                                               andCoordinate:location];

newAnnotation.subtitle = dictionary[@"placeCity"];

How would I add a custom property, for example "placeId"? this is what I have:

newAnnotation.placeId=dictionary[@"placeId"];

Any help would be great. Thank you!

Upvotes: 1

Views: 1121

Answers (2)

βhargavḯ
βhargavḯ

Reputation: 9836

If MapViewAnnotation is subclass of MKAnnotation/MKAnnotationView then simply create property for it, synthesize and use. And if not then create category over MKAnnotationView.

Here are the steps to create category class.

  1. Right click on project and select "New File"
  2. Cacoa Touch > Objective-C category > Next
  3. Give Category : category_name & Category on : category_on_class
  4. in .h file just create property of placeId
  5. in .m synthesize it using @dynamic
  6. import that .h file in you file.

Then you can have "classObject.placeId" property on "classObject" object of that class.

Upvotes: 2

Craig
Craig

Reputation: 8294

Do what Bhargavi said, but do it to MapViewAnnotation.m and MapViewAnnotation.h

Upvotes: 1

Related Questions