我正在尝试构建一个应用程序,该应用程序可以构建和保存类似于绘制我的跑步路线的路线。我正在使用面包屑示例代码,特别是CrumbPath
and CrumbPathView
作为我路线的基础,来自 Apple。两个问题:
-
如果我尝试访问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
我的对象locationManager
我发送到_route
以数组形式上传到我的Parse后端,但我总是收到错误消息:
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);
}
}];
}