-2

I am trying to get formatted address of customer who access my web page.

For that I wrote function to get lat long and then Reverse geocode it to formatted address.

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script>
 function coordinates_to_address(lat, lng) {
    var latlng = new google.maps.LatLng(lat, lng);
    var geocoder= new google.maps.Geocoder();
    geocoder.geocode({'latLng': latlng}, function(results, status) {
        if(status == google.maps.GeocoderStatus.OK) {
            if(results[0]) {
                $('#address_current').text(results[0].formatted_address);
            } else {
                alert('No results found');
            }
        } else {
            var error = {
                'ZERO_RESULTS': 'No address'
            }

            // alert('Geocoder failed due to: ' + status);
            $('#address_new').html('<span class="color-red">' + error[status] + '</span>');
        }
    });
}

navigator.geolocation.getCurrentPosition(function(location) {
  console.log(location.coords.latitude);
  console.log(location.coords.longitude);
  console.log(location.coords.accuracy);
  var lat= location.coords.latitude;
  var long= location.coords.longitude
  coordinates_to_address(lat, long);
});

</script>

The log showed the output lat long

    10.8888888888888888
    76.43923530000001
    40

But this function returned an error:

  Google Maps API error: MissingKeyMapError https://developers.google.com/maps/documentation/javascript/error-messages#missing-key-map-error
  http://maps.google.com/maps/api/js?sensor=false
  Line 34

Is there any easier method to get the formatted address of a lat long?

Abhishek K
  • 335
  • 2
  • 15

1 Answers1

1

Keys are now required with the Google Maps Javascript API v3 (but the sensor parameter is not).

geocodezip
  • 158,664
  • 13
  • 220
  • 245