0

我试图以这种方式在许多 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;
4

1 回答 1

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];
}

我还优化了您的代码以仅使用一个循环。

于 2012-06-26T13:38:07.880 回答