Nic*_*s T 2 javascript maps openlayers-3
我想根据矢量图层要素(点)定位地图的中心和缩放级别.
我有一个填充我的地图的geojson文件:
var vectorSource = new ol.source.Vector({
url: 'assets/js/data.geojson',
format: new ol.format.GeoJSON(),
projection: 'ESPG:3857'
});
Run Code Online (Sandbox Code Playgroud)
我现在尝试获取矢量图层的范围:
vectorSource.once('change', function (e) {
if (vectorSource.getState() === 'ready') {
if (vectorLayer.getSource().getFeatures().length > 0) {
map.getView().fit(vectorSource.getExtent(), map.getSize());
}
}
});
Run Code Online (Sandbox Code Playgroud)
如何根据图层要素获取地图坐标并缩放到特定级别?
map.setCenter("// Center position of lon,lat based on layer features//");
map.setZoom("// Zoom level according to layer features//");
Run Code Online (Sandbox Code Playgroud)
当你使用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());
}
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6005 次 |
| 最近记录: |