[英]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
,它返回NSValue
的CLLocationCoordinate2D
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... 我强烈建议而不是以上所有建议的最终替代方法(至少在此答案中是这样)...
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>
的子类。 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.