view.calculateExtent返回[NaN,NaN,NaN,NaN]

时间:2016-10-20 12:22:24

标签: openlayers-3

我正在使用OpenLayers 3.18.2。 我的地图是olMap,我的视图是olView。 我想获得屏幕上可见地图的范围。

olView.getCenter() = [2142126.3712395,-4085536.2324793]
olView.getZoom() = 16
olView.getResolution() = 2.388657133911758
olMap.getSize() = [1906,904]
olView.calculateExtent(olMap.getSize()) = [NaN,NaN,NaN,NaN]

为什么这不会恢复到适当的程度?

4 个答案:

答案 0 :(得分:3)

在睡觉之后,考虑到这里的反馈以及一些更多的实验,我找到了问题的原因:坐标来自一个返回字符串的AJAX调用。我打电话给

olView.setCenter([x, y])

当x和y为字符串时,地图视图会更改为正确的中心位置,但之后无法正常运行。将呼叫更改为

olView.setCenter([parseFloat(x), parseFloat(y)])

使地图正常工作。

答案 1 :(得分:0)

Actually you shouldn't have any problem in getting the extent of map as the code seems correct.My map is defined as map and map.getView().calculateExtent(map.getSize()).I was able to get extent.You can see from the screenshot

答案 2 :(得分:0)

In the interest of getting on with the job I calculated the extent myself:

var center = olView.getCenter();
var res = olView.getResolution();
var pxSize = olMap.getSize();
var cx = parseFloat(center[0]);
var cy = parseFloat(center[1]);
var dx = res * pxSize[0] / 2;
var dy = res * pxSize[1] / 2;
var extent = [cx - dx, cy - dy, cx + dx, cy + dy];

olView.getCenter() is returning an array of strings, hence the parseFloat.

答案 3 :(得分:0)

这是一个类似于你的地图的工作jsFiddle(使用OSM作为底图)。 calculateExtent()似乎工作正常http://jsfiddle.net/e969kjro/1/

<div id="map" class="map"></div>
<button id="myButton" onclick="test()">Click Me</button>

var view = new ol.View({
    center: [2142126.3712395,-4085536.2324793],
    zoom: 16
    })

var map = new ol.Map({
    target: 'map',
    layers: [
        new ol.layer.Tile({
            source: new ol.source.OSM()
        })
    ],
    view: view
});

window.test = function () {
    alert(view.calculateExtent(map.getSize()));
}