如何存储CLLocationCoordinate2D?

Hig*_*asy 7 mkmapview cllocation ios parse-platform

我正在尝试构建一个应用程序来构建和保存类似于map run的路由.我用面包屑示例代码,特别是CrumbPathCrumbPathView我的路线的基础上,从苹果.两个问题:

  1. 如果我尝试访问类似的MKMapPoint *points对象,CrumbPath那么:

    [_route lockForReading];
    NSLog(@"%@", _route.points);
    NSLog(@"%d", _route.pointCount);
    [_route unlockForReading];
    
    Run Code Online (Sandbox Code Playgroud)

    我的应用程序崩溃,说:

    Thread 1: EXC_BAD_ACCESS (code: 1, address: 0x9450342d)
    
    Run Code Online (Sandbox Code Playgroud)

    我很难理解,因为在CrumbPath.m文件中,苹果的人通过显式获取写锁定然后解锁它来写入"数组",但是如果我获取了读锁并试图从中读取它,那么崩溃.

  2. 我尝试访问的原因points是尝试获取MKMapPoints,将它们转换为CLLocationCoordinate2D对象,并保存它们,以便我可以polyline根据用户的请求重绘.由于我无法访问points,我尝试保存我发送到数组中的CLLocationCoordinate2D对象以上传到我的Parse后端,但我总是收到错误说:locationManager_route

    Sending 'CLLocationCoordinate2D' to parameter of incompatible type 'id'
    
    Run Code Online (Sandbox Code Playgroud)

    这并没有使这更容易.有没有人知道为什么我会收到这些错误?

位置经理代表

-(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];
    }
}
Run Code Online (Sandbox Code Playgroud)

停止录制逻辑

    //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);
            }
        }];
    }
Run Code Online (Sandbox Code Playgroud)

iDe*_*Dev 10

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

NSLog(@"%@", _route.pointCount);
Run Code Online (Sandbox Code Playgroud)

它应该是:

NSLog(@"%d", _route.pointCount);
Run Code Online (Sandbox Code Playgroud)

正如我的评论所述,%d应该用于计数并%@会导致崩溃.

关于你的第二个问题,你不能添加ac结构NSArray.NSValue在将其添加到数组之前,您应该将其包装起来.CLLocationCoordinate2D是一个c结构.请查看此处文档.

改变这个:

[_routePoints addObject:_userLocation];
Run Code Online (Sandbox Code Playgroud)

至:

NSValue *aValue = [NSValue valueWithMKCoordinate:_userLocation];
[_routePoints addObject:aValue];
Run Code Online (Sandbox Code Playgroud)

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

[aValue MKCoordinateValue];
Run Code Online (Sandbox Code Playgroud)

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