How to add annotation in mkmapview

2k Views Asked by At

I have created a person object

ABRecordRef person = ABPersonCreate(); 

I set the properties of person from my web services. person has all the properties set, like first name, last name, email id, phone number, address, url etc.

I want to load a map view, with multiple pins, where each particular pin holds the information about a particular person. On the click of each pin, I need to load the ABPersonViewController class of each person. I am not sure about how to add annotation with the information I have and how a particular pin could hold information about a particular person. In this case the person may or may not be in my address book, and I don't need to save the person in my address book . I am really confused about how to do this. Need help.

Edit:

I have 3 classes:

!. Web service class: where I have the person object. 2. MapView class: where I load the map view. 3. Annotation class: which has the details about my annotation.

How will I use person records and present it through ABPersonViewController. How should my annotation class be?

1

There are 1 best solutions below

5
On BEST ANSWER

What I would do to start is create a Person class (subclass of NSObject) that also implements the MKAnnotation protocol. You could create two separate classes ("Person" and "PersonAnnotation") if you wanted to but it's not necessary.

In the Person class, you could either declare your own person-related properties such as First Name, Last Name, Email Address, etc. or you could just have an ABRecordRef ivar and let it store the individual fields for you.

I would only create an ABPerson record when I want to actually show the ABPersonViewController to keep the AB-specific code isolated and to manage the ABPerson record's creation and release more easily. Regardless, just creating an ABRecordRef does not add it to the address book. As the ABPerson Reference documentation says:

Person records don’t necessarily have to be stored in the Address Book database. You can use person records as a way to group contact information in memory and present it to the user through, for example, a person view controller (ABPersonViewController).

So the Person class could look like this:

@interface Person : NSObject<MKAnnotation>
@property (nonatomic, copy) NSString *firstName;
@property (nonatomic, copy) NSString *lastName;
@property (nonatomic, copy) NSString *emailAddress;
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@end


@implementation Person

@synthesize firstName;
@synthesize lastName;
@synthesize emailAddress;
@synthesize coordinate;

-(NSString *)title
{
    return [NSString stringWithFormat:@"%@ %@", firstName, lastName];
}

-(NSString *)subtitle
{
    return emailAddress;
}

@end

The web service class would create an instance of Person and set the properties. Then the map view class would add that instance directly to the map (since Person already implements MKAnnotation):

[mapView addAnnotation:person];

When the pin is tapped, the map view will call the didSelectAnnotationView delegate method. Or, you could add a disclosure button to the annotation's callout in viewForAnnotation and respond to that in the calloutAccessoryControlTapped delegate method.

Whichever action method you decide to use, in that method you could then create an ABRecordRef, set its values from the annotation object, and then show the ABPersonViewController. In both delegate methods, the Person annotation object could be retrieved using:

Person *personTapped = (Person *)view.annotation;