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

Posted

技术标签:

【中文标题】在 Xcode 4.5 中将 CLLocationCoordinate2D 发送到我的代码不兼容类型的参数【英文标题】:Sending CLLocationCoordinate2D to parameter of incompatible type my code in Xcode 4.5 【发布时间】:2014-07-24 06:39:51 【问题描述】:

我是 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
      

请建议我解决这个问题。 谢谢...

【问题讨论】:

这和你上一个问题基本相同。 顺便说一句 - 你真的在使用 Xcode 4 吗? Apple 不会接受这样的应用程序,因此如果您打算将应用程序发送给 Apple,则必须使用 Xcode 5。随着即将推出的 ios 8,您应该使用 Xcode 6 来支持 iOS 8。 @rmaddy 请建议我解决这个问题,谢谢... 请在查看您上一个问题的答案后,为此付出一些努力。每次遇到困难时,您都无法通过让其他人为您完成工作来编写应用程序。尝试自己先解决这个问题。做一次尝试。如果您在尝试后仍然卡住,请发布您尝试过的内容。 嗯,你似乎没有在学习。我在上一个问题中告诉过你,那些structs 不能直接存储在 Objective-C 集合中,你接受了答案,但实际上你并没有从中学到任何东西:***.com/questions/24925546/… 【参考方案1】:

CLLocationCoordinate2D center 不是对象。您只能在 NSArray 中存储对象。 为此使用CLLocation

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

【讨论】:

抱歉,您能简单介绍一下吗?根据上面的代码 @iBeginner CLLocationCoordinate2D 是一个 C 结构。如果要将其存储在数组中,则应使用 C 数组。但是,如果您想将位置对象存储在数组中,则应将其转换为 Obj C 对象。选项之一是将 CLLocationCoordinate2D 存储在 CLLocation 对象中。另一种方法是将其转换为 NSValue。您可以在此处查看此方法的文档:developer.apple.com/library/ios/documentation/MapKit/Reference/…【参考方案2】:

如前所述,您只能将对象添加到 NSArray

CLLocationCoordinate2D 不是一个对象——它是一个 C 结构体。

您发布的代码本身有一个解决方案: 创建一个NSDictionary,将纬度和经度作为键/值对,并将生成的字典对象添加到数组中。

另一种方法是创建一个CLLocation,如另一个答案所示。

第三个想法是@***foe 之前回答的问题,即将结构包装在NSValue 中。

然而,请注意there is a convenient NSValue addition specifically for MapKit 添加了这两个有用的辅助方法:

valueWithMKCoordinate: 在给定 CLLocationCoordinate2D 的情况下返回 NSValue MKCoordinateValueNSValue 返回一个 CLLocationCoordinate2D

例如,请参阅How to store CLLocationCoordinate2D?。

我强烈推荐的最终(至少在这个答案中)替代方法是这个......

为什么要将坐标存储在数组中? 您不想知道坐标是什么(哪个车辆、哪个地点等)? 为什么不创建一个自定义类,该类实现例如MKAnnotation 协议,不仅具有坐标(作为CLLocationCoordinate2D 属性),还具有所有其他相关信息到坐标该类将是NSObject&lt;MKAnnotation&gt;的子类。 然后,您可以方便地在一个位置访问所有信息,而无需使用多个数组并尝试保持对象的顺序相同,以便它们都具有相同的索引等。 然后您可以直接将这些对象添加到MKMapView,因为它们实现了MKAnnotation

【讨论】:

【参考方案3】:

CLLocationCoordinate2D 是一个 C 结构体,所以你需要先把它放在 NSValue 容器中。

NSValue 对象是单个 C 或 Objective-C 的简单容器 数据项。它可以保存任何标量类型,例如 int、float 和 char,以及指针、结构和对象 id 引用。采用 此类用于处理集合中的此类数据类型(例如 NSArray 和 NSSet)、键值编码和其他需要 Objective-C 对象。

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

【讨论】:

【参考方案4】:

试试这个

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
  

【讨论】:

以上是关于在 Xcode 4.5 中将 CLLocationCoordinate2D 发送到我的代码不兼容类型的参数的主要内容,如果未能解决你的问题,请参考以下文章

在 Swift 中将 CLLocation 转换为 MKMapItem

在 Swift 中将 CLLocationCoordinate2D 转换为 CLLocation 时出错

我在哪里可以在 xcode 4.5 中启用“get-task-allow”以进行 iCloud 调试(带有权利文件)?

在 Xcode 6 和 Xcode 4.5 之间切换

并排安装 xcode 4.4 和 xcode 4.5?

在 Xcode 4.2 上打开 Xcode 4.5 项目时出错