首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何存储CLLocationCoordinate2D?

如何存储CLLocationCoordinate2D?
EN

Stack Overflow用户
提问于 2013-01-14 22:44:56
回答 3查看 6.8K关注 0票数 7

我正在尝试构建一个应用程序来构建和保存类似于映射我的运行的路由。我正在使用来自苹果的Breadcrumb示例代码,特别是CrumbPathCrumbPathView作为我的路线的基础。两个问题:

  1. 如果我尝试访问CrumbPathCrumbPath对象,如下所示: _route lockForReading;NSLog(“%@”,_route.points);NSLog(“%d”,_route.pointCount);_route unlockForReading; 我的应用程序崩溃了,说: 线程1: EXC_BAD_ACCESS (代码: 1,地址: 0x9450342d) 这一点我很难理解,因为在CrumbPath.m文件中,苹果公司的人员通过显式获取写锁,然后解锁,将“数组”写入“数组”,但是如果我获得读锁并试图从中读取,它就会崩溃。
  2. 我尝试访问points的原因是试图获取MKMapPoints,将它们转换为CLLocationCoordinate2D对象,并保存它们,以便我可以根据用户的请求重新绘制polyline。由于无法访问points,所以我尝试将CLLocationCoordinate2D对象从我的locationManager中保存起来,然后将这些对象发送到数组中的_route中上传到我的Parse后端,但是我总是会收到一个错误消息: 将“CLLocationCoordinate2D”发送到不兼容类型“id”的参数 这不会让这件事变得更容易。有人知道我为什么会犯这些错误吗?

位置管理器代表

代码语言:javascript
复制
-(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];
    }
}

停止记录逻辑

代码语言:javascript
复制
    //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);
            }
        }];
    }
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-01-14 23:00:46

关于你的第一个问题,坠机的原因是:

代码语言:javascript
复制
NSLog(@"%@", _route.pointCount);

它应该是:

代码语言:javascript
复制
NSLog(@"%d", _route.pointCount);

正如我在评论中提到的,%d应该用于计数,而%@将导致崩溃。

关于第二个问题,您不能向NSArray添加c结构。在将其添加到数组之前,应该将其包装在NSValue中。CLLocationCoordinate2D是一个c-结构.检查documentation here.

改变这一点:

代码语言:javascript
复制
[_routePoints addObject:_userLocation];

至:

代码语言:javascript
复制
NSValue *aValue = [NSValue valueWithMKCoordinate:_userLocation];
[_routePoints addObject:aValue];

要从NSValue获取坐标,可以使用

代码语言:javascript
复制
[aValue MKCoordinateValue];

正如您在错误消息中提到的,您试图将CLLocationCoordinate2D添加到期望对象的数组中。

票数 10
EN

Stack Overflow用户

发布于 2013-01-14 22:52:06

无论您使用什么api来分析,都需要一个id,它是指向任何对象的指针。如果我没有弄错的话,cllocationcoordinate2d是两个双倍的c-结构,而不是一个对象。您可能应该创建一个小包装器对象来保存这两个双倍,并将它们转换为/从CLLocationCoordinate2d项。

票数 1
EN

Stack Overflow用户

发布于 2013-01-14 22:59:59

1:

行:NSLog(@"%@", _route.points);是错的

_route.points不是字符串,您使用的是NSStrig格式符号"%@“。

此外:

由于CLLocationCoordinate2D是一个create而不是Object对象,所以您可能希望创建一个自己的GeoPoint类。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14328173

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档