Annotation labels cause regionChanged Event to fire?
I have a MapView with annotations from a database. This MapView uses the "regionChanged" event to determine when to update annotations (removeAll and then add all). The problem is that this event seems to fire to often. Selecting a pin sometimes requires the map to move to make space for the label, thus causing "regionChanged" to fire.
Have you dealt with this? How did you get around this? Perhaps I need to get a little more creative with when/how I load my annotations from the database.
2 Answers
-
I spent hours try to figure out how to work around this. In the end it was the simple answer that worked the best.
Add a variable that is visible to the entire section of code used for the MapView.
var preventRefresh = false;Then in your code add an event listener for the MapView click event and set the variable to true if the clicksource was 'pin'.
mapview.addEventListener('click',function(evt) { var clickSource = evt.clicksource; if(evt.clicksource == 'pin'){ preventRefresh = true; } });Then in your code for the regionChanged event listener check to make sure the variable is 'false' before firing the code to for the regionChanged event. (I have a function to update my annotations).
mapview.addEventListener('regionChanged', function(e){ if(preventRefresh == false){ refreshAnnotations() } else{ preventRefresh = false; } });Don't forget to set the
elseto reset your variable to false.Now the regionChanged code is ignored when the pin is clicked causing the bubble above it to make the MapView move.
I hope this prevents male-pattern-baldness in someone else. For me, it's too late.
-
For those that may run into this thread with the same issue: I did end up getting more creative with how I load my annotations.
I have some logic that loops through annotations, deletes annotations that are now off screen, adds new annotations, and leaves annotations that are still on the screen alone.