简体   繁体   English

缩放OpenLayers3地图以覆盖多个矢量图层

[英]Zooming an OpenLayers3 map to cover multiple vector layers

I have an OpenLayers3 map consisting of an OSM Tile layer and one or more Vector layers. 我有一个OpenLayers3地图,包含一个OSM Tile图层和一个或多个Vector图层。 I can zoom the map into a single layer using 我可以使用将地图缩放为单个图层

vector.addEventListener("change", function() {
    map.getView().fitExtent(vectorSource.getExtent(), map.getSize());
});

and this works. 这很有效。 However, if I try to zoom to cover multiple layers using the following in the loop that adds layers 但是,如果我尝试在添加图层的循环中使用以下内容缩放以覆盖多个图层

vector.addEventListener("change", function () {
    if (bounds == null) {
        bounds = vectorSource.getExtent();
    } else {
        bounds = bounds.extend(vectorSource.getExtent());
    }
    map.getView().fitExtent(bounds, map.getSize());
});

with bounds being declared outside the loop, then maps with a single layer continue to work. 如果在循环外声明了边界,那么具有单个图层的地图将继续工作。 However, with more than one layer, the code in the else block give the error "undefined is not a function". 但是,如果有多个图层,则else块中的代码会给出错误“undefined is not a function”。

According to the documentation getExtent() returns an extent object and the extent object has an extend method , so I'm not sure why this errors. 根据文档getExtent()返回一个extent对象而extent对象有一个extend方法 ,所以我不确定为什么会出现这个错误。

Answering own question as I worked it out. 在我解决问题时回答了自己的问题。 I was using extend wrongly. 我错误地使用了扩展。 The correct code is: 正确的代码是:

vector.addEventListener("change", function () {
    if (bounds == null) {
        bounds = vectorSource.getExtent();
    } else {
        bounds = ol.extent.extend(bounds, vectorSource.getExtent());
    }
    map.getView().fitExtent(bounds, map.getSize());
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM