-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Marker ID might change after a map reload #3382
Comments
In the iOS SDK, MGLMapView originally mapped annotation model objects to their IDs, but we reversed it in #3261 so that IDs are mapped to the objects (along with some metadata). The primary reason is that mbgl considers the ID to be the unique identifier of an annotation, not the annotation model object (which mbgl never sees). |
As an example, I ran into this when implementing a MapView.OnInfoWindowClickListener for https://github.com/tnightingale/Mapbox/tree/geojson. Markers were being added before the DID_FINISH_LOADING_MAP event. The marker ids were then used to index data associated with each marker (in this case GeoJSON Feature 'properties'). However when the InfoWindowClickListener fired none of the marker ids matched. I figured out that I needed to wait until DID_FINISH_LOADING_MAP before creating the markers but I assume that there are still other events that could trigger a reset of all marker ids? |
I also ran into this problem recently and spent half a day to find out this issue here. My use case is like this: After seeing this issue, I added a workaround to add a OnMapchangedListener in which I added the markers. It took a while to find the root cause and really confusing to debug. I think the marker id should be fixed after creation or this behavior should be well documented in all the addMarker{s} methods. |
Doing some tests around this issue: When creating the map and adding 3 markers: Adding marker with id : 0
Adding marker with id : 1
Adding marker with id : 2 After an orientation change: Restoring marker with id : 0
Restoring marker with id : 1
Restoring marker with id : 2 Note that the we used to be able to add markers before |
Retested the use case from @tnightingale and this one is resolved for the same reason as above: When creating the map and adding 3 markers: Adding marker with id : 0
Adding marker with id : 1
Adding marker with id : 2 Clicking them in the order as they were added: Click on marker with id : 0
Click on marker with id : 1
Click on marker with id : 2 |
@jiajiahan with the upcoming release for |
The initial issue is resolved and nothing actionable left to do here. Closing |
Yay! |
@jiajiahan Indeed, Marker id are still changing whenever I move the map so I can't rely on it to map my data to it. @tobrun The links for your exemples are dead, can you please update them. Thanks |
@tobrun Thanks, that's indeed the same issue. By the way, I found a workaround by using the Marker's LngLat ( |
The default
OnMapChangedListener
reloads the markers after aDID_FINISH_LOADING_MAP
change event. This reload can produce a change of IDs in the markers because they're effectively removed and re-added:We shouldn't do this because the
hashCode
of a marker depends on itsid
, and this change ofid
can bring unintended consequences. For example, markers won't work well as keys in aHashMap
. If this is necessary, this must be documented, or a differenthashCode
method unaffected byid
changes could be implemented.Possibly related: #1488 which was fixed in #3049.
The text was updated successfully, but these errors were encountered: