iphone - 引脚很多,如何区分每个引脚?

标签 iphone ios mkmapview

请帮助我,我被这个 MapView 困住了:

我有一个 MapView 和那个 MapView 下的一个小标签。在MapView上,我有很多图钉(MKPinAnnotationView),我知道如何使用数组设置每个图钉的标题和副标题,但我不知道如何区分每个引脚。我的意思是当用户点击图钉时,标签将显示所点击图钉的标题。

这是我的一些代码:

这是我定义引脚的地方:

@implementation PlacePin

@synthesize coordinate,title,subtitle;
@synthesize nTag;

- (id)initWithLocation:(CLLocationCoordinate2D)coord{   

    self = [super init];    
    if (self) {         
        coordinate = coord;     
    }
    return self;    
}

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

这是我处理服务器结果的地方:

- (void) resultCheck {

NSString *strUrl    = [NSString stringWithFormat:@"ServerAddress.com"];

NSLog(@"MapView - resultCheck: url: %@ ", strUrl);
NSURL *url          = [ NSURL URLWithString:strUrl];

//get the result from server
NSString *result    = [NSString stringWithContentsOfURL:url];

NSLog(@"MapView - resultCheck: result: %@", result);


NSDictionary *dictionary = [result JSONValue];
NSLog(@"MapView - resultCheck: dictionary: %@", dictionary);





//process the JSON, get two parameters: xPos, yPos 
NSDictionary *value1 = [dictionary valueForKey:@"result"];
NSDictionary *value2 = [value1 valueForKey:@"post"];



NSArray *arrXPos    = [value2 valueForKey:@"xPos"];         //array of xPos
NSArray *arrYPos    = [value2 valueForKey:@"yPos"];         //array of yPos
self.arrName        = [value2 valueForKey:@"name"];         //array of name
self.arrPlaceInfor  = [value2 valueForKey:@"place_info"];



NSLog(@"MapView - resultCheck: value1: %@",value1);
NSLog(@"MapView - resultCheck: value2: %@",value2);
NSLog(@"MapView - resultCheck: value3: %@",arrXPos);
NSLog(@"MapView - resultCheck: value4: %@",arrYPos);



//get the xPos and yPos 
for (int i = 0 ; i < [value2 count]; i++) {

    //display the place depended on the xPos and yPos

    CLLocationCoordinate2D location; 

    location.latitude   = [[ NSString stringWithFormat:@"%@",[arrXPos objectAtIndex:i]] doubleValue];
    location.longitude  = [[ NSString stringWithFormat:@"%@",[arrYPos objectAtIndex:i]] doubleValue];


    PlacePin *mapPoint  = [[PlacePin alloc] initWithLocation:location];
    //set the title and subtitle of the pin depending on the result from server
    mapPoint.title      = [ NSString stringWithFormat:@"%@",[self.arrName objectAtIndex:i]];
    mapPoint.subtitle   = [ NSString stringWithFormat:@"%@",[self.arrPlaceInfor objectAtIndex:i]];

    [mapView addAnnotation:mapPoint];
    [mapPoint release];


    mapPoint = nil;



}

这是我处理 pin 并设置标签的地方 - 我被卡住了:(

    - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view NS_AVAILABLE(NA, 4_0){

    self.labelShortIntro.text = @"1111111111111";


}

//customize an annotation
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{

    MKPinAnnotationView *annView    = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
    annView.pinColor                = MKPinAnnotationColorPurple;
    annView.animatesDrop            = TRUE;
    annView.canShowCallout          = YES;
    annView.calloutOffset           = CGPointMake(-5, 5);
    return annView;
}

最佳答案

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{

    PlacePin *selectedPin       =     view.annotation;

    NSLog(@"Title = %@ Subtitle = %@",selectedPin.title,selectedPin.subtitle);

    self.labelShortIntro.text   =     [NSString stringWithFormat:@"Title = %@ Subtitle = %@",selectedPin.title,selectedPin.subtitle];

}

关于iphone - 引脚很多,如何区分每个引脚?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11464475/

相关文章:

iphone - iPhone 上的 facebook 连接 - 调用电话时出现错误 102 "Session key invalid or no longer valid",但流对话框仍然有效

iphone - 如何在 iOS 应用程序中显示和编辑现有的 PDF 文件

ios - 如何在 Swift 中显示 SDWebimage 背景颜色

ios - 使用 MapLinks 时如何防止 Safari 启动?

iphone - MapKit 没有 field 名称?

c# - MonoTouch MKLocalSearch示例

iphone - 提高应用程序更新的最低 iOS 部署目标版本

ios - Xcode 9.1 不支持 iOS 11.2.1 Xcode 需要 9.2

objective-c - 在 Xcode 中将 RSS 日期格式转换为德语日期

ios - 增加高度后,UIToolBar按钮未正确定位