获取mapView Annotation的indexPath

时间:2012-09-24 02:50:25

标签: objective-c ios mkmapview mkannotation

我正在使用来自plist的数据在地图上加载一堆引脚。以下是我获取数据的方法:

for (int i=0; i<self.dataArray.count; i++){

    NSDictionary *dataDictionary = [self.dataArray objectAtIndex:i];
    NSArray *array = [dataDictionary objectForKey:@"Locations"];

    for (int i=0; i<array.count; i++){

        NSMutableDictionary *dictionary = [array objectAtIndex:i];

        double latitude = [[dictionary objectForKey:@"Latitude"] doubleValue];
        double longitude = [[dictionary objectForKey:@"Longitude"] doubleValue];

        CLLocationCoordinate2D coord = {.latitude =
            latitude, .longitude =  longitude};
        MKCoordinateRegion region = {coord};

        MapAnnotation *annotation = [[MapAnnotation alloc] init];
        annotation.title = [dictionary objectForKey:@"Name"];

        NSString *cityState = [dictionary objectForKey:@"City"];
        cityState = [cityState stringByAppendingString:@", "];
        NSString *state = [dictionary objectForKey:@"State"];
        cityState = [cityState stringByAppendingString:state];
        annotation.subtitle = cityState;
        annotation.coordinate = region.center;
        [mapView addAnnotation:annotation];
    }
}

现在,对于每个注释,我都添加了detailDisclosureButton。我想从plist中显示该特定位置的详细信息。问题是我需要indexPath.section和indexPath.row。

如何获取pin的indexPath?有没有办法找到填充注释标题的字典的indexPath?

1 个答案:

答案 0 :(得分:3)

我建议在注释对象中存储对dictionary本身的引用,而不是跟踪“section”和“row”。

MapAnnotation类中,添加一个属性以保存对源词典的引用:

@property (nonatomic, retain) NSMutableDictionary *sourceDictionary;

创建注释(在现有循环中)时,请将此属性与title等一起设置:

annotation.sourceDictionary = dictionary;
annotation.title = [dictionary objectForKey:@"Name"];

然后在详细按钮处理程序方法中(假设您使用calloutAccessoryControlTapped委托方法),将注释对象强制转换为类,并且您将能够访问注释来自的原始字典:< / p>

MapAnnotation *mapAnn = (MapAnnotation *)view.annotation;
NSLog(@"mapAnn.sourceDictionary = %@", mapAnn.sourceDictionary);