3

Is it possible to reset or clear the map and plot new points? I have a search box that am using to query a geojson API.

After the first mapped result, the second search is plotted in addition which I do not want. What I want to achieve is, if I search, that result should be plotted not all subsequent searches I make.

Is there a way, to overwrite that first plotted result or remove it before plotting a new search result?

button.addEventListener('click', function(e) {
    inputVal = input.value;
    // console.log(inputVal);
    getPointLayer = new ol.layer.Vector({
        source: new ol.source.Vector({
            url: 'http://0.0.0.0:5000/api/v1/dumela/' + inputVal,
            format: new ol.format.GeoJSON()
        }),
        style: styleFunction
    });

    var layersOnMap = map.getLayers();
    // reset the map, clear all point
    if (layersOnMap) {
        map.removeLayer(getPointLayer);
    }

    console.log(layersOnMap.length);
    map.addLayer(getPointLayer);
});

3 Answers 3

1

The issue is that there isn't a command to clear and redraw those points without keeping all subsequent points from your input. You can:

try calling vectorLayer.redraw(); afterward

3
  • redraw is not a function(), thats the error i get.. i did vectorLayer.redraw() and map.redraw() and all return this error. I would appreciate help link to the api documentation..
    – saviour123
    Commented Aug 24, 2017 at 15:16
  • @saviour123 which version of OpenLayers are you using?
    – whyzar
    Commented Aug 24, 2017 at 16:28
  • v4, the latest version.
    – saviour123
    Commented Aug 24, 2017 at 18:29
0

I don't think removeLayer() clears the existing data. You should also be clearing the vector source when the function initializes. And if you do this I don't think removing layer is necessary unless you don't need it any more. If that doesn't work try adding the removeLayer() before the getPointLayer is defined.

Try adding source.clear() in the function as shown below. Here's the api doc for this.

button.addEventListener('click', function(e) {
    var layersOnMap = map.getLayers();
    // reset the map, clear all point
    if (layersOnMap) {
        map.removeLayer(getPointLayer);
    }
    //Clearing the source
    if(pointSource){
        pointSource.clear();
    }
    inputVal = input.value;
    // console.log(inputVal);
    pointSource = new ol.source.Vector({
        url: 'http://0.0.0.0:5000/api/v1/dumela/' + inputVal,
        format: new ol.format.GeoJSON()
    });

    getPointLayer = new ol.layer.Vector({
        source: pointSource,
        style: styleFunction
    });

    map.addLayer(getPointLayer);
});
1
  • It didn't work. But I found a solution now. check below
    – saviour123
    Commented Aug 31, 2017 at 17:12
0

This is the little hack i did. I destroy the previous plot by new ol.layer.Group and then i called map.addLayer(hgtmap) again.

....

if (pointSource.getState() === "ready") {
    map.setLayerGroup(new ol.layer.Group());
    map.addLayer(hgtmap);
 }

 ....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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