不重绘现有特征的 BBox 策略
BBox strategy without redrawing existing features
似乎 ol.loadingstrategy.bbox
所有现有功能在平移或缩放时都会重新绘制。我注意到它是因为半透明的默认多边形填充颜色在每次平移或缩放操作后变得越来越不透明。如何实现只添加新功能到图层而不重绘现有功能?
这是我的相关代码:
var geojsonFormat = new ol.format.GeoJSON();
var vectorSource = new ol.source.Vector({
loader: function(extent, resolution, projection) {
var epsg4326Extent = ol.proj.transformExtent(extent, projection, 'EPSG:4326');
var url = 'geojson.php?bbox=' + epsg4326Extent.join(',');
$.ajax({
url: url,
success: function(data) {
var features = geojsonFormat.readFeatures(data, { dataProjection:'EPSG:4326', featureProjection:'EPSG:3857' });
vectorSource.addFeatures(features);
}
});
},
strategy: ol.loadingstrategy.bbox
});
编辑:
如果 GeoJSON 看起来像这样(请参阅 的回复),它会起作用:
{
"type": "FeatureCollection",
"features":
[
{
"type": "Feature",
"id": 1,
"geometry": {
"type": "Point",
"coordinates": [125.6, 10.1]
},
"properties": {
"name": "Dinagat Islands"
}
},
{
"type": "Feature",
"id": 2,
"geometry": {
"type": "Point",
"coordinates": [126.05, 9.85]
},
"properties": {
"name": "Siargao"
}
}
]
}
为了使 bbox 策略正常工作,您的服务器返回的 GeoJSON 必须将服务器唯一 ID 设置为每个 'Feature' 对象的 'id'。没有这个,OpenLayers 将不知道已经加载了一个功能。
似乎 ol.loadingstrategy.bbox
所有现有功能在平移或缩放时都会重新绘制。我注意到它是因为半透明的默认多边形填充颜色在每次平移或缩放操作后变得越来越不透明。如何实现只添加新功能到图层而不重绘现有功能?
这是我的相关代码:
var geojsonFormat = new ol.format.GeoJSON();
var vectorSource = new ol.source.Vector({
loader: function(extent, resolution, projection) {
var epsg4326Extent = ol.proj.transformExtent(extent, projection, 'EPSG:4326');
var url = 'geojson.php?bbox=' + epsg4326Extent.join(',');
$.ajax({
url: url,
success: function(data) {
var features = geojsonFormat.readFeatures(data, { dataProjection:'EPSG:4326', featureProjection:'EPSG:3857' });
vectorSource.addFeatures(features);
}
});
},
strategy: ol.loadingstrategy.bbox
});
编辑:
如果 GeoJSON 看起来像这样(请参阅
{
"type": "FeatureCollection",
"features":
[
{
"type": "Feature",
"id": 1,
"geometry": {
"type": "Point",
"coordinates": [125.6, 10.1]
},
"properties": {
"name": "Dinagat Islands"
}
},
{
"type": "Feature",
"id": 2,
"geometry": {
"type": "Point",
"coordinates": [126.05, 9.85]
},
"properties": {
"name": "Siargao"
}
}
]
}
为了使 bbox 策略正常工作,您的服务器返回的 GeoJSON 必须将服务器唯一 ID 设置为每个 'Feature' 对象的 'id'。没有这个,OpenLayers 将不知道已经加载了一个功能。