简体   繁体   English

在Xcode 4.5中将CLLocationCoordinate2D发送到不兼容类型的参数我的代码

[英]Sending CLLocationCoordinate2D to parameter of incompatible type my code in Xcode 4.5

I am new to Xcode. 我是Xcode的新手。 I am developing a vehicle tracking app for that I need to display more annotation points simultaneously. 我正在开发一个车辆跟踪应用程序,因为我需要同时显示更多注释点。 For this I need to store the coordinates in the array but it shows the error: Sending CLLocationCoordinate2D to parameter of incompatible type 为此,我需要将坐标存储在数组中,但是会显示错误:将Sending CLLocationCoordinate2D to parameter of incompatible type
my code is: 我的代码是:

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

Kindly advice me to solve this problem. 请给我解决这个问题。
Thank you... 谢谢...

CLLocationCoordinate2D center isn't an object. CLLocationCoordinate2D center不是对象。 You can store only objects in NSArray. 您只能在NSArray中存储对象。 Use CLLocation for this. 为此使用CLLocation

CLLocation *location = [[CLLocation alloc] initWithLatitude:lat longitude:lon];

As already mentioned, you can only add objects to an NSArray . 如前所述,您只能将对象添加到NSArray

CLLocationCoordinate2D is not an object -- it is a C struct. CLLocationCoordinate2D不是对象,而是C结构。


The code you posted itself has one solution: 您自己发布的代码具有一种解决方案:
Create a NSDictionary with the latitude and longitude as key/value pairs and add the resulting dictionary object to the array. 创建一个NSDictionary ,将纬度和经度作为键/值对,然后将结果字典对象添加到数组中。


Another approach is to create a CLLocation as shown in another answer. 另一种方法是创建另一个答案所示的CLLocation


A third idea is what @trojanfoe answered previously which is to wrap the struct in an NSValue . 第三个想法是@trojanfoe先前回答的是将结构包装在NSValue

However , note that there is a convenient NSValue addition specifically for MapKit that adds these two useful helper methods: 但是 ,请注意, 有一个方便的NSValue专门用于MapKit ,它添加了以下两个有用的帮助程序方法:

  • valueWithMKCoordinate: which returns an NSValue given a CLLocationCoordinate2D valueWithMKCoordinate:在给定CLLocationCoordinate2D返回NSValue
  • MKCoordinateValue which returns a CLLocationCoordinate2D for the NSValue MKCoordinateValue ,它返回NSValueCLLocationCoordinate2D

For an example, see How to store CLLocationCoordinate2D? 有关示例,请参见如何存储CLLocationCoordinate2D? .


A final (at least in this answer) alternate approach which I would highly recommend instead of all the above is this... 我强烈建议而不是以上所有建议的最终替代方法(至少在此答案中是这样)...

  • Why do you want to store only the coordinates in an array? 为什么要将坐标存储在数组中?
  • Wouldn't you want to know what the coordinates are for (which vehicle, place, etc)? 您是否不想知道坐标的含义(哪个车辆,地点等)?
  • Why not create a custom class that implements, say, the MKAnnotation protocol and has not only the coordinates (as a CLLocationCoordinate2D property) but also all the other information related to the coordinate ? 为什么不创建一个自定义类该类实现例如MKAnnotation协议,并且不仅具有坐标(作为CLLocationCoordinate2D属性),而且还具有与坐标有关的所有其他信息 The class would be a subclass of NSObject<MKAnnotation> . 该类将是NSObject<MKAnnotation>的子类。
  • You could then conveniently access all the information in one place without using multiple arrays and trying to keep the objects in the same order so they all have the same index, etc. 然后,您可以方便地在一个位置访问所有信息,而无需使用多个数组,并尝试将对象保持相同的顺序,以便它们都具有相同的索引,等等。
  • You could then directly add these objects to an MKMapView since they implement MKAnnotation . 然后可以将这些对象直接添加到MKMapView因为它们实现了MKAnnotation

CLLocationCoordinate2D is a C struct, so you need to put it in NSValue container at first. CLLocationCoordinate2D是C结构,因此您首先需要将其放入NSValue容器中。

An NSValue object is a simple container for a single C or Objective-C data item. NSValue对象是单个C或Objective-C数据项的简单容器。 It can hold any of the scalar types such as int, float, and char, as well as pointers, structures, and object id references. 它可以容纳任何标量类型,例如int,float和char,以及指针,结构和对象id引用。 Use this class to work with such data types in collections (such as NSArray and NSSet), key-value coding, and other APIs that require Objective-C objects. 使用此类可处理集合中的此类数据类型(例如NSArray和NSSet),键值编码以及其他需要Objective-C对象的API。

[coordinates addObject:[NSValue value:&coordinate withObjCType:@encode(CLLocationCoordinate2D)]];

Try this 尝试这个

CLLocationCoordinate2D new_coordinate = CLLocationCoordinate2DMake(latitude, longitude);
[points addObject:[NSValue valueWithMKCoordinate:new_coordinate]];

Or 要么

CLLocation *location = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
[coordinates addObject: location];

Can't u directly add coordinates to mapview that means Place MKAnnotation on Mapview, instead of taking coordinates to into array ? 您不能直接将坐标添加到mapview中,这意味着将MKAnnotation放置在Mapview上,而不是将坐标放入数组中吗?

SEE Below i commented with lines 请参阅下面我用行注释

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
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM