【发布时间】:2016-10-29 22:44:04
【问题描述】:
我正在将地图应用程序从 Openlayers 2 迁移到 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,
});
功能集合的清除和重置感觉就像我做错了什么,刷新似乎变慢了。
map.on('moveend',... 是在 ol3 上实现这个的方法吗?
【问题讨论】:
-
实际上 OpenLayers 3.x 中的
ol.loadingstrategy.bbox与 OpenLayers 2 中的OpenLayers.Strategy.BBOX完全相同。是什么让你认为它是一个“不同的怪物”? -
@ahocevar 我阅读了代码并理解您的意思。对我来说模糊的是 ol2 如何在后台合并来自后续请求的数据,现在我不得不使用功能集合。我正在清除可能会重新插入的功能,我认为这可能会导致它变慢。无论如何感谢您的关注,我已经添加了更多详细信息。