ZoomToExtent OpenLayers 3

前端 未结 3 490
不知归路
不知归路 2021-01-01 14:28

OpenLayers 2 had a very useful map.zoomToExtent(extent) feature. Is there something similar in OpenLayers 3? I can get the extent of interest with source

相关标签:
3条回答
  • 2021-01-01 14:32

    With OpenLayers 4.x this is still a valid solution:

    map.getView().fit(source.getExtent(), map.getSize()); 
    

    Make sure to set the optional second parameter to prevent console errors, if there are no points on the map.

    0 讨论(0)
  • 2021-01-01 14:33

    With OpenLayers 4.x, I found the following methods useful:

    map.getView().setCenter([x, y]); map.getView().setZoom(z);

    where x, y, z are the coordinates where you want to zoom to.

    0 讨论(0)
  • 2021-01-01 14:42

    Going off the function sfletche linked to:

    var extent = source.getExtent();
    map.getView().fitExtent(extent, map.getSize());
    

    EDIT July 23, 2013

    Apparently fitExtent is deprecated. Should be ol.View.fit, so something linke this (untestesd):

    var extent = source.getExtent();
    map.getView().fit(extent, map.getSize()); 
    
    0 讨论(0)
提交回复
热议问题