[英]Open Layers 3 center the map based on extent on vector layer?
我想根據矢量圖層要素(點)定位地圖的中心和縮放級別。
我有一個geojson文件正在填充我的地圖:
var vectorSource = new ol.source.Vector({
url: 'assets/js/data.geojson',
format: new ol.format.GeoJSON(),
projection: 'ESPG:3857'
});
我現在嘗試獲取矢量層的范圍:
vectorSource.once('change', function (e) {
if (vectorSource.getState() === 'ready') {
if (vectorLayer.getSource().getFeatures().length > 0) {
map.getView().fit(vectorSource.getExtent(), map.getSize());
}
}
});
如何獲取基於圖層要素的地圖坐標並將其縮放到特定級別?
map.setCenter("// Center position of lon,lat based on layer features//");
map.setZoom("// Zoom level according to layer features//");
當您使用map.getView().fit(...)
您已經在centering
和zooming
。 之后您是否想知道中心並放大?
vectorSource.once('change', function(evt){
if (vectorSource.getState() === 'ready') {
// now the source is fully loaded
if (vectorLayer.getSource().getFeatures().length > 0) {
map.getView().fit(vectorSource.getExtent(), map.getSize());
console.info(map.getView().getCenter());
console.info(map.getView().getZoom());
}
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.