Search code examples
clickmapsopenlayersdrawgeoserver

Openlayers how to handle map onclick and draw at the same time problem


I'm using tileWMS to get my layers from geoserver.
So when I click the map, I get the feature info if click on the layer, but then if I draw let's just say polygon, if the point is on the layer, the click trigger on map will run as well. I've tried interaction select with vector but doesn't work with tileWMS. I can remove the draw interaction after draw but how can I handle so it won't get the feature info while drawing? is it possible to do something like addinteraction and removeinteraction but with tileWMS? I'm using openlayers 3 recently.

https://i.ibb.co/5vZwPYx/2.png

Thanks.

// this is my layer & map
var layer = new ol.layer.Tile(
{
    source      : new ol.source.TileWMS(
    {
        url: 'http://localhost:2121/geoserver/geoserver/wms',
        params: {'LAYERS': 'geoserver:coordinate_polygon', 'TILED': true},
        serverType: 'geoserver'
    })    
});

var map = new ol.Map(
{    
    pixelRatio: 1,
    controls: ol.control.defaults().extend([zoomslider, mousePositionControl]),
    loadTilesWhileInteracting: true,
    layers: [osm, layer, layer_draw_vector],
    target: 'map',
    view: view
});
// ----------------------------

//the trigger
map.on('singleclick', function(evt)
{
    //doin request to geoserver let's say I alert the response
    alert(feature[0].properties.name;
}

map.addInteraction(interaction_draw);
interaction_draw.on('drawend',function(e)
{
    alert(e.feature.getGeometry().getExtent());
}); 
// ----------------------------

Solution

  • If you use interaction_draw.setActive(true); and interaction_draw.setActive(false); to turn on and off the interaction you could check if the interaction is active before proceeding with any map click actions.

    map.on('singleclick', function(evt)
    {
        if (interaction_draw.getActive()) return;
    
        ...
        ...
        ...
    
    });