Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps v3 - Viewable map radius in miles

How can I find the proximity of the visible area of my map ?

All this works and is a post to help others who may be looking.

This code looks at your Google Map v3 and outputs by var proximity the radius in miles of your current map view.

Make sure in your HTML you have

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

as libraries=geometry enables it.

// 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

I hope it helps someone.

An additional edit ( untested ) which allows for screen rotation follows:

    // 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

Another EDIT

// 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
};

I put the edited code in the first post.

Terran

like image 886
Terran Brown Avatar asked Feb 03 '12 00:02

Terran Brown


People also ask

How do I get a mile radius on Google Maps?

If you're using CalcMaps, click on Draw a circle and add the circle on the map area you're interested in. Use can then use the drop-down menu to select the radius type you want to use. The tool automatically calculates the circle area as well.

Can Google Maps show a driving radius?

The radius option will enable you to draw a circle in miles or traveling distance (in time) from any specific location, incorporating all directions up to the maximum. The radius map tool allows users to determine distances between a location and specific markers that fall within the area.

How do I get 2 mile radius on Google Maps?

Select Measure distance . Move the map so that the black circle is on the next point you want to add. At the bottom, you can find the total distance in miles (mi) and kilometers (km).

How do I get 5km radius on Google Maps?

Have a street directory and a compass you last used in Year 11? Sweet. Find the map's scale, extend your compass to cover 5km, stick the pin in your home address and give that bad boy a 360 degree twirl. That's your radius.


1 Answers

// 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");
}
like image 186
Terran Brown Avatar answered Oct 17 '22 17:10

Terran Brown