简体   繁体   中英

How to get the lat/long in MapBox iOS SDK after drag&drop

I'm using drag&drop the same way as in the official example of MapBox: https://www.mapbox.com/ios-sdk/maps/examples/draggable-views/

I can't find a way to get the lat/long of the dragged annotation after dropping. There is no delegate. Neither in MGLMapViewDelegate nor in any other delegate.

MGLAnnotationView has some dragState-members which do not match the lat/long-request. https://www.mapbox.com/ios-sdk/api/4.6.0/Classes/MGLAnnotationView.html

When I use initWithAnnotation:reuseIdentifier: instead of initWithReuseIdentifier: the MGLAnnotationView has its annotation-property set correctly. But the annotation.coordinate does not get updated.

https://www.mapbox.com/ios-sdk/api/4.6.0/Classes/MGLAnnotationView.html#/c:objc(cs)MGLAnnotationView(im)initWithAnnotation:reuseIdentifier :

So how can I get the lat/long after dropping?

If I'm understanding you correctly, this should be as simple as checking the annotation associated with the annotationView once the dragging has finished.

Have you tried something like the following inside your draggable MGLAnnotationView subclass?

let newLatitude = self.annotation?.coordinate.latitude
let newLongitude = self.annotation?.coordinate.longitude

-- allowing for the usual optional checks, etc. I have these lines inside the endDragging() method.

The coordinate of the associated annotation gets updated only at ending not during dragging.

    case MGLAnnotationViewDragStateDragging:
        NSLog(@"%@ / %@", @(self.annotation.coordinate.latitude), @(self.annotation.coordinate.longitude));
        break;

    case MGLAnnotationViewDragStateEnding:
    case MGLAnnotationViewDragStateCanceling:
        NSLog(@"endDragging: %@ / %@", @(self.annotation.coordinate.latitude), @(self.annotation.coordinate.longitude));
        [self endDragging];
        break;

Results in:

2018-11-17 10:15:22.122488+0100 FreeFlightAtlas AdHoc[4780:1555191] -34.613152 / -58.377232
2018-11-17 10:15:22.145187+0100 FreeFlightAtlas AdHoc[4780:1555191] -34.613152 / -58.377232
2018-11-17 10:15:22.169465+0100 FreeFlightAtlas AdHoc[4780:1555191] -34.613152 / -58.377232
2018-11-17 10:15:22.193490+0100 FreeFlightAtlas AdHoc[4780:1555191] -34.613152 / -58.377232
2018-11-17 10:15:22.485666+0100 FreeFlightAtlas AdHoc[4780:1555191] endDragging: 25.12811184395072 / -41.88967123348579

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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