【问题标题】:OpenLayers Refresh Strategy ProblemsOpenLayers 刷新策略问题
【发布时间】:2026-01-18 11:35:02
【问题描述】:

我正在开发一个应用程序,其中一部分使用 OpenLayers(称为 Geoserver 服务的 WMS)显示一些经常更新的数据(船只轨迹 - 或者更具体地说,一系列点)。

我希望以设定的时间间隔更新此船只轨迹 - OpenLayers.Strategy.Refresh 似乎是最合适的方法。我稍微修改了 wms.html 示例(OpenLayers 2.11)来尝试这个,即:

underway = new OpenLayers.Layer.WMS("Underway Data",
    "http://ubuntu-geospatial-server:8080/geoserver/underway/wms", 
    {'layers': 'underway:ss2011_v03', transparent: true, format: 'image/gif'},
    {isBaseLayer: false},
    {strategies : [new OpenLayers.Strategy.Refresh({interval: 6000})]} 
);

map.addLayers([layer, underway]);

据我所知,这应该可以正常工作(即每 6 秒刷新一次正在进行的图层),但是什么也没有发生。底层 WMS 正在更新 - 如果我手动刷新地图,则会出现更新的数据。

我确定我遗漏了一些相当明显的东西,任何帮助将不胜感激。我在 Firebug 或任何东西中没有收到任何错误,它只是什么也没做。

【问题讨论】:

    标签: javascript map openlayers


    【解决方案1】:

    好吧,据我所知,您无法对 WMS 服务执行刷新策略。所以我将我的代码转换为使用 WFS,它按预期工作。代码:

            underway = new OpenLayers.Layer.Vector("WFS", {
                strategies: [new OpenLayers.Strategy.BBOX(), new OpenLayers.Strategy.Refresh({interval: 4000, force: true})],
                protocol: new OpenLayers.Protocol.WFS({
                    url:  "http://ubuntu-geospatial-server:8080/geoserver/wfs",
                    featureType: "ss2011_v03",
                    featureNS: "http://csiro.au/underway",
                    geometryName: "position"
                });
    

    请注意,我还需要一个 BBOX 策略。我发现的另一个问题是我需要手动指定geometryName,否则它会默认为“the_geom”,我的图层不存在。

    【讨论】:

      【解决方案2】:

      我很确定您需要添加new OpenLayers.Strategy.Static() 策略才能使其正常工作。 你需要激活你的Refresh 策略,这意味着你必须把它放在一个单独的变量中。

      【讨论】:

      • 那里没有欢乐。我在主干或 2.11 中都找不到 OpenLayers.Strategy.Static()。我尝试设置一些看起来可能有效的其他策略(bbox,已修复),但效果不佳。我修改后的代码看起来像:bboxStrategy = new OpenLayers.Strategy.BBOX(); bboxStrategy.setLayer(underway); bboxStrategy.activate(); refreshStrategy = new OpenLayers.Strategy.Refresh({interval: 2000, force: true}); refreshStrategy.setLayer(underway); refreshStrategy.activate(); 但这导致了同样的问题,没有发生任何事情。