简体   繁体   English

获取以公里为单位的Google Maps API地图的宽度

[英]Get width in kilometers of Google Maps API map

How can I get the current width of the visible area on Google map, i have read many docs but not success? 我已经阅读了许多文档但没有成功,如何获得Google地图上可见区域的当前宽度? I need to get real width of visible area in kilometers. 我需要获取可见区域的实际宽度(以千米为单位)。

I think getBounds() is what you are looking for. 我认为getBounds()是您要寻找的。

var map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);

    google.maps.event.addListener(map, 'idle', function(event) {
        var bounds = map.getBounds();

You will receive a LatLngBounds object (for reference https://developers.google.com/maps/documentation/javascript/reference#LatLngBounds ). 您将收到一个LatLngBounds对象(以供参考https://developers.google.com/maps/documentation/javascript/reference#LatLngBounds )。

This contains NortEase and SouthWest LatLng points. 这包含NortEase和SouthWest LatLng点。 Using those it should be easy to get the width in kilometers using this formula. 使用这些公式,使用该公式很容易获得以千米为单位的宽度。 You can create 2 pairs out of LatLng points to create a NorthWest and NorthEast pair and calculate the distance between those. 您可以在LatLng点中创建2对,以创建NorthWest和NorthEast对,并计算它们之间的距离。

var R = 6371000; // metres
var φ1 = lat1.toRadians();
var φ2 = lat2.toRadians();
var Δφ = (lat2-lat1).toRadians();
var Δλ = (lon2-lon1).toRadians();

var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
    Math.cos(φ1) * Math.cos(φ2) *
    Math.sin(Δλ/2) * Math.sin(Δλ/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

var d = R * c;

See this as an reference ( http://www.movable-type.co.uk/scripts/latlong.html ). 将此作为参考( http://www.movable-type.co.uk/scripts/latlong.html )。

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

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