在我的矢量层协议中动态更改了url,刷新后不考虑

Dynamically changed url in my Vector layer protocol not taken into account after refresh

本文关键字:url 刷新 不考虑 动态 我的 协议      更新时间:2023-09-26

我有一个地图,当用户点击一个单选按钮时,我试图动态地改变它的url。

下面是我的代码:
var refreshLayer = new OpenLayers.Strategy.Refresh({ force: true, active: true });
var fromProjection = new OpenLayers.Projection("EPSG:4326");
var toProjection = new OpenLayers.Projection("EPSG:3857");
var vectorProtocol = new OpenLayers.Protocol.HTTP({
        url: '/getmaindata',
        format: new OpenLayers.Format.GeoJSON({
            'internalProjection': fromProjection,
            'externalProjection': toProjection
        }),
        params: {
            "naf": "04"
        }
    });
var map = new OpenLayers.Map("map_element");
var vectorStrategies = [new OpenLayers.Strategy.Fixed(), refreshLayer];
var osmLayer = new OpenLayers.Layer.OSM();
var vectorLayer = new OpenLayers.Layer.Vector('vector_layer', {
   protocol: vectorProtocol,
   strategies: vectorStrategies
});
map.addLayers([osmLayer, vectorLayer]);
(...)
// $radio is my radiobutton
$radio.onclick = function() {
      vectorProtocol.url = "/getouterdata";
      refreshLayer.refresh({force: true});
});

当我用Firebug调试时,我注意到url在我的层的协议对象中被正确地更改了。但是,调用的是之前的url (/getmaindata),而不是新的url (/getouterdata)。所以我问自己是不是做错了什么。什么时候调用新url ?是通过调用我的策略的refresh()方法。刷新吗?还是去别的地方?

问题解决:'url'位于vectorProtocol的'options'字段

$radio.onclick = function() {
      vectorProtocol.options.url = "/getouterdata";
      refreshLayer.refresh({force: true});
});