Ios 如何存储CLLocationCoordinate2D?
我正在尝试构建一个应用程序,该应用程序可以构建和保存类似于映射我的跑步路线的路线。我正在使用示例代码,特别是苹果的Ios 如何存储CLLocationCoordinate2D?,ios,mkmapview,cllocation,parse-platform,Ios,Mkmapview,Cllocation,Parse Platform,我正在尝试构建一个应用程序,该应用程序可以构建和保存类似于映射我的跑步路线的路线。我正在使用示例代码,特别是苹果的CrumbPath和CrumbPathView作为我路线的基础。两个问题: 如果我尝试访问CrumbPath的MKMapPoint*点对象,如下所示: [_route lockForReading]; NSLog(@"%@", _route.points); NSLog(@"%d", _route.pointCount); [_route unlockForReading]; 我的
CrumbPath
和CrumbPathView
作为我路线的基础。两个问题:
CrumbPath
的MKMapPoint*点
对象,如下所示:
[_route lockForReading];
NSLog(@"%@", _route.points);
NSLog(@"%d", _route.pointCount);
[_route unlockForReading];
我的应用程序崩溃,说:
Thread 1: EXC_BAD_ACCESS (code: 1, address: 0x9450342d)
我很难理解,因为在CrumbPath.m
文件中,苹果公司的员工通过显式获取写锁,然后解锁它来写入“数组”,但如果我获取读锁并尝试从中读取,它就会崩溃点
的原因是试图获取MKMapPoints
,将它们转换为CLLocationCoordinate2D
对象,并保存它们,以便我可以根据用户的请求重新绘制多段线
。由于我无法访问点
,因此我尝试从我的位置管理器
中保存CLLocationCoordinate2D
对象,并将其发送到数组中的\u路由
,以上载到我的后端,但我总是会收到一个错误消息:
Sending 'CLLocationCoordinate2D' to parameter of incompatible type 'id'
这并没有让这变得更容易。有人知道我为什么会犯这些错误吗-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
if (_userLocation.longitude != manager.location.coordinate.longitude
&& _userLocation.latitude != manager.location.coordinate.latitude) {
_userLocation = manager.location.coordinate;
}
if (_isRecording) {
if (!_route) {
NSLog(@"lat: %f, lng: %f", _userLocation.latitude, _userLocation.longitude);
_route = [[CrumbPath alloc] initWithCenterCoordinate:_userLocation];
[_mapView addOverlay:_route];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(_userLocation, 2000, 2000);
[_mapView setRegion:region animated:YES];
}else {
MKMapRect updateRect = [_route addCoordinate:_userLocation];
if (!MKMapRectIsNull(updateRect)) {
MKZoomScale currentZoomScale = (CGFloat)(_mapView.bounds.size.width / _mapView.visibleMapRect.size.width);
CGFloat lineWidth = MKRoadWidthAtZoomScale(currentZoomScale);
updateRect = MKMapRectInset(updateRect, -lineWidth, -lineWidth);
[_routeView setNeedsDisplayInMapRect:updateRect];
}
}
[_routePoints addObject:_userLocation];
[_route lockForReading];
NSLog(@"%d", _route.pointCount);
NSLog(@"%@", _route.points);
[_route unlockForReading];
}
}
停止记录逻辑
//stop recording
NSLog(@"STOP");
if (_route) {
NSLog(@"There is a route");
//Show route options toolbar
[_route lockForReading];
NSLog(@"%@", _route);
NSLog(@"%d", _route.pointCount);
NSLog(@"%@", _route.points);
PFObject *routeToSave = [PFObject objectWithClassName:@"Routes"];
//[routeToSave setObject:_route forKey:@"routePoints"];
[_route unlockForReading];
[routeToSave saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
NSLog(@"%c", succeeded);
}else {
NSLog(@"%@", error);
}
}];
}
无论您使用什么api来解析,都需要一个id,它是指向任何对象的指针。cllocationcoordinate2d是两个双精度的c结构,如果我没有弄错的话,它不是对象。您可能应该创建一个小包装器对象来保存这两个double,并将它们转换为CLLocationCoordinate2d项或从中转换为CLLocationCoordinate2d项。1: 行:
NSLog(@“%@,_route.points)
;这是错误的
_route.points不是字符串,您正在使用NSStrig格式化符号“%@”
进一步:
由于CLLocationCoordinate2D是C-Sruct而不是Objective-C对象,因此您可能希望创建自己的地质点类。关于第一个问题,崩溃是因为:
NSLog(@"%@", _route.pointCount);
应该是:
NSLog(@"%d", _route.pointCount);
正如我在评论中提到的,%d
应用于计数,%@
将导致崩溃
关于第二个问题,您不能将c结构添加到NSArray
。在将其添加到数组之前,应将其包装在NSValue
中CLLocationCoordinate2D
是一个c结构。检查
更改此项:
[_routePoints addObject:_userLocation];
致:
要从NSValue
获取坐标,可以使用
[aValue MKCoordinateValue];
如错误消息中所述,您试图将
CLLocationCoordinate2D
添加到需要对象的数组中。NSLog(@“%@”,u route.pointCount)代码>这是整数吗?那么你应该使用%d
嗯,是的。对不起,我打得很快。它仍然不起作用。即使在那之后它还会崩溃吗?您如何使用CLLocationCoordinate2D
对象调用该方法?你能发布那段代码吗?MKMapPoint是一个C结构,不是Objective-C类。更正,pointCount有效。向OP now添加代码崩溃不是由初始的NSLog(@“%@,_route.pointCount)引起的代码>因为NSLog(@“%@”,_route.points)代码>行在它之前,我在意识到它永远无法到达\u route.pointCount
行后重新排序了它。但在你纠正我之前,我无知到使用了%@
。而我的initWithCenterCoordinate:
方法正好说明了这一点。如果您按问题所示使用它,它将崩溃。对于第二个问题,您确定使用的是(CLLocationCoordinate2D)
而不是(CLLocationCoordinate2D*)
?是的,我没有偏离苹果的示例代码。我把实际的文件放到我的项目中,只添加了NSLog()
语句。@HighFlyingFantasy,解决了这个问题。用解决方案更新了答案。比创建一个无用的包装器类要好得多。谢谢,是的。包装器可能应该是PFGeoPoint。
[aValue MKCoordinateValue];