2015-12-02 41 views
2

我想根据矢量图层的特征(点)来定位我的地图的中心和缩放级别。打开图层3根据矢量图层的范围将地图居中?

我有的填充我的地图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//"); 

回答

5

当您使用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()); 
    } 
    } 
}); 
+0

我没有意识到!我认为我仍然需要应用setCenter。 现在已解决!谢谢! –

+1

看看[fit](http://openlayers.org/en/master/apidoc/ol.View.html#fit)方法,有一些选项。别客气。 –

相关问题