0

如何从我的数组中获取电话对象到我的地图注释调用上的按钮?

我从 JSON 数组中加载引脚就好了,我正在从电话号码中解析出地址。按下呼叫时,如何在按钮上获取正确的电话号码?

      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);

}

地图注释.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

地图注释.m

#import "MapAnnotation.h"

@implementation MapAnnotation

@synthesize coordinate, title, subtitle, phone;

-(void)dealloc
{
    [title release];
    [subtitle release];
    [phone release];
    [super release];
    [super dealloc];
}

@end
4

1 回答 1

0

苹果示例代码的部分代码:MapCallouts

SFAnnotation.h

#import <MapKit/MapKit.h>

@interface SFAnnotation : NSObject <MKAnnotation>
{
    UIImage *image;
    NSNumber *latitude;
    NSNumber *longitude;
}

@property (nonatomic, retain) UIImage *image;
@property (nonatomic, retain) NSNumber *latitude;
@property (nonatomic, retain) NSNumber *longitude;

@end

SFAnnotation.m

#import "SFAnnotation.h"

@implementation SFAnnotation 

@synthesize image;
@synthesize latitude;
@synthesize longitude;


- (CLLocationCoordinate2D)coordinate;
{
    CLLocationCoordinate2D theCoordinate;
    theCoordinate.latitude = 37.786996;
    theCoordinate.longitude = -122.419281;
    return theCoordinate; 
}

- (void)dealloc
{
    [image release];
    [super dealloc];
}

- (NSString *)title
{
    return @"San Francisco";
}

// optional
- (NSString *)subtitle
{
    return @"Founded: June 29, 1776";
}

@end

其中标题是调用标题中显示的字符串

编辑 1

-(void)button:(id)sender {

UIButton *button = (UIButton *)sender;

MKAnnotationView *annotationView = button.superview.superview;

MapAnnotaion *mapAnnotaion = annotationView.annotation;

NSLog(@"%@", mapAnnotation.phone);


// Call the phone number here!!!!!!!
//NSLog(@"Button action: %@",phone);

}

于 2012-06-28T17:55:59.733 回答