mapbox / mapbox-maps-ios

Interactive, thoroughly customizable maps for iOS powered by vector tiles and Metal
https://www.mapbox.com/mapbox-mobile-sdk
Other
452 stars 149 forks source link

PointAnnotation Location Coordinate Update #2086

Open dlpgithub opened 6 months ago

dlpgithub commented 6 months ago

Environment

Observed behavior and steps to reproduce

Our app receives location updates via stream. The app displays the incoming locations as pointannotation. Annotations have unique ids.

With one of the latest mapbox updates the app crashes when a message is received from an existing annotation with new location coordinates.

Swift/NativeDictionary.swift:770: Fatal error: Duplicate values for key: 'xxxxxxxx'

Before "this update" mapbox handles "the update" by itself. If the id already exists the pointAnnotationManager sets the new location of the given annotation.

This behavior was similar about a year ago. Then there was a MapBox update and the pointAnnotationManager did the updates for existing annotations itself (by id). But now it doesn't work again and you have to delete the annotation first and then add it again.

Expected behavior

The pointAnnotationManager should handle updates (location coordinate change) of an annotation by itself.

How to update multiple moving objects using MapBox? (without removing and adding them again)

Additional links and references

Similar question here

https://github.com/mapbox/mapbox-maps-ios/issues/478

persidskiy commented 6 months ago

@dlpgithub Hi, can you please make sure that the annotations actually have unique ids? This error is typically happening if you set two annotations with the same id.

dlpgithub commented 6 months ago

Yes, all point annotations have unique IDs. To explain it a little better. I receive information about an object via stream. For example Lat: 30, Lon: 9, ID: an 8 digit string. I create the point annotation with the 3 values. One minute later an update comes. The ID now has Lat: 30.1, Lon: 9.1, ID stays the same. Until recently, the SDK made the update easy. It set the "old" point annotation on the map to the new location. However, since one of the last updates this no longer works. I now always have to delete the annotation from the map first and then add it with the new values. What is the correct way to "update" a point annotation?

persidskiy commented 6 months ago

The ID now has Lat: 30.1, Lon: 9.1, ID stays the same.

How do you update the annotation? any code sample would be helpful here.

@dlpgithub The correct way is to update the manager.annotations list:

let manager = mapView.makePointAnnotationManager()

manager.anntations = /* annotations 1*/
manager.anntations = /* annotations 2*/

Or, you can update position in-place in annotations array:

manager.annotations[0].point = Point(coordinates)

but please note that annotations are value types, so if you update the copy of anntoation it won't work.

dlpgithub commented 6 months ago

manager.annotations[0].point = Point(coordinates)

Thats it!

Thank you very much. I did it by finding the index of the pointAnnotation and setting the new point for it. Thank you very very much.

Unfortunately this does not work with polyLineAnnotations. When I try to pass new coordinates with:

polyLineAnnnotationManager.annotations[0].lineCoordinates = newCircleCoordinates

it gives me an error:

Value of type 'PolylineAnnotation' has no member 'lineCoordinates'

But actually it has! :) When i initiate one i can give an id and of course some coordinates. Like this:

var myPolyLineAnnotation = PolylineAnnotation(id: 8-digit string, lineCoordinates: arrayOfCoordinates)

Is there a way to pass new coordinates to polylineAnnotations too? Kind regards

nathsociete commented 4 months ago

Hello,

The release 11.2.0 "Fixed a crash that occurs when annotations have duplicate identifiers" seems to fix this problem, isn't it ?

Can you confirm this is the same problem ?

Thanks

dlpgithub commented 3 months ago

Yes. Topic could be closed. Thanks