2

I have the coordinates of my marker. Now I want to get the address of the marker. So I searched the web and found google maps reserve geocode. Now I tried to do the following:

$.getJSON('http://maps.google.com/maps/api/geocode/json?latlng='+point.lat()+','+ point.lng() +'&key='+apiKey+'&sensor=false&output=json&callback=?',
function(data) {
    console.log(data);
});

When I try to show the address, meaning getting the json, firebug throws the following error:

invalid label

on

 "status": "OK",\n

I searched a lot, but didn't find an answer solving my problem. Can you tell me whats wrong with my code?

Is there another way to get the address data for the coordinates?

Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
Newbie
  • 1,644
  • 5
  • 29
  • 40

2 Answers2

4

You may be getting blocked by the same origin policy. Yesterday I answered a similar question on this topic, which you may want to check out.

You are trying to use the Server-side Geocoding Web Service when Google Maps provides a full featured Client-side Geocoding API for JavaScript. There is also a v2 version, but this has been deprecated recently.

This automatically solves your same-origin problem, and in addition the request limits would be calculated per client IP address instead of of per server IP address, which can make a huge difference for a popular site.

The client-side service should be quite easy to use. Here's a very simple reverse geocoding example using the v3 API:

<script src="http://maps.google.com/maps/api/js?sensor=false"></script>

<script type="text/javascript">     
   var geocoder = new google.maps.Geocoder();
   var point = new google.maps.LatLng(51.50, -0.12);

   if (geocoder) {
      geocoder.geocode({ 'latLng': point }, function (results, status) {
         if (status == google.maps.GeocoderStatus.OK) {
            if (results[1]) {
               console.log(results[1].formatted_address);
            } 
            else {
               console.log('No results found');
            }
         } 
         else {
            console.log('Geocoder failed due to: ' + status);
         }
      });
   }    
</script>

The above example should print Lambeth, Greater London SE1 7, UK to the console.

Community
  • 1
  • 1
Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
1

thanks a lot for the answer that i was looking for. I think here a minor change is required, results[1] should be results[0], otherwise it will show the address of the "neighborhood". the complete should be like this,

<script src="http://maps.google.com/maps/api/js?sensor=false"></script>

<script type="text/javascript">     
   var geocoder = new google.maps.Geocoder();
   var point = new google.maps.LatLng(23.7442812,90.3728129);

   if (geocoder) {
      geocoder.geocode({ 'latLng': point }, function (results, status) {
         if (status == google.maps.GeocoderStatus.OK) {
            if (results[0]) {
               console.log(results[0].formatted_address);
            } 
            else {
               console.log('No results found');
            }
         } 
         else {
            console.log('Geocoder failed due to: ' + status);
         }
      });
   }    
</script>

the output will be shown in the console, Road No 8A, Dhaka, Bangladesh

nasirkhan
  • 9,948
  • 5
  • 27
  • 33