2

I am trying to make a selection from a search result. I managed to make the selection on the map using the code below.

Now my problem is that when I select one or more buildings with the same style, it does not automatically zoom in on the respective lands. I would also like to zoom in on the selected land.

My code:

var extent = ol.extent.createEmpty ();

var selected_stile = new ol.style.Style({
    fill: new ol.style.Fill({color: makePattern()}),
    stroke: new ol.style.Stroke({
      color: [102,255,51], width: 3
    }),
  text: new ol.style.Text({
    font: 'bold 11px "Open Sans", "Arial Unicode MS", "sans-serif"',
    placement: 'center',
    fill: new ol.style.Fill({
      color: 'white', width: 23
    })
  })
});

imobile.getSource().forEachFeature(function(feature){
  feature.setId(feature.get('text'));
  
  var array = id_teren.split(",");
  for (i=0;i<array.length;i++){
    var data = "";
    if($('#etc_tarla').is(":checked")){ data = "T:"+feature.get('tarla')+"  "; }
    if($('#etc_parcela').is(":checked")){ data = data+"P:"+feature.get('parcela')+"  "; }
    if($('#etc_suprafata').is(":checked")){ data = data+"S:"+feature.get('suprafata')+"MP.  "; }
    if($('#etc_proprietari').is(":checked")){ data = data+"P:"+feature.get('proprietari')+"  "; }
    if($('#etc_eterra').is(":checked")){ data = data+"C.F.:"+feature.get('e_terra')+"  "; }

     if(feature.get('text') == array[i]){
        console.log('GASITTTTTTTTTTTTTTTTT');
        var feature = imobile.getSource().getFeatureById(array[i]);
        
        selected_stile.getText().setText(data);
        feature.setStyle(selected_stile);
        ol.extent.extend(extent, feature.getGeometry().getExtent());
        
     }
  }   
});
7
  • 2
    You are extending extent, but I don't see it initially defined anywhere, neither it's being used to zoom to this extent.
    – TomazicM
    Commented Jan 24, 2021 at 10:24
  • Here is stated a little above the above code: var extent = ol.extent.createEmpty (); Commented Jan 24, 2021 at 19:01
  • 1
    Please edit your question with the code from comment. And then, where is this extent used to set view zoom?
    – TomazicM
    Commented Jan 24, 2021 at 19:36
  • Yes for the zoom view. Commented Jan 24, 2021 at 19:46
  • What do you mean by "Yes for the zoom view."? I don't see any code setting the view zoom.
    – TomazicM
    Commented Jan 24, 2021 at 19:54

1 Answer 1

1

To zoom map to the desired extent, you can use map view .fit method (see https://openlayers.org/en/latest/apidoc/module-ol_View-View.html#fit).

If your map object is map, then this would look like this:

map.getView().fit(extent, {size: map.getSize()});

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.