发送CLLocationCoordinate2D到不兼容types的参数在Xcode 4.5中我的代码
我是Xcode的新手。 我正在开发一个车辆跟踪应用程序,我需要同时显示更多的注释点。 为此,我需要将坐标存储在数组中,但显示错误:将Sending CLLocationCoordinate2D to parameter of incompatible type
我的代码是:
NSString *urlMapString=[NSString stringWithFormat:@"http://logix.com/logix_webservice/mapvehiclelist.php?uid=20&format=json"]; NSURL *urlMap=[NSURL URLWithString:urlMapString]; NSData *dataMap=[NSData dataWithContentsOfURL:urlMap]; if(dataMap!=NULL){ NSError *errorMap; NSMutableArray *coordinates = [[NSMutableArray alloc]init]; NSDictionary *jsonMap = [NSJSONSerialization JSONObjectWithData:dataMap options:kNilOptions error:&errorMap]; NSArray *resultsMap = [jsonMap valueForKey:@"posts"]; for(int count=1;count<resultsMap.count;count++) { NSDictionary *resMap = [[resultsMap objectAtIndex:count]valueForKey:@"post"]; NSString *latOrgstring =[resMap valueForKey:@"latitude"]; latitude=[latOrgstring doubleValue]; NSString *longitudeString=[resMap valueForKey:@"longitude"]; longitude=[longitudeString doubleValue]; //Center CLLocationCoordinate2D center; center.latitude=latitude; center.longitude=longitude; [coordinates addObject:center]; //here it shows the error } }
请build议我解决这个问题。
谢谢…
CLLocationCoordinate2D center
不是一个对象。 您只能将对象存储在NSArray中。 为此使用CLLocation
。
CLLocation *location = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
如前所述,您只能将对象添加到NSArray
。
CLLocationCoordinate2D
不是一个对象 – 它是一个C结构。
您发布的代码有一个解决scheme:
用纬度和经度创build一个NSDictionary
作为键/值对,并将生成的字典对象添加到数组中。
另一种方法是创build一个CLLocation
,如另一个答案所示。
第三个想法是@trojanfoe以前回答的是将结构包装在NSValue
。
但是请注意, MapKit中增加了一个方便的NSValue
添加了两个有用的帮助器方法:
-
valueWithMKCoordinate:
它返回给定CLLocationCoordinate2D
的NSValue
-
MKCoordinateValue
,它为NSValue
返回一个CLLocationCoordinate2D
有关示例,请参阅如何存储CLLocationCoordinate2D? 。
最后(至less在这个答案)替代方法,我会强烈build议,而不是所有上述是这个…
- 为什么你只想在数组中存储坐标?
- 你不想知道坐标是什么(车辆,地点等)?
- 为什么不创build一个实现
MKAnnotation
协议的自定义类 ,它不仅具有坐标(作为CLLocationCoordinate2D
属性),而且还具有与坐标有关的所有其他信息 ? 该类将是NSObject<MKAnnotation>
一个子类。 - 您可以方便地访问所有的信息在一个地方,而不使用多个数组,并试图保持对象的顺序相同,所以他们都有相同的索引,等等。
- 您可以直接将这些对象添加到
MKMapView
因为他们实现MKAnnotation
。
CLLocationCoordinate2D是一个C结构,所以你首先需要把它放在NSValue容器中。
NSValue对象是单个C或Objective-C数据项的简单容器。 它可以保存任何标量types,如int,float和char,以及指针,结构和对象id引用。 使用这个类来处理集合中的数据types(如NSArray和NSSet),键值编码和其他需要Objective-C对象的API。
[coordinates addObject:[NSValue value:&coordinate withObjCType:@encode(CLLocationCoordinate2D)]];
尝试这个
CLLocationCoordinate2D new_coordinate = CLLocationCoordinate2DMake(latitude, longitude); [points addObject:[NSValue valueWithMKCoordinate:new_coordinate]];
要么
CLLocation *location = [[CLLocation alloc] initWithLatitude:lat longitude:lon]; [coordinates addObject: location];
不能ü直接添加坐标到mapview,这意味着将MKAnnotation放在Mapview上,而不是坐标到数组中?
看到下面我评论线
for(int count=1;count<resultsMap.count;count++) { NSDictionary *resMap = [[resultsMap objectAtIndex:count]valueForKey:@"post"]; NSString *latOrgstring =[resMap valueForKey:@"latitude"]; latitude=[latOrgstring doubleValue]; NSString *longitudeString=[resMap valueForKey:@"longitude"]; longitude=[longitudeString doubleValue]; //Center CLLocationCoordinate2D center; center.latitude=latitude; center.longitude=longitude; ----------------------------------------- ////// Annotation is MKAnnotation Subclass Annotation * cartAnn = [Annotation new]; cartAnn.coordinate = center; [self.mapView addAnnotation: cartAnn]; ---------------------------------------------------- /////// [coordinates addObject:center]; //here it shows the error }