【问题标题】:OpenLayers 3: Forcing a vector layer to reload GeoJSON sourceOpenLayers 3:强制矢量图层重新加载 GeoJSON 源
【发布时间】:2016-06-20 11:45:54
【问题描述】:

我正在尝试强制 OpenLayers 3 中的矢量图层定期从 GeoJSON 源重新加载其数据。 GeoJSON 源不时更改。

在使用不同方法进行了多次不同尝试后,我认为这是迄今为止我设法得到的最接近的结果:

$(document).ready(function(){
  map.once("postcompose", function(){
       //start refreshing each 3 seconds
       window.setInterval(function(){
         var source = testLayer.getSource();
         var params = source.getParams();
         params.t = new Date().getMilliseconds();
         source.updateParams(params);
       }, 3000);
   });
});

但是,这给了我以下信息(当然每 3 秒一次):

Uncaught TypeError: source.getParams is not a function

这是我用来加载和显示 GeoJSON 文件的所有其他代码。地图中其他图层的加载方式相同:

var testSource = new ol.source.Vector({
    url: '/js/geojson/testfile.geojson',
    format: new ol.format.GeoJSON()
});

...

window.testLayer = new ol.layer.Vector({
    source: testSource,
    style: function(feature, resolution) {
        var style = new ol.style.Style({
            fill: new ol.style.Fill({color: '#ffffff'}),
            stroke: new ol.style.Stroke({color: 'black', width: 1}),
            text: new ol.style.Text({
                font: '12px Verdana',
                text: feature.get('testvalue'),
                fill: new ol.style.Fill({
                    color: 'rgba(255, 255, 255, 0.1)'
                }),
                stroke: new ol.style.Stroke({
                    color: 'rgba(0, 0, 0, 1)',
                    width: 1
                })
            })
        });

        return style
    }
});

...

var olMapDiv = document.getElementById('olmap');
var map = new ol.Map({
    layers: [paddocksLayer,zonesLayer,structuresLayer,roadsLayer,testLayer],
    interactions: ol.interaction.defaults({
        altShiftDragRotate: false,
        dragPan: false,
        rotate: false
    }).extend([new ol.interaction.DragPan({kinetic: null})]),
    target: olMapDiv,
    view: view
});
view.setZoom(1);
view.setCenter([0,0]);

我在其他地方读到 getParams 根本不适用于矢量图层,所以这个错误并非完全出乎意料。

是否有另一种方法可以重新加载(不仅仅是重新渲染)矢量图层?

提前致谢。我还必须提前道歉,因为我需要非常具体的指导 - 我对 JS 和 OpenLayers 非常陌生。

加雷斯

【问题讨论】:

  • 您必须展示如何加载 (AJAX) json 文件。
  • 查看我上面的编辑 - 我在初始化地图时没有使用 AJAX 加载 GeoJSON。我应该这样做吗?该层使用上面的附加代码正确显示 - 只是没有更新。

标签: javascript openlayers-3 geojson


【解决方案1】:

(fiddle)

您可以使用自定义 AJAX 加载程序来实现这一点(当您将 url 传递给 ol.source.Vector 时,这也是 AJAX)。然后,您可以随意重新加载 JSON 文件。

您可以使用任何您想要的 AJAX 库,但这很简单:

var utils = {
  refreshGeoJson: function(url, source) {
    this.getJson(url).when({
      ready: function(response) {
        var format = new ol.format.GeoJSON();
        var features = format.readFeatures(response, {
          featureProjection: 'EPSG:3857'
        });
        source.addFeatures(features);
        source.refresh();
      }
    });
  },
  getJson: function(url) {
    var xhr = new XMLHttpRequest(),
        when = {},
        onload = function() {
          if (xhr.status === 200) {
            when.ready.call(undefined, JSON.parse(xhr.response));
          }
        },
        onerror = function() {
          console.info('Cannot XHR ' + JSON.stringify(url));
        };
    xhr.open('GET', url, true);
    xhr.setRequestHeader('Accept','application/json');
    xhr.onload = onload;
    xhr.onerror = onerror;
    xhr.send(null);

    return {
      when: function(obj) { when.ready = obj.ready; }
    };
  }
};

【讨论】:

  • 谢谢你 - 我也刚刚看了你的小提琴,会试试看......
  • 这太棒了 - 花了一些时间将它融入我现有的代码中,但它确实有效!感谢您的帮助 - 我自己永远不会解决任何问题。
  • @GarethJones - 我很高兴能帮上忙。
  • 我希望我不会在 Jonatas 上把友谊拉得太远,但是我在这段代码中遇到了很多“Cannot XHR”错误。您能否建议一种在此代码中添加重试循环的方法?我从其他 StackOverflow 问题和其他网站上的其他问题中尝试了一些东西,但没有任何东西完全适合这种结构。如果您有任何想法,我将非常感谢您的帮助!
  • 好吧@GarethJones 错误是关于错误的 URL - 该地址无法访问。您可以在线显示您的代码吗?喜欢 jsfiddle?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-13
  • 1970-01-01
  • 2016-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多