简体   繁体   English

OpenLayers 3:将地图缩放到数组中的坐标

[英]OpenLayers 3: Zoom map to coordinates in array

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. 我需要缩放我的OpenLayers地图(使用Google Maps基本图层)以将所有给定的坐标列表放入视图中。

I've stored the coordinates in an array of longitude and latitude pairs named pointsDecimal which looks like this: 我已将坐标存储在名为pointsDecimal的经度和纬度对数组中,其外观如下:

pointsDecimal: Array[50]
  0: 148.77638
  1: -34.491728
  2: 148.77896
  3: -34.492302
  4: 148.778090711323
  ...

Even-numbered indices (0, 2, 4 etc) are longitude, and odd-numbered indices (1, 3, 5 etc) are latitude. 偶数索引(0、2、4等)是经度,而奇数索引(1、3、5等)是纬度。

This is the code I'm trying to use: 这是我要使用的代码:

map.getView().fit(pointsDecimal, map.getSize());

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! 但是由于某种原因,这使我处于[0,0]坐标的最大缩放级别-在几内亚湾,而不是在我希望成为的新南威尔士州东南部!

I know this would be much easier if I was using an OpenLayers source (eg 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. 我知道,如果我使用的OpenLayers源(例如,这将是很容易source.getExtent()来代替我的) pointsDecimal阵列,但这不是在这种情况下的原因,我不会进入现在的选择-我只能使用数组。

Can anyone give me any hints please? 有人可以给我任何提示吗?

EDIT: I think I'm nearly there, thanks to Anupam. 编辑:我想我快到了,这要归功于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

But I'm still getting dumped out in the ocean at [0,0]. 但是我仍然被[0,0]丢进大海。 What am I doing wrong here? 我在这里做错了什么?

In order to use map.getView().fit(extent,size). 为了使用map.getView()。fit(extent,size)。 extent denotes four points as [minx, miny, maxx,maxy]. 范围将四个点表示为[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());

most propable reason is that your map is projected on EPSG:3857 while your extent EPSG:4326 . 最可能的原因是您的地图投影在EPSG:3857而范围投影在EPSG:4326 So you need to transform your extent to your map projection. 因此,您需要将范围转换为地图投影。

var coordMin = ol.proj.fromLonLat([148.77638,-34.491728], 'EPSG:3857');
var coordMax = ol.proj.fromLonLat([148.77896,-34.492302], 'EPSG:3857');
var extent=[coordMin[0],coordMin[1],coordMax[0],coordMax[1]];
map.getView().fit(extent,map.getSize());

You may find a more elegand way to do the extent transformation. 您可能会发现一种更广泛的方法来进行范围转换。 Unless you provide your full code we can only guess what is your map projection. 除非您提供完整的代码,否则我们只能猜测您的地图投影是什么。 I am just guessing is EPSG:3857 cause thats the default ol3 use. 我只是猜测是EPSG:3857原因,这就是默认的ol3使用。

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

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