0

Using ESRI Leaflet I am adding markers to the map from an ArcGIS online feature service. I do this using the L.esri.query method similar to:

function slideChange(time1, time2, service) {
    L.esri.query({
        url: service
    }).where("(date_cst BETWEEN DATE '" + time1 + "' AND DATE '" + time2 + "')").run(function (error, pings) {
        trucks = L.geoJSON(pings);
        // remove previously added markers
        removeAllLayers();
        map.addLayer(trucks);
    });
}

Each time I try to call the function I need to remove previously placed markers using leaflet removeLayer() function.

    function removeAllLayers() {
        map.removeLayer(trucks)
    }

The markers are never removed and the map freezes. How can I remove this specific layer from the map using esri leaflet?

  • 1
    Make a layergroup, add the layer trucks to it, add the layergroup to the map, when you want to remove the data in the layer, use clearLayers. leafletjs.com/reference-1.3.4.html#layergroup-clearlayers then to refresh just add your layer back into the layerGroup. – Bill Chappell Dec 21 '18 at 2:16
  • 1
    @BillChappell is right. another option would be to use L.esri.featureLayer and call setWhere() when the slider is changed instead of adding and removing the layer over and over. – John Gravois Dec 21 '18 at 19:09
1

@BillChappell suggestion provided me the answer I needed. After adding the L.geojson() variable to a group I was able to clear the layers with a function. Example code...

var active = L.layerGroup();

...

query = L.esri.query({
    url: service
});

query.where("(date_cst BETWEEN DATE '" + time1 + "' AND DATE '" + time2 + "')");

query.run(function (error, pings) {
    trucks = L.geoJSON(pings, { options });
    trucks.addTo(active);
}

active.addTo(map);

...


$("#clearMap").click(function () {
    removeAllLayers();
 }

 function removeAllLayers() {
    active.clearLayers();
};
  • @wtgeograper can you mark this as the answer? – GavinR Jan 8 at 13:44

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.