How to store CLLocationCoordinate2D?

旧街凉风 提交于 2019-11-30 09:27:41

Regarding your first issue, the crash was because of this:

NSLog(@"%@", _route.pointCount);

It should be:

NSLog(@"%d", _route.pointCount);

As mentioned in my comments, %d should be used for count and %@ will cause a crash.

Regarding your second issue, you cannot add a c struct to an NSArray. You should wrap it in NSValue before adding it to an array. CLLocationCoordinate2D is a c-struct. Check the documentation here.

Change this:

[_routePoints addObject:_userLocation];

to:

NSValue *aValue = [NSValue valueWithMKCoordinate:_userLocation];
[_routePoints addObject:aValue];

To get the coordinate back from NSValue, you can use,

[aValue MKCoordinateValue];

As mentioned in your error message, you were trying to add CLLocationCoordinate2D to an array which expects an object.

Whatever api you're using to talk to parse is expecting an id which is a pointer to any object. A cllocationcoordinate2d is a c-struct of two doubles and not an object if I'm not mistaken. You should probably create a little wrapper object to save those two doubles and convert them to/from CLLocationCoordinate2d items.

1:

Line: NSLog(@"%@", _route.points); is wrong

_route.points is not a String, and you are using the NSStrig formating symbol "%@".

Further:

Since CLLocationCoordinate2D is a C-Sruct and not an Objective-C Object, you probaly want to create an own GeoPoint class.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!