jQuery 从谷歌地图 api json 获取 formatted_address

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19398702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 23:52:27  来源:igfitidea点击:

get formatted_address from google maps api json

javascriptjqueryjsongoogle-mapsgeocoding

提问by lizart

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

我想从 json 数组中获取 formatted_adress。一个示例链接可能是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"

但我越来越“未定义”

回答by nik

Here's the corrected JS..

这是更正后的 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);
    }
});

回答by Wellington Alves

data["results"][0]["formatted_address"]