我正在尝试构建一个应用程序来构建和保存类似于映射我的运行的路线。我正在使用来自 Apple 的Breadcrumb示例代码,特别是CrumbPath
andCrumbPathView
作为我的路线的基础。两个问题:
如果我尝试访问类似的
MKMapPoint *points
对象CrumbPath
:[_route lockForReading]; NSLog(@"%@", _route.points); NSLog(@"%d", _route.pointCount); [_route unlockForReading];
我的应用程序崩溃,说:
Thread 1: EXC_BAD_ACCESS (code: 1, address: 0x9450342d)
我很难理解,因为在
CrumbPath.m
文件中,苹果公司的人通过显式获取写锁然后解锁它来写入“数组”,但是如果我获取读锁并尝试从中读取,它崩溃。我尝试访问 的原因
points
是试图获取MKMapPoints
,将它们转换为CLLocationCoordinate2D
对象并保存它们,以便我可以根据用户的请求重绘polyline
。由于我无法访问 ,因此points
我尝试将我发送到的CLLocationCoordinate2D
对象保存在一个数组中以上传到我的Parse后端,但我总是收到一条错误消息:locationManager
_route
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);
}
}];
}