反向地理编码为 return 完整格式地址

Reverse Gecoding to return full formatted address

我正在使用反向地理编码来读取坐标并将其转换为街道地址。 我的代码有效,但我的问题是,只有 returns 郊区和城镇才能以 returns 完整地址号码 - 街道 - 郊区 - 城镇 - 国家的方式对其进行格式化。

 // Read coordinates and convert to Readible Address
function codeLatLng() {
geocoder = new google.maps.Geocoder();
//var input = document.getElementById('latlng').value;
//var latlngStr = input.split(',', 2);
var lat = parseFloat(latitude);
var lng = parseFloat(longitude);
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({ 'latLng': latlng }, function (results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        if (results[1]) {
                       $("#txtCurrentAdd").val(results[1].formatted_address)

            //infowindow.setContent(results[1].formatted_address);
            //infowindow.open(map, marker);
        } else {
            alert('No results found');
        }
    } else {
        alert('Geocoder failed due to: ' + status);
    }
});
}

您是否尝试过将结果[1]更改为结果[0]?来自 Google Geocode Docs 文档:

results[0].formatted_address: "275-291 Bedford Ave, Brooklyn, NY 11211, USA",
results[1].formatted_address: "Williamsburg, NY, USA",
results[2].formatted_address: "New York 11211, USA",
results[3].formatted_address: "Kings, New York, USA",
results[4].formatted_address: "Brooklyn, New York, USA",
results[5].formatted_address: "New York, New York, USA",
results[6].formatted_address: "New York, USA",
results[7].formatted_address: "United States"