首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将Openlayers 2 BBOX策略迁移到Openlayers 3

将Openlayers 2 BBOX策略迁移到Openlayers 3
EN

Stack Overflow用户
提问于 2016-10-30 06:44:05
回答 1查看 417关注 0票数 1

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

代码语言:javascript
复制
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策略,但每次平移时,特征标记都会闪烁,数据也会重新加载

代码语言:javascript
复制
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',...是可行的吗?

EN

回答 1

Stack Overflow用户

发布于 2017-02-13 17:38:49

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40324406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档