I would like to obtain the formatted_address from the json data returned from the following query string by using javascript.
http://maps.googleapis.com/maps/api/geocode/json?latlng=44.4647452,7.3553838&sensor=true
function getReverseGeocodingData(lat, lng) {
//use ajax and json
$.ajax({
type: "POST",
url: "http://maps.googleapis.com/maps/api/geocode/json?latlng="+sessionStorage.latitude+","+sessionStorage.longitude+"&sensor=true",
data: jsondata,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var address = response.Result;
var error = response.Error;
if (address != null) {
alert("Found current location: " + address.formatted_address);
sessionStorage.currentAddress = address.formatted_address;
return;
}
},
error: function (msg) {
errorConnection();
}
});
}
I tried getting formatted_address but it return undefined.
There are a few things to fix:
apparently you don't have to post data as you're making a
GET
request.I rewrote your code to take the parameters from
lat
andlng
in order to test it easily.response
is an object that contains an array ofresults
objects (lowercaser
plural). Each object contains aformatted_address
property.Most likely you'll want to fetch the first one:
response.results[0].formatted_address
To try it:
Outputs:
Found current location: Via Pasubio, 34, 12025 Dronero CN, Italia