BBox策略没有重新绘制现有功能

时间:2015-07-06 13:43:03

标签: openlayers-3

似乎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看起来像这样(见ahocevar的回复):

{
 "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"
      }
     }    
   ]
}

1 个答案:

答案 0 :(得分:0)

要使bbox策略正常运行,服务器返回的GeoJSON必须将服务器唯一ID设置为' id'每个'功能'宾语。如果没有这个,OpenLayers将不知道已经加载了一个功能。