反向地理编码以返回完整格式化的地址

时间:2015-01-01 14:32:52

标签: javascript google-maps geolocation geocoding reverse-geocoding

我使用反向geococoding读取坐标并将其转换为街道地址。 我的代码有效但我的问题是它只返回郊区和城镇是否可以格式化它以返回完整的地址编号 - 街道 - 郊区 - 城镇 - 国家。

 // 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 :(得分:2)

您是否尝试更改结果[1]的结果[1]?来自Google Geocode Docs 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"