get formatted_address from google maps api json

30,699

Solution 1

Here's the corrected JS..

Demo Fiddle

var latlng = "55.397563, 10.39870099999996";
var url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + latlng + "&sensor=false";
$.getJSON(url, function (data) {
    for(var i=0;i<data.results.length;i++) {
        var adress = data.results[i].formatted_address;
        alert(adress);
    }
});

Solution 2

data["results"][0]["formatted_address"]
Share:
30,699
lizart
Author by

lizart

Updated on February 20, 2020

Comments

  • lizart
    lizart about 4 years

    I want to get the formatted_adress from the json array. an example link could be http://maps.googleapis.com/maps/api/geocode/json?latlng=55.397563, 10.39870099999996&sensor=false

    var url = "http://maps.googleapis.com/maps/api/geocode/json?latlng="+latlng+"&sensor=false";
                    $.getJSON(url, function(data) {
                        var adress = data['formatted_address'];
                        alert(adress);
                    });
    

    but im getting "undefined"

  • lizart
    lizart over 10 years
    thank you, this solved my problem, ill accpect the answer in 4mins.
  • Daniel Cheeseman
    Daniel Cheeseman over 9 years
    Thanks for this.. Just added to my code and worked perfectly!
  • Toniq
    Toniq about 7 years
    Is there a rule on how much is the length of formatted_address if I want to get specific address format?
  • Nikhil N
    Nikhil N about 7 years
    @Toniq not sure about the current version of api. You should check out latest documentation.