gpt4 book ai didi

javascript - 谷歌地理编码地址分割

转载 作者:行者123 更新时间:2023-12-03 11:52:11 25 4
gpt4 key购买 nike

我对此很陌生,想知道如何获取完整的字符串,而不是像当前那样获取第一个字符,

当前使用来自 https://developers.google.com/maps/documentation/javascript/examples/geocoding-reverse 的 html + js 示例

function codeLatLng(callback) {
var input = document.getElementById('latlng').value;
var latlngStr = input.split(',', 2);
var lat = parseFloat(latlngStr[0]);
var lng = parseFloat(latlngStr[1]);
var latlng = new google.maps.LatLng(lat, lng);



geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {

if (results[1]) {
map.setZoom(17);
marker = new google.maps.Marker({
position: latlng,
map: map



});
infowindow.setContent(results[0].formatted_address);
infowindow.open(map, marker);
document.getElementById('Addressb').value= results[0].formatted_address; //working
document.getElementById('Strn').value = results[0].formatted_address[0];
document.getElementById('Strna').value = results[0].formatted_address[2];




} else {
alert('No results found');
}
} else {
alert('Geocoder failed due to: ' + status);
}
});

}



google.maps.event.addDomListener(window, 'load', initialize);



</script>

结果需要存储在指定的文本字段中,如下所示

Street Number: <input id="Strn" type="text"><br>
Street Name: <input id="Strna" type="text"><br>
Suburb: <input id="Subu" type="text"><br>
Town: <input id="Town" type="text"><br>
Code: <input id="Code1" type="text"><br>

最佳答案

您必须遍历地址组件,然后遍历类型,这是代码

  var streetNumber;
var streetName;
var city;
var state;
var zip;
var country;
for(var i = 0; i < results[0].address_components.length; i++){
for(var k = 0; k < results[0].address_components[i].types.length; k++){
if (results[0].address_components[i].types[k] == "street_number")
streetNumber = results[0].address_components[i].long_name;
else if (results[0].address_components[i].types[k] == "route")
streetName = results[0].address_components[i].short_name;
else if (results[0].address_components[i].types[k] == "locality")
city = results[0].address_components[i].long_name;
else if (results[0].address_components[i].types[k] == "administrative_area_level_1")
state = results[0].address_components[i].long_name;
else if (results[0].address_components[i].types[k] == "postal_code")
zip = results[0].address_components[i].long_name;
else if (results[0].address_components[i].types[k] == "country")
country = results[0].address_components[i].long_name;
}
}

关于javascript - 谷歌地理编码地址分割,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25774041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com