How to clear previous layers, point and data in Azure Map

Steve Lau 211 Reputation points
2020-10-05T22:23:11.847+00:00

Hi All,

I'm new to Azure Maps. I managed to create the map based on the given date range with (long & lat). The first query brings back say 50 rows and the data are correct. On the second query (short date range and expecting 2 rows), but the map is still showing points and other layers from the first query. How could I fix the problem?
I tried the following in the javascript with no luck

if (null != clusterBubbleLayer) {
        map.layers.remove(clusterBubbleLayer);
    }
    if (null != clusterLayer) {
        map.layers.remove(clusterLayer);
    }
    if (null != timeTextLayer) {
        map.layers.remove(timeTextLayer);
    }

    if (dataSource != undefined && dataSource.shapes.length > 0) {
        dataSource.shapes = [];
    }

any help is much appreciated.

TIA,
Steve

P.S. I'm using ASP.NET c# with javascript front end.

Azure Maps
Azure Maps
An Azure service that provides geospatial APIs to add maps, spatial analytics, and mobility solutions to apps.
666 questions
0 comments No comments
{count} vote

Accepted answer
  1. Steve Lau 211 Reputation points
    2020-10-14T20:42:46.817+00:00

    Hello rbrundritt,

    I went thru my code again and I was clearing the dataSource in the wrong location.
    In my code, I was trying to clear the dataSource in the drawfunction() and I moved it to prior fetching of the data, and it works.

    Again, thanks for your help.

    Steve

    3 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. rbrundritt 16,551 Reputation points Microsoft Employee
    2020-10-06T01:17:12.407+00:00

    All data is managed by a Source. Your app is most likely using the DataSource class. You can add data to a data source using the add function on this class. Clear all data using the clear function. Overwrite all data (clear+add) all at once using the setShapes function. This last one triggers a single re-render on the map, while calling clear then add would trigger two re-renders.

    Rendering layers connect to these, but once connected and configured, should rarely need to be changed.

    If you haven't come across it yet, tons of great examples here: https://azuremapscodesamples.azurewebsites.net/

    1 person found this answer helpful.