Search code examples
zoomingopenlayerspan

how to zoom/pan automatically to all features in a vector layer?


Using Openlayers, I would like to initialize the map in a position where it shows all the features in a vector layer. How to do that?


Solution

  • This is a little sharp/rough

    map.zoomToExtent(yourVectorLayer.getDataExtent());
    

    This may be smoother

    map.panTo(yourVectorLayer.getDataExtent().getCenterLonLat());
    map.zoomTo(yourVectorLayer.getDataExtent().getZoomExtent());