3

Today I struggled with the following:

$.ajax({url:'http://maps.google.com/maps/api/geocode/jsonaddress=Karachi&sensor=false&output=json&callback=?',
        dataType: 'json',
        success: function(data){
         //eval("("+data+")");
         alert(data);
        }
});

Firefox gives the error "Invalid Label" and Chrome "Uncaught SyntaxError: Unexpected token :". I found a lot of posts about this, and I tried all kinds of things like eval(), but also:

$.getJSON('http://maps.google.com/maps/api/geocode/jsonaddress=Karachi&sensor=false&output=json&callback=?',
 function(data){
  //eval("("+data+")");
  alert(data);
 }
);

Same result. Also, other json data works fine, for instance flickr ("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?). So it has something to do with the Google Maps API output i guess..

Thanks in advance.

RedBlueThing
  • 42,006
  • 17
  • 96
  • 122
Bennystijn
  • 53
  • 1
  • 6

2 Answers2

2

You can do it in maps V3, but you can't use $.getJSON to get the data, instead there is a method in the maps api which retrives the data if you give it an address.

var  geocoder = new google.maps.Geocoder();
 geocoder.geocode( { 'address': '10 downing street, London, UK'}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        console.log(results);
        alert(results[0].geometry.location);

      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });

these links have all the info.... https://developers.google.com/maps/documentation/javascript/geocoding#GeocodingRequests

https://google-developers.appspot.com/maps/documentation/javascript/examples/geocoding-simple

iamalismith
  • 1,531
  • 9
  • 22
0

You just can't do it; AFAIK Geocoder V3 does not allow callback=?.
Check this thread for further information.

systempuntoout
  • 71,966
  • 47
  • 171
  • 241
  • Thanks for your reply, but Google DOES send back the JSON data. I can see it in Firebug. Also callback= is filled in, in my case: jsonp1271887454223. So that works.. – Bennystijn Apr 21 '10 at 22:04
  • Anyway, I now fixed it with help of this tut: http://www.developer.com/db/article.php/10920_3621981_2/Performing-HTTP-Geocoding-with-the-Google-Maps-API.htm. It's kinda slow and xml instead of json, but works in my case. Thanks! – Bennystijn Apr 21 '10 at 23:33
  • @Benny JSON is returned but not wrapped in jsonp1271887454223() and firefox raises "invalid label" error.You could use callback but with V2 (deprecated) i think. – systempuntoout Apr 22 '10 at 06:12
  • Yeah, i think the tut i followed used v2. – Bennystijn Apr 22 '10 at 18:34