Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get formatted_address from google maps api json

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"

like image 569
lizart Avatar asked Oct 16 '13 08:10

lizart


People also ask

How do I get data from Google Maps API?

Go to APIs & Services → Dashboard → Enable APIs & Services at the top and Choose Maps Javascript API from the API Library. This will open up the Map JavaScript API page, and Enable it.

How do I get latitude and longitude from Google Maps API?

That's right, you can go straight to the simplest page on the Internet—google.com—and enter your latitude and longitude into the search box. Usually coordinates are listed with latitude first, then longitude. Double check that is the case and that you've included a comma between the numbers.

How do I get a city from places API?

2) Make another web-service call to https://maps.googleapis.com/maps/api/place/details/json?key=API_KEY&placeid=place_id_retrieved_in_step_1. This will return a JSON which contains address_components . Looping through the types to find locality and postal_code can give you the city name and postal code.


2 Answers

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);
    }
});
like image 67
nik Avatar answered Oct 30 '22 08:10

nik


data["results"][0]["formatted_address"]
like image 34
Wellington Alves Avatar answered Oct 30 '22 08:10

Wellington Alves