0

Previously we did when you click on a geometry on the map to color in another color and zoom on geometry. But now you should deselect the previous selection in the original color in which the map was loaded and selected recently selected object.

 map.on('click', function(e){
         console.log(e);
        //coordinate = evt.coordinate;

        var feature = map.forEachFeatureAtPixel(e.pixel, function(feature) { return feature; });

        var coordinate = e.coordinate;
        var hdms = ol.coordinate.toStringHDMS(ol.proj.transform(coordinate, 'EPSG:31700', 'EPSG:3857'));

        _myStroke = new ol.style.Stroke({
           color : 'yellow',
           width : 4    
        });

        _myFill = new ol.style.Fill({
           color: 'rgba(255,0,0,1.0)'
        });

        selected_polygon_style = new ol.style.Style({
           stroke : _myStroke,
           fill : _myFill
         });


       feature.setStyle (selected_polygon_style);
       map.getView().fit(feature.getGeometry(), { duration: 2000 }, {"maxZoom":11} );

    })
2

If you keep a selectedFeature variable you can reset when a new one is selected

var selectedFeature;

map.on('click', function(e){
         console.log(e);
        //coordinate = evt.coordinate;

        var feature = map.forEachFeatureAtPixel(e.pixel, function(feature) { return feature; });

        var coordinate = e.coordinate;
        var hdms = ol.coordinate.toStringHDMS(ol.proj.transform(coordinate, 'EPSG:31700', 'EPSG:3857'));

        _myStroke = new ol.style.Stroke({
           color : 'yellow',
           width : 4    
        });

        _myFill = new ol.style.Fill({
           color: 'rgba(255,0,0,1.0)'
        });

        selected_polygon_style = new ol.style.Style({
           stroke : _myStroke,
           fill : _myFill
         });

       feature.setStyle (selected_polygon_style);
       map.getView().fit(feature.getGeometry(), { duration: 2000 }, {"maxZoom":11} );

       // reset any previous selected feature and remember this one
       if (selectedFeature) {
           selectedFeature.setStyle(deselected_polygon_style);
       }
       selectedFeature = feature;

    })

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.