title: Google Maps v3 - Viewable map radius in miles
body: <p>How can I find the proximity of the visible area of my map ?</p>
  
  <p><em>All this works and is a post to help others who may be looking.</em> </p>
  
  <p>This code looks at your Google Map v3 and outputs by var proximity the radius in miles of your current map view.</p>
  
  <p>Make sure in your HTML you have </p>
  
  <pre><code>&lt;script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true&amp;libraries=geometry"&gt;&lt;/script&gt;
  </code></pre>
  
  <p>as libraries=geometry enables it.</p>
  
  <pre><code>// 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
  </code></pre>
  
  <p>I hope it helps someone.</p>
  
  <p>An additional edit ( untested ) which allows for screen rotation follows:</p>
  
  <pre><code>    // 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 &gt; proximitymetersenw) {proximitymeterswne = proximitymiles}
  else {if (proximitymetersenw &gt; proximitymeterswne) {proximitymetersenw = proximitymiles};}; 
  
  var proximitymiles = proximitymeter * 0.000621371192;
  alert(" " + proximitymiles + " Miles Proximity");
  var proxmity =  proximitymiles; 
  // Get Gmap radius / proximity End
  </code></pre>
  
  <p>Another EDIT</p>
  
  <pre><code>// 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
  };
  </code></pre>
  
  <hr>
  
  <p>I put the edited code in the first post.</p>
  
  <p>Terran</p>