OpenLayers 3:将地图缩放到数组中的坐标 [英] OpenLayers 3: Zoom map to coordinates in array
问题描述
我需要缩放OpenLayers地图(使用Google Maps基本图层),以将所有给定的坐标列表放入视图中.
I need to zoom my OpenLayers map (which uses a Google Maps baselayer) to fit all of a given list of coordinates into the view.
我已将坐标存储在名为pointsDecimal
的经度和纬度对的数组中,如下所示:
I've stored the coordinates in an array of longitude and latitude pairs named pointsDecimal
which looks like this:
pointsDecimal: Array[50]
0: 148.77638
1: -34.491728
2: 148.77896
3: -34.492302
4: 148.778090711323
...
偶数索引(0、2、4等)是经度,奇数索引(1、3、5等)是纬度.
Even-numbered indices (0, 2, 4 etc) are longitude, and odd-numbered indices (1, 3, 5 etc) are latitude.
这是我要使用的代码:
map.getView().fit(pointsDecimal, map.getSize());
但是由于某些原因,这使我在[0,0]坐标处处于最大缩放级别-在几内亚湾,而不是在我希望成为的新南威尔士州东南部!
But for some reason this puts me at maximum zoom level at the coordinates [0,0] - in the Gulf of Guinea, not in southeastern New South Wales where I'd like to be!
我知道,如果我使用OpenLayers源代码(例如source.getExtent()
)代替我的pointsDecimal
数组,这会容易得多,但是在这种情况下这不是一个选项,因为我不会做对现在-我只能使用数组.
I know this would be much easier if I was using an OpenLayers source (e.g. source.getExtent()
) in place of my pointsDecimal
array, but this is not an option in this case for reasons I won't go into right now - I can only use the array.
有人可以给我任何提示吗?
Can anyone give me any hints please?
编辑:我想我快到了,这要感谢Anupam.这就是我现在拥有的:
I think I'm nearly there, thanks to Anupam. This is what I have now:
var extent = [pointsLongMin,pointsLatMin,pointsLongMax,pointsLatMax];
map.getView().fit(extent,map.getSize());
我传递给数组的值如下:
The values I'm passing to the array are as follows:
pointsLongMin: 148.771162
pointsLatMin: -34.5029736405108
pointsLongMax: 148.77896
pointsLatMax: -34.491728
但是我仍然被[0,0]倾倒在海洋中.我在这里做什么错了?
But I'm still getting dumped out in the ocean at [0,0]. What am I doing wrong here?
推荐答案
为了使用map.getView().fit(extent,size). 范围将四个点表示为[minx,miny,maxx,maxy].
In order to use map.getView().fit(extent,size). extent denotes four points as [minx, miny, maxx,maxy].
它实际上是一个矩形.尝试
It is actually a rectangle. try
var extent=[148.77638,-34.491728,148.77896,-34.492302];
map.getView().fit(extent,map.getSize());
这篇关于OpenLayers 3:将地图缩放到数组中的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!