简体   繁体   中英

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!

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.

做Bhargavi所说的,但要做到MapViewAnnotation.m和MapViewAnnotation.h

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM