ZoomToExtent OpenLayers 3

流过昼夜 提交于 2019-12-30 03:44:09

问题


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.getExtent(), but I can't figure out how to apply that extent as a "zoom level".


回答1:


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()); 



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/23682286/zoomtoextent-openlayers-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!