简体   繁体   English

如何存储CLLocationCoordinate2D?

[英]How to store CLLocationCoordinate2D?

I'm attempting to build an application that builds and saves routes similar to map my run. 我正在尝试构建一个应用程序来构建和保存类似于map run的路由。 I'm using the Breadcrumb sample code, specifically the CrumbPath and CrumbPathView as the base of my routes, from Apple. 我正在使用Breadcrumb示例代码,特别是来自Apple的CrumbPathCrumbPathView作为我的路线的基础。 Two questions: 两个问题:

  1. If I try to access the MKMapPoint *points object of the CrumbPath like so: 如果我尝试访问MKMapPoint *points对象, CrumbPath所示:

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

    my app crashes, saying: 我的应用程序崩溃,说:

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

    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. 我很难理解,因为在CrumbPath.m文件中,apple的人通过显式获取写锁定然后解锁它来写入“数组”,但是如果我获取了读锁并尝试从中读取它,它崩溃了。

  2. 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. 我尝试访问这些points的原因是为了获取MKMapPoints ,将它们转换为CLLocationCoordinate2D对象,然后保存它们以便我可以根据用户的请求重绘polyline 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: 由于我无法访问这些points ,因此我尝试从我的locationManager保存CLLocationCoordinate2D对象,我将其发送到数组中的_route传到我的Parse后端,但我总是收到错误说:

     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? 有没有人知道为什么我会收到这些错误?

Location Manager Delegate 位置经理代表

-(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 Logic 停止录制逻辑

    //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);

It should be: 它应该是:

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

As mentioned in my comments, %d should be used for count and %@ will cause a crash. 正如我的评论中所提到的, %d应该用于计数, %@会导致崩溃。

Regarding your second issue, you cannot add ac struct to an NSArray . 关于第二个问题,您不能将ac结构添加到NSArray You should wrap it in NSValue before adding it to an array. 在将其添加到数组之前,应将其包装在NSValue CLLocationCoordinate2D is a c-struct. CLLocationCoordinate2D是一个c-struct。 Check the documentation here. 请查看此处文档。

Change this: 改变这个:

[_routePoints addObject:_userLocation];

to: 至:

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

To get the coordinate back from NSValue , you can use, 要从NSValue获取坐标,您可以使用,

[aValue MKCoordinateValue];

As mentioned in your error message, you were trying to add CLLocationCoordinate2D to an array which expects an object. 如您的错误消息中所述,您试图将CLLocationCoordinate2D添加到需要对象的数组中。

Whatever api you're using to talk to parse is expecting an id which is a pointer to any object. 无论你使用什么api与parse交谈,都期望一个id是指向任何对象的指针。 A cllocationcoordinate2d is a c-struct of two doubles and not an object if I'm not mistaken. 如果我没有弄错的话,cllocationcoordinate2d是两个双精度的c结构而不是对象。 You should probably create a little wrapper object to save those two doubles and convert them to/from CLLocationCoordinate2d items. 您应该创建一个小包装器对象来保存这两个双精度并将它们转换为CLLocationCoordinate2d项目。

1: 1:

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

_route.points is not a String, and you are using the NSStrig formating symbol "%@". _route.points不是String,而您使用的是NSStrig格式符号“%@”。

Further: 进一步:

Since CLLocationCoordinate2D is a C-Sruct and not an Objective-C Object, you probaly want to create an own GeoPoint class. 由于CLLocationCoordinate2D是C-Sruct而不是Objective-C对象,因此您可能想要创建自己的GeoPoint类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM