Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Radius of "viewable" region in google maps v3

I need to know how to retrieve the radius of the viewable zoom level in google maps API v3.

For example, if I am at zoom level 3, and depending on the users screen size (lets just say 400x400 viewable region) how do I get the "radius" circle of the viewable area.

Alternatively I'm currently using map.fitBounds() for all the points I've added to the map so really all I NEED is the radius of all the bounds. What I want is something like "20 miles" that I can feed into my database app.

like image 956
Mech Avatar asked Aug 19 '10 19:08

Mech


People also ask

Can Google maps show radius?

Google Maps does not support the radius functionality, which means that you can't determine the radius around a given location. But you can measure the distance between two or more points. As a quick reminder, the radius of a circle is the distance from its edge to its center.

How do you get a travel radius on Google Maps?

Click on the map and create a popup marker to select the point. From there, opt for the “Draw Radius.” Choose the proximity distance from the given address found within the radius options in the software. Once settings are entered, a map will show the highlighted parameters on the map.

How do you mark a 3 mile radius on Google Maps?

Drawing the RadiusSelect the radius and proximity tool from the left-hand menu. Depending on your needs, choose either distance radius or drive time polygon. Enter your locations and the distance you would like your radius to reach from the center point. Customize your map with color-coding and labeling tools.

How do I limit areas on Google Maps?

Step 1 Go to Add or Edit Map and scroll down to 'Limit Panning Settings' section. Step 2 Enable 'Limit Panning' tab.


2 Answers

The radius would equal the distance from the center of the bounds to one of the bound's corners. Using the Great Circle Distance Formula from the calculations from this page, I came up with the following:

var bounds = map.getBounds();  var center = bounds.getCenter(); var ne = bounds.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 var dis = r * Math.acos(Math.sin(lat1) * Math.sin(lat2) +    Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1)); 

After playing some more with this, I've noticed that map.getBounds() will contain the full map viewport. But if your LatLngBounds is built by extending to include LatLng points, and then you issue a map.fitBounds(bounds), the api increases the map's viewport a bit so that the bounds "box" has some padding.

If you use the map's current viewport, the radius from the center to the corner of the viewport might be a longer radius than you want. Maybe the distance from the viewport center to the middle of the furthest viewport edge. (If the map isn't a perfect square)

like image 87
Eric C Avatar answered Sep 28 '22 09:09

Eric C


Refactored version of Eric's answer above using google.maps.geometry.spherical namespace (make sure you loaded Geometry library to make this work).

var bounds = map.getBounds(); var center = map.getCenter(); if (bounds && center) {   var ne = bounds.getNorthEast();   // Calculate radius (in meters).   var radius = google.maps.geometry.spherical.computeDistanceBetween(center, ne); } 
like image 33
Ruslan Kabalin Avatar answered Sep 28 '22 09:09

Ruslan Kabalin