How do get the phone object from my array to the button on my map annotation call out?
I have the pins loading from a JSON array just fine and I am parsing out the address from the phone number. How do I get the correct phone number to the button when that call out is pushed?
for(id key in json) {
id value = [json objectForKey:key];
NSString *titlePin = [value valueForKey:@"address"];
NSString *addressPhone = [value valueForKey:@"title"];
NSString *latitude = [value valueForKey:@"latitude"];
NSString *longitude = [value valueForKey:@"longitude"];
NSArray* foo = [addressPhone componentsSeparatedByString: @":"];
NSString* justAddress = [foo objectAtIndex: 0];
NSString* phone = [foo objectAtIndex: 1];
double myLatitude = [latitude doubleValue];
double myLongitude = [longitude doubleValue];
MKCoordinateRegion location1;
location1.center.latitude =myLatitude;
location1.center.longitude= myLongitude;
location1.span.longitudeDelta=0.1;
location1.span.latitudeDelta =0.1;
MapAnnotation *ann1 =[[[MapAnnotation alloc] init] autorelease];
ann1.title=[NSString stringWithFormat:@"%@",titlePin];
ann1.subtitle=[NSString stringWithFormat:@"%@",justAddress];
//EDIT added this line for part of sollution
ann1.phone=[NSString stringWithFormat:@"%@",phone];
ann1.coordinate= location1.center;
[mapView addAnnotation:ann1];
}
}
}
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"current"];
MyPin.pinColor = MKPinAnnotationColorPurple;
UIButton *advertButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[advertButton addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
MyPin.rightCalloutAccessoryView = advertButton;
MyPin.draggable = NO;
MyPin.highlighted = YES;
MyPin.animatesDrop=TRUE;
MyPin.canShowCallout = YES;
return MyPin;
}
-(void)button:(id)sender {
// Call the phone number here!!!!!!!
//NSLog(@"Button action: %@",phone);
}
MapAnnotaion.h
#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>
@interface MapAnnotation : NSObject <MKAnnotation>
{
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
NSString *phone;
}
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, copy) NSString *phone;
@end
MapAnnotaion.m
#import "MapAnnotation.h"
@implementation MapAnnotation
@synthesize coordinate, title, subtitle, phone;
-(void)dealloc
{
[title release];
[subtitle release];
[phone release];
[super release];
[super dealloc];
}
@end
Part of codes from apple sample code :MapCallouts
SFAnnotation.h
SFAnnotation.m
where the title is the string shows in the call out’s title
Edit 1
}