2011-02-10 242 views
1

我目前使用OpenLayers(Vector和WMS)成功显示多个图层。 我的应用程序允许用户修改一些参数,这将:
*修改势必
*修改地图中心
*修改WMS图像
我使用Ajax来避免页面重载,它工作得很好,但是当一些层需要一段时间才能加载,其他尚未重绘的图像仍会显示在背景中(但与我的新上下文无关)。 我想在重新向服务器请求新图像之前清除WMS图层(如果可能),但尚未找到任何东西。 我在矢量图层上通过调用removeAllFeature();在OpenLayers上重绘之前清除WMS图层

感谢您的帮助!

这里是我的伪代码,当用户点击该按钮:

$.getJSON("url/updateMapConfig.php", 
    { 
     data: $(this).serialize() , 
     ajax: 'true', 
     cache: 'false' 
    }, 
    function(j){ 
     if (j.result == 'ok') { 
     var layersToShow = []; 
     // Refresh vector layer 
     for(var i=0;i<map.layers.length;i++) { 
      if (map.layers[i].isVector) { 
      map.layers[i].removeAllFeatures(); 
      map.layers[i].refresh({force:true}); // Vector layer 
      } 
     } 
     // Refresh visible layers 
     for(var i=0;i<map.layers.length;i++) { 
      if (map.layers[i].visibility && !map.layers[i].isBaseLayer && !map.layers[i].isVector) { // Refresh visible non base 
       map.layers[i].redraw(true); // Other layer 
      } 
     } 
     // Set new bounds 
     var bounds = new OpenLayers.Bounds(); 
      bounds.extend(new OpenLayers.LonLat(j.bounds.xmin-100, j.bounds.ymin-100)); 
      bounds.extend(new OpenLayers.LonLat(parseInt(j.bounds.xmax)+100, parseInt(j.bounds.ymax)+100)); 
     map.zoomToExtent(bounds); 
     // Move to destination point 
     if (j.poiCenter != "") { 
      eval("map.setCenter(new OpenLayers.LonLat("+j.poiCenter+"))"); 
     } 
     } 
    } 
); 

回答

1

我终于找到了利用每一层的clearGrid()函数重绘他们面前的大团圆结局。

我还发现,如果任何正在运行该功能将停止瓦负载。 示例我有两个基础层:EmptyLayer和WhateverTiledBGLayer。 如果花了20s加载WhateverTiledBGLayer图块,即使用户选择切换到空图层,我也可以看到WhateverTiledBGLayer上的剩余图块向服务器发出了请求。

简单的调用WhateverTiledBGLayer.clearGrid()将停止请求层。

感谢您的帮助。

1

我会用mergeNewParams设置PARAM,使层的拉丝坯料。该方法触发图层的重绘。当您的回调从您的更新请求返回时,您会做相反的操作,使其再次正确绘制图层。

(我已经做了类似这样的东西,但我没有源现在)

编辑:

或者 - 当然 - 隐藏与层的visibility属性层。 :-)

+0

谢谢,我试过:map.layers [i] .setVisibility(false); \t \t \t \t \t map.layers [i]中。setVisibility(真);然而,这层仍然保留,直到重绘,足够奇怪。我会尝试mergeNewParams – 2011-02-11 08:43:10

+0

也许你必须在设置可视性时强制重绘?尽管看起来不太可能... – 2011-02-11 10:48:41

0

你可以相应地监听WMS层的loadstart和loadend事件和隐藏/显示层。

快速测试结果显示,虽然你不能设置图层的可见性为false loadstart因为的OpenLayers就停止请求层新的图像,它永远不会被完全加载。你可以尝试使用layer.display(false/true),看看它是否能解决你的问题。代码应该是这个样子:

yourWmsLayer.events.on({ 
     "loadstart" : function(){ 
      this.display(false); 
     }, 
     "loadend" : function(){ 
      this.display(true); 
     } 
    }); 

顺便说一句,如果你不设置transitionEffect您的WMS层上的“调整”,那么默认行为是旧图片被删除之前,新的问题也加载。这不是你想要的吗?