Openlayers 2 BBOX策略迁移到Openlayers 3



我正在将地图应用程序从Openlayers 2迁移到ol3,并且有一个bbox层,当范围更改时向服务器发出请求。我使用刷新策略(强制:true),服务器返回我使用自定义格式处理的对象数组。

var refreshStrategy = new OpenLayers.Strategy.Refresh({
  force: true
});
OpenLayers.Format.GTFS = OpenLayers.Class(OpenLayers.Format, {
  read: function(body) {
    var stops = JSON.parse(body), point, features = [];
    for(var i=0,l=stops.length; i<l; i++) {
      point = new OpenLayers.Geometry.Point(stops[i].stop_lon, stops[i].stop_lat);
      features.push(new OpenLayers.Feature.Vector(point, stops[i]));
    }
    return features;
    }
});
var layer = new OpenLayers.Layer.Vector('Stops', {
  projection: new OpenLayers.Projection('EPSG:4326'),
  visibility: true,
  strategies: [
    new OpenLayers.Strategy.BBOX({resFactor: 1.2}),
    refreshStrategy
    ],
  protocol: new OpenLayers.Protocol.HTTP({
    format: new OpenLayers.Format.GTFS(),
    url: '/api/v1/stops.json'
  })
});
refreshStrategy.activate();

看来ol.source.Vector只支持一种策略。我试着只使用bbox策略,但每次平移

时,特征标记闪烁,数据重新加载。
var stopFeatures = new ol.Collection();
var source = new ol.source.Vector({
    features: stopFeatures,
    loader: function(extent, resolution, projection) {
        extent = ol.extent.applyTransform(extent, ol.proj.getTransform("EPSG:3857", "EPSG:4326"));
        var url = '/api/stops.json?bbox=' + extent.join(',');
        $http.get(url).then(function (res) {
            var features = _.map(res.data, function (stop) {
                var stopFeature = new ol.Feature(stop);
                stopFeature.setGeometry(new ol.geom.Point(ol.proj.transform([stop.stop_lon,stop.stop_lat],
                    'EPSG:4326', 'EPSG:3857')));
                return stopFeature;
            });
            stopFeatures.clear();
            stopFeatures.extend(features);
        });
    },
    projection: 'EPSG:4326',
    strategy: ol.loadingstrategy.bbox,
});

清理和重置功能集合感觉我做错了什么,刷新似乎更慢。

map.on('moveend',...是在ol3上实现这一点的方法吗?

您是对的-您不应该在特性集合上调用clear()extend()。相反,应该为JSON中的每个特性设置一个惟一的id。如果没有,可以使用从纬度和经度创建的散列。有了id后,使用stopFeature.setId(id)在特性上设置id。然后简单地调用source.addFeatures(features)。该策略将在内部比较特性id与现有特性的id,并且只插入那些id不在源文件中的特性。

最新更新