javascript 如何从 OpenLayers 中获取地图的当前视口作为几何、边界框或 wkt?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8988453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:14:37  来源:igfitidea点击:

How to get the current viewport of the map out of OpenLayers as geometry, bounding box or wkt?

javascriptopenlayerslatitude-longitude

提问by Patrick Hillert

I'm trying to find some hints where I should search for this topic but I've nothing found - and I spent many hours on this.

我试图找到一些我应该搜索这个主题的提示,但我什么也没找到 - 我花了很多时间在这上面。

I'm also trying to get the current coordinates out of the current displayed viewport from the OpenLayers map to add only these vectors that are in the current bounding box of the current viewport.

我还试图从 OpenLayers 地图中获取当前显示视口的当前坐标,以仅添加当前视口的当前边界框中的这些向量。

回答by the_skua

For OpenLayers 2:

对于 OpenLayers 2:

Map.getExtent()

...will return a Bounds, which you can then use to get the lat/long coordinates in any number of ways: http://dev.openlayers.org/apidocs/files/OpenLayers/BaseTypes/Bounds-js.html#OpenLayers.Bounds

...将返回一个边界,然后您可以使用它以多种方式获取纬度/经度坐标:http: //dev.openlayers.org/apidocs/files/OpenLayers/BaseTypes/Bounds-js.html# OpenLayers.Bounds

Ideally, you'd turn the vectors into Geometry objects and check them against Map.getExtent() using Bounds.intersectBounds() to see if they're in the current viewport.

理想情况下,您可以将矢量转换为 Geometry 对象,并使用 Bounds.intersectBounds() 对照 Map.getExtent() 检查它们是否在当前视口中。

For OpenLayers 3:

对于 OpenLayers 3:

ol.Map.getView().calculateExtent(map.getSize())

...will return an array of coordinates, representing the bounding box of the extent.

...将返回一个坐标数组,表示范围的边界框。

回答by swatchai

For openlayers 5.3.

对于 openlayers 5.3。

olmap.getView().calculateExtent(olmap.getSize());