2

I am required to get the formatted address of a given coordinates using Google Maps API. I use the Google Reverse Geo coding for finding the location name. This works fine when there is a name available for the location in the Google maps database.

Most of the times, the given coordinates are from a location far off from city boundaries (on a highway for example). The function returns ZERO_RESULTS as there is no name defined on the map. Requirement is to find the nearest known location address to be returned.

Functionally this is a nice to hear, but technically, how to go about it?

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.

Personally, didn't like this approach because of the following reasons:

  1. Can't guess which direction to go to find the location which has a name

  2. I might go in a direction but a known place is just few meters in the reverse direction.

  3. 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.

The following is the complete code, which is working but with the above issues.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Map Test</title>
     <script type="text/javascript" src="//code.jquery.com/jquery-1.11.1.min.js"></script>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>

</head>
<body>
    <span id="spanId">Loading...</span>
    <script>
        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);
                    }

               }
            });
        }


    </script>
</body>
</html>

would really appreciate if anyone has a good solution.

JSFiddle Link: https://jsfiddle.net/hbybs68q/1/

thanks.

Lakshman Pilaka
  • 1,803
  • 2
  • 24
  • 48

2 Answers2

2

You need to modify the if condition inside getLocation function. Check for results[0] not result[1].

if (status == google.maps.GeocoderStatus.OK) {
      if (results[0]) {
            document.getElementById('spanId').innerHTML = results[0].formatted_address + ', ' + distance + ' kms away from original point' ;
      } else {
              window.alert('No results found');
      }
} else {
       window.alert('Geocoder failed due to - ' + status)
}
Shreeram K
  • 1,719
  • 13
  • 22
  • the status itself shows `ZERO_RESULTS`. the results section doesn't even execute. there is use changing from results[0] or results[1] – Lakshman Pilaka Aug 07 '15 at 11:23
  • Are you trying to click somewhere in Oceans? Because for ocean results[0] will be null i.e ZERO_RESULTS – Shreeram K Aug 07 '15 at 11:30
  • i am trying to click at 32.6811,74.8732. See the location in [maps] (https://www.google.co.in/maps/@32.6811,74.8732,15z?hl=en). This is somewhere in Jammu & Kashmir state in India. – Lakshman Pilaka Aug 07 '15 at 11:33
  • I think, google has not mapped that area. In my application also I am getting ZERO_RESULT for any click in Jammu and Kashmir. I'll file this bug to Google. – Shreeram K Aug 07 '15 at 11:45
  • @eugensunic, I have added the fiddle link... https://jsfiddle.net/hbybs68q/1/ for your quick reference. – Lakshman Pilaka Aug 07 '15 at 12:19
  • @LakshmanPilaka, Have you made the equations yourself or did you copy paste them from somewhere? – EugenSunic Aug 07 '15 at 12:24
  • @eugensunic i have used the code from this [post](http://stackoverflow.com/questions/2637023/how-to-calculate-the-latlng-of-a-point-a-certain-distance-away-from-another) – Lakshman Pilaka Aug 07 '15 at 12:47
0

ZERO_RESULTS from Reverse Geocoding in the Google Geocoding API typically mean the latlng is in a body water far off shore (oceans, seas, really big lakes, etc.) or in an area with a territorial dispute. This is a known issue: https://code.google.com/p/gmaps-api-issues/issues/detail?id=8783

miguev
  • 4,481
  • 21
  • 41