问题描述
我正在尝试构建一个应用程序,该应用程序可以构建和保存类似于我的跑步地图的路线.我正在使用 面包屑 示例代码,特别是 CrumbPath
和 CrumbPathView
作为我路线的基础,来自 Apple.两个问题:
I'm attempting to build an application that builds and saves routes similar to map my run. I'm using the Breadcrumb sample code, specifically the CrumbPath
and CrumbPathView
as the base of my routes, from Apple. Two questions:
如果我尝试像这样访问
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
文件中,apple 的人通过显式获取写锁然后解锁它来写入数组",但是如果我获取了读锁并尝试从中读取,但它崩溃了.
Which I have a hard time understanding, because within the CrumbPath.m
file, the folks at apple write to the "array" by explicitly acquiring the write lock, and then unlocking it, but if I acquire the read lock and attempt to read from it, it crashes.
我尝试访问 points
的原因是试图获取 MKMapPoints
,将它们转换为 CLLocationCoordinate2D
对象,并保存它们,以便我可以根据用户的要求重新绘制 polyline
.由于我无法访问 points
,我尝试从发送到 _routelocationManager
中保存 CLLocationCoordinate2D
对象/code> 在一个数组中上传到我的 Parse 后端,但我总是收到一条错误消息:
The reason I attempt to access the points
is in an attempt to get the MKMapPoints
, convert them to CLLocationCoordinate2D
objects, and save them so I can redraw the polyline
at the user's request. Since I cannot get access to the points
, I attempt to save the CLLocationCoordinate2D
objects from my locationManager
that I send to the _route
in an array to upload to my Parse backend, but I always get an error saying:
Sending 'CLLocationCoordinate2D' to parameter of incompatible type 'id'
这并没有让这变得更容易.有人知道我为什么会收到这些错误吗?
Which isn't making this any easier. Does anybody have any insight to why I'm getting these errors?
位置经理代表
-(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);
}
}];
}
推荐答案
关于你的第一个问题,崩溃的原因是:
Regarding your first issue, the crash was because of this:
NSLog(@"%@", _route.pointCount);
应该是:
NSLog(@"%d", _route.pointCount);
正如我在评论中提到的,%d
应该用于计数,%@
会导致崩溃.
As mentioned in my comments, %d
should be used for count and %@
will cause a crash.
关于您的第二个问题,您不能将 c 结构添加到 NSArray
.在将它添加到数组之前,您应该将它包装在 NSValue
中.CLLocationCoordinate2D
是一个 c 结构.在此处查看文档.
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.
改变这个:
[_routePoints addObject:_userLocation];
到:
NSValue *aValue = [NSValue valueWithMKCoordinate:_userLocation];
[_routePoints addObject:aValue];
要从 NSValue
取回坐标,可以使用,
To get the coordinate back from NSValue
, you can use,
[aValue MKCoordinateValue];
如您的错误消息中所述,您试图将 CLLocationCoordinate2D
添加到需要对象的数组中.
As mentioned in your error message, you were trying to add CLLocationCoordinate2D
to an array which expects an object.
这篇关于如何存储 CLLocationCoordinate2D?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!