views:

202

answers:

1

Hello! 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?

A: 

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"&gt;&lt;/script&gt;

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

Daniel Vassallo