【发布时间】:2012-12-28 23:41:30
【问题描述】:
我正在尝试构建一个应用程序来构建和保存类似于映射我的运行的路线。我正在使用来自 Apple 的 Breadcrumb 示例代码,特别是 CrumbPath 和 CrumbPathView 作为我的路线的基础。两个问题:
-
如果我尝试像这样访问
CrumbPath的MKMapPoint *points对象:[_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,因此我尝试将我发送到_route的locationManager中的CLLocationCoordinate2D对象保存在一个数组中,以上传到我的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);
}
}];
}
【问题讨论】:
-
NSLog(@"%@", _route.pointCount);这是一个整数吗?那么你应该使用%d -
嗯,是的。对不起,我很快就输入了这个。还是不行。
-
在那之后它还会崩溃吗?以及如何使用
CLLocationCoordinate2D对象调用该方法?你能发布那个代码吗? -
MKMapPoint 是一个 C 结构,而不是一个 Objective-C 类。
-
更正,pointCount 有效。立即向 OP 添加代码
标签: ios mkmapview cllocation parse-platform