OpenLayers 更改图层源 URL(或替换从另一个 URL 加载的功能)
OpenLayers3 change Layer source URL (or replace features loaded from another URL)
我有一张带有矢量层的地图,包含来自 GeoJSON 源的特征:
var map = new ol.Map({
layers: [new ol.layer.Tile({
source: new ol.source.OSM()
}),
new ol.layer.Vector({
source: new ol.source.Vector({
url: 'http://example.com:5000/geo/data/zones/1',
format: new ol.format.GeoJSON()
}),
})],
renderer: 'canvas',
target: 'map',
view: new ol.View({
center: [737514.438475665, 5864533.629390752],
zoom: 13
})
});
我有多个 URL 是 returns GeoJSON 字符串:
http://example.com:5000/geo/data/zones/1
http://example.com:5000/geo/data/zones/2
http://example.com:5000/geo/data/zones/n
我需要能够切换图层源的 URL(或从另一个 URL 获取和显示功能)。
我试图在 ol.Layer.Vector 实例上找到 'url' 属性:
l=m.getLayers().getArray()[1]
l.getProperties()
在 ol.source.Vector 实例上:
s = l.getSource()
s.getProperties()
但我还没有找到关于 'url' 属性 的任何信息。
你能提供一种方法吗?
- 是否可以简单地更新源 URL(并自动刷新图层功能)?
- 我应该删除现有功能,使用我自己的逻辑加载新功能并添加已加载的功能吗?
- 我要删除整个层,重新创建它,然后重新添加它吗?
- is it possible to simply update the source URL (and automagically refresh the layer features) ?
您可以使用 ol.layer.Layer.setSource 创建新源并将其设置在目标图层上。
s=new ol.source.Vector(/* your source config goes here */);
l=m.getLayers().getArray()[1];
l.setSource(s);
如果两个图层都可见,地图将自动刷新要素。
- shall I remove existing features, load new features using my own logic and add the loaded ones ?
您可以使用以下方法在矢量图层上添加或删除特征:
另请参阅:ol.Feature and ol.layer.Vector.forEachFeature
var feature = new ol.Feature({
geometry: new ol.geom.Polygon(polyCoords),
labelPoint: new ol.geom.Point(labelCoords),
name: 'My Polygon'
});
l=m.getLayers().getArray()[1];
s=l.getSource();
s.addFeature(feature);
s.addFeatures(/* array of features */);
s.removeFeature(feature);
- shall I remove the whole Layer, re-create it, and re-add it ?
您可以使用 ol.Map.addLayer and ol.Map.removeLayer 来做到这一点。
// m is your map variable
v = new ol.layer.Vector(cfg);
m.addLayer(v);
m.removeLayer(v);
最终答案
上面列出的所有选项都会切换您图层的URL。每个选项都会触发自己的一组事件,并使用不同的参数和对象。根据我个人的理解,我建议:
如果要加载与旧图层具有相同属性但包含新数据的新图层,请使用选项 1。
如果您对矢量图层上的某些特征的更改很少,请使用选项 2。
如果您有一个全新的层,其属性与之前的层不同,请使用选项 3。
我有一张带有矢量层的地图,包含来自 GeoJSON 源的特征:
var map = new ol.Map({
layers: [new ol.layer.Tile({
source: new ol.source.OSM()
}),
new ol.layer.Vector({
source: new ol.source.Vector({
url: 'http://example.com:5000/geo/data/zones/1',
format: new ol.format.GeoJSON()
}),
})],
renderer: 'canvas',
target: 'map',
view: new ol.View({
center: [737514.438475665, 5864533.629390752],
zoom: 13
})
});
我有多个 URL 是 returns GeoJSON 字符串:
http://example.com:5000/geo/data/zones/1
http://example.com:5000/geo/data/zones/2
http://example.com:5000/geo/data/zones/n
我需要能够切换图层源的 URL(或从另一个 URL 获取和显示功能)。
我试图在 ol.Layer.Vector 实例上找到 'url' 属性:
l=m.getLayers().getArray()[1]
l.getProperties()
在 ol.source.Vector 实例上:
s = l.getSource()
s.getProperties()
但我还没有找到关于 'url' 属性 的任何信息。
你能提供一种方法吗?
- 是否可以简单地更新源 URL(并自动刷新图层功能)?
- 我应该删除现有功能,使用我自己的逻辑加载新功能并添加已加载的功能吗?
- 我要删除整个层,重新创建它,然后重新添加它吗?
- is it possible to simply update the source URL (and automagically refresh the layer features) ?
您可以使用 ol.layer.Layer.setSource 创建新源并将其设置在目标图层上。
s=new ol.source.Vector(/* your source config goes here */);
l=m.getLayers().getArray()[1];
l.setSource(s);
如果两个图层都可见,地图将自动刷新要素。
- shall I remove existing features, load new features using my own logic and add the loaded ones ?
您可以使用以下方法在矢量图层上添加或删除特征:
另请参阅:ol.Feature and ol.layer.Vector.forEachFeature
var feature = new ol.Feature({
geometry: new ol.geom.Polygon(polyCoords),
labelPoint: new ol.geom.Point(labelCoords),
name: 'My Polygon'
});
l=m.getLayers().getArray()[1];
s=l.getSource();
s.addFeature(feature);
s.addFeatures(/* array of features */);
s.removeFeature(feature);
- shall I remove the whole Layer, re-create it, and re-add it ?
您可以使用 ol.Map.addLayer and ol.Map.removeLayer 来做到这一点。
// m is your map variable
v = new ol.layer.Vector(cfg);
m.addLayer(v);
m.removeLayer(v);
最终答案
上面列出的所有选项都会切换您图层的URL。每个选项都会触发自己的一组事件,并使用不同的参数和对象。根据我个人的理解,我建议:
如果要加载与旧图层具有相同属性但包含新数据的新图层,请使用选项 1。
如果您对矢量图层上的某些特征的更改很少,请使用选项 2。
如果您有一个全新的层,其属性与之前的层不同,请使用选项 3。