繁体   English   中英

Open Layers 3是否基于矢量层的范围将地图居中?

[英]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(...)您已经在centeringzooming 之后您是否想知道中心并放大?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM