将CLLocationCoordinate2D发送到Xcode 4.5中的不兼容类型my code的参数 [英] Sending CLLocationCoordinate2D to parameter of incompatible type my code in Xcode 4.5
问题描述
我是Xcode的新手。我正在开发一个车辆跟踪应用程序,我需要同时显示更多的注释点。为此,我需要将坐标存储在数组中,但它显示错误:将CLLocationCoordinate2D发送到不兼容类型的参数
我的代码是:
I am new to 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
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...
推荐答案
如前所述,你只能添加对象到 NSArray
。
As already mentioned, you can only add objects to an NSArray
.
CLLocationCoordinate2D
不是对象 - 它是一个C结构。
CLLocationCoordinate2D
is not an object -- it is a C struct.
你发布的代码有一个解决方案:
创建一个 NSDictionary
将纬度和经度作为键/值对,并将生成的字典对象添加到数组中。
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.
另一种方法是创建一个 CLLocation
,如另一个答案所示。
Another approach is to create a CLLocation
as shown in another answer.
第三个想法是@trojanfoe之前回答的是将结构包装在 NSValue
中。
但,请注意有一个方便的 NSValue
特别为MapKit添加,它添加了这两个有用的帮助方法:
However, note that there is a convenient NSValue
addition specifically for MapKit that adds these two useful helper methods:
-
valueWithMKCoordinate:
返回NSValue
给出CLLocationCoordinate2D
-
MKCoordinateValue
返回CLLocationCoordinate2D
NSValue
valueWithMKCoordinate:
which returns anNSValue
given aCLLocationCoordinate2D
MKCoordinateValue
which returns aCLLocationCoordinate2D
for theNSValue
有关示例,请参阅如何存储CLLocationCoordinate2D?。
我强烈推荐的最后一个(至少在这个答案中)替代方法,而不是以上所有...
A final (at least in this answer) alternate approach which I would highly recommend instead of all the above is this...
- 为什么要存储 o nly 数组中的坐标?
- 您不想知道坐标是什么(车辆,地点等)吗?
- 为什么不创建一个自定义类来实现,比如
MKAnnotation
协议不仅坐标(作为CLLocationCoordinate2D
属性)而且所有其他与坐标相关的信息?该类将是一个NSObject< MKAnnotation>
的子类。 - 然后,您可以方便地访问一个地方的所有信息,而无需使用多个阵列并尝试保留对象的顺序相同,所以它们都有相同的索引等。
- 然后你可以直接将这些对象添加到
MKMapView
因为他们实现MKAnnotation
。
- 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 aCLLocationCoordinate2D
property) but also all the other information related to the coordinate? The class would be a subclass ofNSObject<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 implementMKAnnotation
.
这篇关于将CLLocationCoordinate2D发送到Xcode 4.5中的不兼容类型my code的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!