我正在将一个地图应用程序从Openlayers2迁移到ol3,并且有一个bbox层,当范围发生变化时,它会向服务器发出一个请求。我使用刷新策略(使用force: 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,
});功能集合的清除和重置感觉像是我做错了什么,刷新速度似乎变慢了。
在ol3上实现这一点,map.on('moveend',...是可行的吗?
发布于 2017-02-13 17:38:49
您是对的-您不应该在特性集合上调用clear()和extend()。相反,JSON中的每个特性都应该有一个惟一的id。如果没有,可以使用从纬度和经度创建的散列。一旦有了id,就可以使用stopFeature.setId(id)在特性上设置id。然后只需调用source.addFeatures(features)。该策略将在内部将特征in与现有特征的in进行比较,并且只插入那些in不在源中的特征。
https://stackoverflow.com/questions/40324406
复制相似问题