Javascript 获取视口两侧的纬度和经度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14285963/
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-08-24 16:16:49  来源:igfitidea点击:

Get latitude and longitude of the viewport's sides

javascriptgoogle-maps

提问by Salman A

Possible Duplicate:
get current latlng bounds?

可能的重复:
获取当前的纬度边界?

I'm looking for the best way to use the Google maps API to get the latitude and longitude of the map viewport. Ideally I'd like to be able to use the latitude of the map's upper and lower borders and the longitude of the sides. The idea is the figure out the area of the visible map and adjust some related elements on the page based on what's currently visible on the map. Is there a a function in the map's API that handles this?

我正在寻找使用 Google 地图 API 获取地图视口的纬度和经度的最佳方法。理想情况下,我希望能够使用地图上下边界的纬度和两侧的经度。这个想法是找出可见地图的区域,并根据地图上当前可见的内容调整页面上的一些相关元素。地图的 API 中是否有处理此问题的函数?

Thanks, any help is much appreciated.

谢谢,非常感谢任何帮助。

回答by Salman A

Map.getBounds()gives you the LatLngBoundsof the current viewport. You can then use LatLngBounds.getNorthEast()and LatLngBounds.getSouthWest()to get north-east (top right) and south-west (bottom-left) coordinates as LatLng. More specifically:

Map.getBounds()为您提供LatLngBounds当前视口的 。然后LatLngBounds.getNorthEast(),您可以使用和LatLngBounds.getSouthWest()将东北(右上角)和西南(左下角)坐标作为LatLng。进一步来说:

var lat0 = map.getBounds().getNorthEast().lat();
var lng0 = map.getBounds().getNorthEast().lng();
var lat1 = map.getBounds().getSouthWest().lat();
var lng1 = map.getBounds().getSouthWest().lng();

回答by HugoLemos

Use method getBoundsof the Mapclass.

使用方法getBounds的的地图类。