Forum Discussion
How to remove highlight on previously selected feature in geojson data on Azure Map polygon layer
JadedBelle Just seeing this now, note that the main technical forums for Azure Maps are on Microsoft Q&A here: https://learn.microsoft.com/en-us/answers/tags/209/azure-maps
As for your issue, there are a couple of ways to resolve it. The main issue is that you are creating and adding errors from within the event handler of a click event. This means that each time you click on a polygon, another layer is created.
Option 1:
- Currently your "polygonClickedLayer" and "selectedLineLayer" variables are inside the event handler and not accessible outside of that handler. Make them global variables, and later pass those layers into the map.layers.remove function to clear the highlight state. This is a simple solution but you could still run into issues if you click a second time before removing the layers.
Option 2:
- Create your layers once outside of the event handler and only set the filter using setOptions on the layers within the event handler. This is the most common approach used and will have less issues and better performance than Option 1. There is a code sample that does this here: https://samples.azuremaps.com/?sample=polygon-hover-style