title: Find nearest known location: Google Reverse Geocoding
body: <p>I am required to get the formatted address of a given coordinates using Google Maps API. I use the <a href="https://developers.google.com/maps/documentation/javascript/examples/geocoding-reverse" rel="nofollow">Google Reverse Geo coding</a> for finding the location name. This works fine when there is a name available for the location in the Google maps database.</p>
  
  <p>Most of the times, the given coordinates are from a location far off from city boundaries (on a highway for example). The function returns <code>ZERO_RESULTS</code> as there is no name defined on the map. Requirement is to find the nearest known location address to be returned.</p>
  
  <p>Functionally this is a nice to hear, but technically, how to go about it? </p>
  
  <p>Currently I am finding the location which is few (kilo)meters away from this point, checking if the location has a name, and recursively going till i get a name.</p>
  
  <p>Personally, didn't like this approach because of the following reasons:</p>
  
  <ol>
  <li><p>Can't guess which direction to go to find the location which has a name</p></li>
  <li><p>I might go in a direction but a known place is just few meters in the reverse direction.</p></li>
  <li>I might go too far during the increment, as in a place which is 15
  kms away has a name. I search for the name 10 kms away and check at
  20 kms again as the increment identity is 10 kms.</li>
  </ol>
  
  <p>The following is the complete code, which is working but with the above issues.</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
      &lt;title&gt;Map Test&lt;/title&gt;
       &lt;script type="text/javascript" src="//code.jquery.com/jquery-1.11.1.min.js"&gt;&lt;/script&gt;
      &lt;script src="https://maps.googleapis.com/maps/api/js?v=3.exp"&gt;&lt;/script&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
      &lt;span id="spanId"&gt;Loading...&lt;/span&gt;
      &lt;script&gt;
          Number.prototype.toRad = function () {
              return this * Math.PI / 180;
          }
  
          Number.prototype.toDeg = function () {
              return this * 180 / Math.PI;
          }
  
          google.maps.LatLng.prototype.destinationPoint = function (brng, dist) {
              dist = dist / 6371;
              brng = brng.toRad();
  
              var lat1 = this.lat().toRad(), lon1 = this.lng().toRad();
  
              var lat2 = Math.asin(Math.sin(lat1) * Math.cos(dist) +
                                   Math.cos(lat1) * Math.sin(dist) * Math.cos(brng));
  
              var lon2 = lon1 + Math.atan2(Math.sin(brng) * Math.sin(dist) *
                                           Math.cos(lat1),
                                           Math.cos(dist) - Math.sin(lat1) *
                                           Math.sin(lat2));
  
              if (isNaN(lat2) || isNaN(lon2)) return null;
  
              return new google.maps.LatLng(lat2.toDeg(), lon2.toDeg());
          }
  
          var pointA = new google.maps.LatLng(32.6811,74.8732);
  
          getLocation(pointA);
  var distance = 0;
          function getLocation(info) {
  
              var myCenter = info; //new google.maps.LatLng(info.split(",", 3)[1], info.split(",", 3)[2]);
              var gc = new google.maps.Geocoder();
  
              gc.geocode({ 'location': myCenter }, function (results, status) {
                  if (status == google.maps.GeocoderStatus.OK) {
                      if (results[1]) {
                          document.getElementById('spanId').innerHTML = results[1].formatted_address + ', ' + distance + ' kms away from original point' ;
                      } else {
                          window.alert('No results found');
                      }
                  } else {
                      if (status == 'ZERO_RESULTS' )
                      {
                          var radiusInKm = 10;
                          distance += radiusInKm;
                          document.getElementById('spanId').innerHTML = 'Getting results from  ' + distance + ' kms away';
                          var pointB = pointA.destinationPoint(90, distance);
                          setTimeout(function(){
                              getLocation(pointB);
                          }, 2000);
                      }
  
                 }
              });
          }
  
  
      &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>would really appreciate if anyone has a good solution.</p>
  
  <p>JSFiddle Link: <a href="https://jsfiddle.net/hbybs68q/1/" rel="nofollow">https://jsfiddle.net/hbybs68q/1/</a></p>
  
  <p>thanks.</p>