我需要缩放我的 OpenLayers map (它使用 Google Maps baselayer)以将所有给定的坐标列表放入 View 中。
我已将坐标存储在名为 pointsDecimal
的经度和纬度对数组中,如下所示:
pointsDecimal: Array[50]
0: 148.77638
1: -34.491728
2: 148.77896
3: -34.492302
4: 148.778090711323
...
偶数索引(0、2、4 等)是经度,奇数索引(1、3、5 等)是纬度。
这是我尝试使用的代码:
map.getView().fit(pointsDecimal, map.getSize());
但出于某种原因,这使我在坐标 [0,0] 处处于最大缩放级别 - 在几内亚湾,而不是我想去的新南威尔士州东南部!
我知道如果我使用 OpenLayers 源(例如 source.getExtent()
)代替我的 pointsDecimal
数组,这会容易得多,但这不是在这种情况下的一个选项,出于我现在不会讨论的原因 - 我只能使用数组。
谁能给我一些提示吗?
编辑:感谢 Anupam,我想我快到了。这就是我现在拥有的:
var extent = [pointsLongMin,pointsLatMin,pointsLongMax,pointsLatMax];
map.getView().fit(extent,map.getSize());
我传递给数组的值如下:
pointsLongMin: 148.771162
pointsLatMin: -34.5029736405108
pointsLongMax: 148.77896
pointsLatMax: -34.491728
但我仍然在 [0,0] 处被扔进海里。我在这里做错了什么?
最佳答案
为了使用map.getView().fit(extent,size)。 extent 表示四个点 [minx, miny, maxx,maxy]。
它实际上是一个矩形。尝试
var extent=[148.77638,-34.491728,148.77896,-34.492302];
map.getView().fit(extent,map.getSize());
关于javascript - 开放层 3 : Zoom map to coordinates in array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38291471/