jQuery JSON parse google maps problem
I'm trying to get json response from google maps using this code:
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!');
}
}
});
It's working really good, but...
as a result i'm getting
Uncaught SyntaxError: Unexpected token : in Chrome and Error: no element found in FireFox..
it's seems that something wrong is with returned JSON, but it looking good:
{
"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,
开发者_StackOverflow中文版"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
}
}
}
} ] }
The return must be jsonp not json (the json should be wrapped into a function call)
Try using GM2 (looks like jsonp is not supported in 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!');
}
} });
精彩评论