在 OpenLayers 上重绘之前清除 WMS 图层
我目前正在使用 OpenLayers(Vector 和 WMS)成功显示多个图层。 我的应用程序允许用户修改一些参数,这将:
* 修改绑定
* 修改地图中心
* 修改WMS图片
我使用 Ajax 来避免页面重新加载,它工作得很好,但是当某些图层需要一段时间才能加载时,其他尚未重绘的图层仍然显示在后台(但与我的新上下文完全无关)。 我想在重新向服务器请求新图像之前清除 WMS 层(如果可能),但尚未找到任何内容。 我通过调用removeAllFeature()在矢量图层上执行此操作;
感谢您的帮助!
这是用户单击按钮时我的伪代码:
$.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+"))");
}
}
}
);
I'm currently successfully displaying multiple layers using OpenLayers (Vector and WMS).
My application allow the user to modify some parameters, which will :
* modify bound
* modify map center
* modify the WMS image
I use Ajax to avoid page reload, it works nicely, however when some layers take a while to load, the others which are not yet redrawn are still shown in the background (but aren't related at all to my new context).
I would like to clear WMS layers (if possible) before re-asking the server for the new image but didn't find anything yet.
I do this on the vector layer by calling removeAllFeature();
Thanks for any help !
Here is my pseudo-code when the user click the button :
$.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+"))");
}
}
}
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会使用 mergeNewParams 设置一个参数,使图层绘制为空白。该方法会触发图层的重绘。当回调从更新请求返回时,您执行相反的操作以使其再次正确绘制图层。
(我已经做了类似的事情,但我现在没有源代码)
编辑:
或者 - 当然 - 使用 可见性 图层属性。 :-)
I would use mergeNewParams to set a param that makes the layer draw blank. That method triggers a redraw of the layer. When your callback returns from your update request, you do the opposite to make it draw the layer correctly again.
(I have done something similar to this, but I don't have the source for now)
EDIT:
Or - of course - hide the layers with the visibility property of the layers. :-)
在重绘之前,我终于在每一层上使用clearGrid()函数找到了一个美好的结局。
我还发现,如果有任何正在运行,此函数将停止图块加载。
示例我有两个基础层:EmptyLayer 和WhateverTiledBGLayer。
如果加载WhateverTiledBGLayer图块需要20秒,即使用户选择切换到空层,我也可以看到向服务器发出的请求,以获取WhateverTiledBGLayer上的剩余图块。
只需调用WhateverTiledBGLayer.clearGrid() 将停止请求图层。
感谢您的帮助。
I finally found out an happy ending using the clearGrid() function on every layer before redrawing them.
I also found that this function will stop tile loading if any is running.
Example I had two base layer : EmptyLayer and WhateverTiledBGLayer.
if it tooks 20s to load WhateverTiledBGLayer tiles, even if the user chose to switch to empty layer, I could see the request done to the server for the remaining tiles on WhateverTiledBGLayer.
Simply calling WhateverTiledBGLayer.clearGrid() will stop requesting layers.
Thanks for help.
您可以监听WMS图层的loadstart和loadend事件并相应地隐藏/显示图层。
快速测试表明,您无法在加载启动时将图层的可见性设置为 false,因为 OpenLayers 将停止请求该图层的新图像,并且它永远不会完全加载。您可以尝试使用 layer.display(false/true) 并查看它是否可以解决您的问题。代码应该如下所示:
顺便说一句,如果您没有在 WMS 图层上将 transitionEffect 设置为“调整大小”,则默认行为是在加载新图像之前删除旧图像。这不是你想要的吗?
You could listen to WMS layer's loadstart and loadend event and hide/show layer accordingly.
A quick test showed though that you can't set visibility of the layer to false on loadstart because OpenLayers will then stop requesting new images for the layer and it will never be completely loaded. You could though try with layer.display(false/true) and see if it solves your problem. The code should look something like this:
BTW, if you don't set transitionEffect to "resize" on your WMS layer then the default behavior is that old images are removed before new ones are loaded. Isn't that what you want?