在NSArray中读取MKMapViews的注释

时间:2012-06-26 13:01:29

标签: objective-c xcode

我试图以这种方式在许多MKMapViews中找到注释的坐标:

NSMutableArray *latitudes = [NSMutableArray array];

for (MKMapView *map in MapViewArray)
{
  NSString *latitude = [NSString stringWithFormat:@"%.2f", [map.annotations lastObject].coordinate.latitude];                       
  [latitudes addObject: latitude];
}

NSMutableArray *longitudes = [NSMutableArray array];

for (MKMapView *map in MapViewArray)
{
     NSString *longitude = [NSString stringWithFormat:@"%.2f", [map.annotations lastObject].coordinate.latitude];


    [longitudes addObject: longitude];
}

这段代码虽然给了我这个错误:

property 'coordinate' not found on object of type id

我该如何解决?

数组中的地图视图属于以下类型:

@property (nonatomic, retain) IBOutlet MKMapView *mapView2;

1 个答案:

答案 0 :(得分:2)

您必须指定MKAnnotation协议才能进行编译:

NSMutableArray *latitudes = [NSMutableArray array];
NSMutableArray *longitudes = [NSMutableArray array];

for (MKMapView *map in MapViewArray)
{
    id<MKAnnotation> annotation = [map.annotations lastObject];
    NSString *latitude = [NSString stringWithFormat:@"%.2f", annotation.coordinate.latitude];                       
    NSString *longitude = [NSString stringWithFormat:@"%.2f", annotation.coordinate.latitude];        
    [longitudes addObject: longitude];
    [latitudes addObject: latitude];
}

此外,我优化了您的代码,只使用一个循环。