本文介绍了读取 NSArray 中 MKMapViews 的注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to find the coordinates of an annotation in many MKMapViews in this way:

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

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

This code though gives me this error:

property 'coordinate' not found on object of type id

我该如何解决??

数组中的地图视图属于这种类型:

The map views in the array are of this type:

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

推荐答案

您必须指定 MKAnnotation 协议才能使其编译:

You must specify the MKAnnotation protocol to make it compile:

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

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

Also I optimized your code to use only one loop.

这篇关于读取 NSArray 中 MKMapViews 的注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 07:24