Open Layers 3是否基于矢量层的范围将地图居中? [英] Open Layers 3 center the map based on extent on vector layer?

查看:76
本文介绍了Open Layers 3是否基于矢量层的范围将地图居中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据矢量图层要素(点)来定位地图的中心和缩放级别.

I want to position the center and zoom level of my map depending on the vector layer features (points).

我有一个geojson文件正在填充我的地图:

I have a geojson file which is populating my map:

var vectorSource = new ol.source.Vector({
   url: 'assets/js/data.geojson',
   format: new ol.format.GeoJSON(),
   projection: 'ESPG:3857'
});

我现在尝试获取矢量层的范围:

I now try to get the extent of the vector layer:

vectorSource.once('change', function (e) {
    if (vectorSource.getState() === 'ready') {
        if (vectorLayer.getSource().getFeatures().length > 0) {
            map.getView().fit(vectorSource.getExtent(), map.getSize());
        }
    }
});

如何获取基于图层要素的地图坐标并将其缩放到特定级别?

How can I get the coordinates of the map based on the layer features and zoom to specific level?

map.setCenter("// Center position of lon,lat based on layer features//");
map.setZoom("// Zoom level according to layer features//");

推荐答案

使用map.getView().fit(...)时,您已经是centeringzooming.您要在此之后知道中心并缩放吗?

When you use map.getView().fit(...) you're already centering and zooming. Do you want to know the center and zoom after this?

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

这篇关于Open Layers 3是否基于矢量层的范围将地图居中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆