2011-02-10 3 views
1

J'essaie d'obtenir une réponse JSON google maps en utilisant ce code:jQuery JSON Parse google maps problème

var address = 'Sydney,NSW'; 
$.ajax({ 
    type: "GET", 
    url: "http://maps.googleapis.com/maps/api/geocode/json?sensor=false", 
    dataType: "jsonp", 
    data: ({"address":address}), 
    success: function(json){ 
     if (json.status == "OK") { 
      alert(":)"); 
     } else { 
      alert('wrong!'); 
     } 
    } 
}); 

Cela fonctionne vraiment bien, mais ...

En conséquence, je suis obtenir

Uncaught SyntaxError: jeton inattendu: dans Chrome et erreur: aucun élément trouvé dans FireFox ..

il est semble que quelque chose de mal est avec JSON retourné, mais en regardant bien:

{ 
"status": "OK", 
"results": [ { 
"types": [ "locality", "political" ], 
"formatted_address": "Sydney New South Wales, Australia", 
    "address_components": [ { 
    "long_name": "Sydney", 
    "short_name": "Sydney", 
    "types": [ "locality", "political" ] 
    }, { 
    "long_name": "New South Wales", 
    "short_name": "New South Wales", 
    "types": [ "administrative_area_level_1", "political" ] 
}, { 
    "long_name": "Australia", 
    "short_name": "AU", 
    "types": [ "country", "political" ] 
} ], 
"geometry": { 
    "location": { 
    "lat": -33.8689009, 
    "lng": 151.2070914 
    }, 
    "location_type": "APPROXIMATE", 
    "viewport": { 
    "southwest": { 
     "lat": -34.1648540, 
     "lng": 150.6948538 
    }, 
    "northeast": { 
     "lat": -33.5719182, 
     "lng": 151.7193290 
    } 
    }, 
    "bounds": { 
    "southwest": { 
     "lat": -34.1692489, 
     "lng": 150.5022290 
    }, 
    "northeast": { 
     "lat": -33.4245980, 
     "lng": 151.3426361 
    } 
    } 
} 

}] }

Répondre

1

Le retour doit être jsonp pas JSON (JSON doit être enveloppé dans une fonction appel)

Essayez d'utiliser GM2 (qui ressemble à jsonp est pas pris en charge dans GM3)

$.ajax({ 
type: "GET", 
url: "http://maps.google.com/maps/geo?sensor=false&output=json&"+$.param({"q":address}), 
dataType: "jsonp", 
success: function(json){ 
    if (json.Status.code == "200") { 
     alert(":)"); 
    } else { 
     alert('wrong!'); 
    } 

} });

+1

Et il semblait, il n'y a pas d'option pour le faire via jsonp http://stackoverflow.com/questions/844341/google-geocode-via-http-callback-function – kirilloid