现在在我的输入中我打印街道,街道号码,州,国家.. 如何仅打印街道和街道号码? 我的代码:
var options = {
types: ['geocode']
};
var input = document.getElementById('address');
autocomplete = new google.maps.places.Autocomplete(input, options);
答案 0 :(得分:0)
您可以通过在选择地点后覆盖输入的值来解决此问题。
var input = document.getElementById("address");
var autocomplete = new google.maps.places.Autocomplete(input, {types: ["geocode"]});
autocomplete.addListener("place_changed", function() {
var placeResult = autocomplete.getPlace();
var addressComponents = placeResult.address_components;
var street = "";
var number = "";
// loop through addressComponents and find the route and street_number types
for (var i = 0, i_len = addressComponents.length; i < i_len; i++) {
var addressType = addressComponents[i].types[0];
switch(addressType) {
case "route":
street = addressComponents[i]["long_name"];
break;
case "street_number":
number = addressComponents[i]["long_name"];
break;
}
}
input.value = number + " " + street;
});
我最近创建了一个jQuery plugin,很容易让您这样做。这将是您的新代码:
$("#address").geocomplete({
fields: {
"#address" : "street address"
}
});
该插件允许您根据所选位置的结果自动填充任意数量的字段。它还添加了自定义样式,提供了回调并修复了烦人的scrolling issue。