TransWikia.com

Select Features in qgis2web - openlayers

Geographic Information Systems Asked by AdriBu on August 9, 2021

After using qgis2web to export a map, I am looking to modify the qgis2web.js file to have the possibility to select (highlight) features on click and keep them selected until I click somewhere else (example [here]) and to display the pop up info under the same circumstances. I am new to coding so any help is most welcomed.

    map.on('pointermove', function(evt) {
    onPointerMove(evt);
});
map.on('singleclick', function(evt) {
    onSingleClick(evt);
});

if I add if front of this

`map.on('click', function (evt) {
        var pixel = map.getEventPixel(evt.originalEvent);
        displayFeatureInfo(evt.pixel);

    })
map.on('pointermove', function(evt) {
    if (evt.dragging) {
      return;   
    }

    var pixel = map.getEventPixel(evt.originalEvent);
    displayFeatureInfo(pixel);
  });`

It allows me to do that however the feature does not remain highlighted when dragging the map.

One Answer

Add another (empty) vector layer to your map with your desired highlight style. As you get / select the feature from the first layer, add this feature to the second layer. When you're done with it, just clear the layer. So you should go with something along the lines of:

//assume layer2 is the other vector layer
map.on("singleclick", function (data) {
    map.forEachFeatureAtPixel(data.pixel, function (ftr) {
        layer2.getSource().addFeature(ftr);
    });
});

//when you're done with this, clear the second vector layer
layer2.getSource().clear();

Answered by Ulas on August 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP