【问题标题】:How to store CLLocationCoordinate2D?如何存储 CLLocationCoordinate2D?
【发布时间】:2012-12-28 23:41:30
【问题描述】:

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

  1. 如果我尝试像这样访问CrumbPathMKMapPoint *points 对象:

    [_route lockForReading];
    NSLog(@"%@", _route.points);
    NSLog(@"%d", _route.pointCount);
    [_route unlockForReading];
    

    我的应用崩溃了,说:

    Thread 1: EXC_BAD_ACCESS (code: 1, address: 0x9450342d)
    

    我很难理解,因为在CrumbPath.m 文件中,苹果公司的人通过显式获取写锁然后解锁它来写入“数组”,但是如果我获取读锁并尝试要从中读取,它会崩溃。

  2. 我尝试访问points 的原因是为了尝试获取MKMapPoints,将它们转换为CLLocationCoordinate2D 对象,然后保存它们,以便我可以根据用户的请求重绘polyline。由于我无法访问points,因此我尝试将我发送到_routelocationManager 中的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


【解决方案1】:

关于你的第一个问题,崩溃是因为这个:

NSLog(@"%@", _route.pointCount);

应该是:

NSLog(@"%d", _route.pointCount);

正如我的 cmets 中提到的,%d 应该用于计数,%@ 会导致崩溃。

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

改变这个:

[_routePoints addObject:_userLocation];

到:

NSValue *aValue = [NSValue valueWithMKCoordinate:_userLocation];
[_routePoints addObject:aValue];

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

[aValue MKCoordinateValue];

如您的错误消息中所述,您试图将 CLLocationCoordinate2D 添加到需要对象的数组中。

【讨论】:

  • 崩溃不是由最初的NSLog(@"%@", _route.pointCount); 引起的,因为NSLog(@"%@", _route.points); 行在它之前,我意识到它永远不会到达_route.pointCount 行后重新排序。但是在您纠正我之前,我无知到使用%@。而我的initWithCenterCoordinate: 方法正是如此。
  • 如果您按照问题所示使用它,它会崩溃。对于第二个问题,您确定您使用的是(CLLocationCoordinate2D) 而不是(CLLocationCoordinate2D *)
  • 是的,我没有偏离 Apple 示例代码。我把实际文件放到了我的项目中,只添加了NSLog() 语句。
  • @HighFlyingFantasy,有问题。用解决方案更新了答案。
  • 比创建一个无用的包装类要好得多。谢谢。
【解决方案2】:

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

【讨论】:

  • 是的。包装器应该是 PFGeoPoint。
【解决方案3】:

1:

线路:NSLog(@"%@", _route.points);错了

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

进一步:

由于 CLLocationCoordinate2D 是 C-Sruct 而不是 Objective-C 对象,您可能想要创建自己的 GeoPoint 类。

【讨论】:

    猜你喜欢
    • 2013-09-25
    • 2011-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-29
    • 2015-08-28
    • 1970-01-01
    相关资源
    最近更新 更多