Google Maps v3 - 以英里为单位的可视地图半径

时间:2012-02-03 00:00:42

标签: javascript html5 google-maps-api-3 geolocation

如何找到地图可见区域的距离?

这一切都有效,并且是一个帮助其他可能正在寻找的人的帖子。

此代码会查看您的Google地图v3,并按当前地图视图的半径(以英里为单位)输出。

确保您的HTML

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true&libraries=geometry"></script>

as libraries = geometry启用它。

// Get Gmap radius / proximity start
var bounds = new google.maps.LatLngBounds();
var sw = bounds.getSouthWest(); 
var ne = bounds.getNorthEast();

var proximitymeter = google.maps.geometry.spherical.computeDistanceBetween (sw, ne);
var proximitymiles = proximitymeter * 0.000621371192;
alert(" " + proximitymiles + " Miles Proximity");
var proxmity =  proximitymiles; 
// Get Gmap radius / proximity End

我希望它有所帮助。

允许屏幕旋转的附加编辑(未测试)如下:

    // Get Gmap radius / proximity start
var bounds = new google.maps.LatLngBounds();
var sw = bounds.getSouthWest(); 
var ne = bounds.getNorthEast();
var se = bounds.getSouthEast(); 
var nw = bounds.getNorthWest();

var proximitymeterswne = google.maps.geometry.spherical.computeDistanceBetween (sw, ne);
var proximitymetersenw = google.maps.geometry.spherical.computeDistanceBetween (se, nw);

if (proximitymeterswne > proximitymetersenw) {proximitymeterswne = proximitymiles}
else {if (proximitymetersenw > proximitymeterswne) {proximitymetersenw = proximitymiles};}; 

var proximitymiles = proximitymeter * 0.000621371192;
alert(" " + proximitymiles + " Miles Proximity");
var proxmity =  proximitymiles; 
// Get Gmap radius / proximity End

另一个编辑

// Watches for map bounds change - if so - set radius and refresh data Start
function setproximityfrommap() {
    // http://stackoverflow.com/questions/3525670/radius-of-viewable-region-in-google-maps-v3
    // Get Gmap radius / proximity start
    var bounds = new google.maps.LatLngBounds();
    // bounds = map.LatLngBounds();
    //center = new google.maps.LatLng();

    ne = bounds.getNorthEast();    
    center = map.getCenter();

    //var center = map.LatLngBounds.getCenter();
    //var ne = map.LatLngBounds.getNorthEast();

    // r = radius of the earth in statute miles
    var r = 3963.0;

    // Convert lat or lng from decimal degrees into radians (divide by 57.2958)
    var lat1 = center.lat() / 57.2958;
    var lon1 = center.lng() / 57.2958;
    var lat2 = ne.lat() / 57.2958;
    var lon2 = ne.lng() / 57.2958;

    // distance = circle radius from center to Northeast corner of bounds
    proximity = r * Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1));
    $('#proximity').val(proximity);
    // Get Gmap radius / proximity End
};

我把编辑后的代码放在第一篇文章中。

人族

1 个答案:

答案 0 :(得分:10)

// Watches for map bounds change - if so - set radius and refresh data Start
function setproximityfrommap() {
    // http://stackoverflow.com/questions/3525670/radius-of-viewable-region-in-google-maps-v3
    // Get Gmap radius / proximity start
    // First, determine the map bounds
    var bounds = map.getBounds();

    // Then the points
    var swPoint = bounds.getSouthWest();
    var nePoint = bounds.getNorthEast();

    // Now, each individual coordinate
    var swLat = swPoint.lat();
    var swLng = swPoint.lng();
    var neLat = nePoint.lat();
    var neLng = nePoint.lng();

    var proximitymeter = google.maps.geometry.spherical.computeDistanceBetween(swPoint, nePoint);
    var proximitymiles = proximitymeter * 0.000621371192;
    proxmity = proximitymiles;
    console.log("Proxmity " + proximitymiles + " miles");
}