我使用Places库自动完成地址输入.搜索仅限于一个城市,我得到如下输出:
"Rossiya,Moskva,Leninskiy prospekt 28"
如何隐藏"Rossiya,Moskva"?...
我的查询:
function() {
// Search bounds
var p1 = new google.maps.LatLng(54.686534, 35.463867);
var p2 = new google.maps.LatLng(56.926993, 39.506836);
self.options = {
bounds : new google.maps.LatLngBounds(p1, p2),
componentRestrictions: {country: 'ru'},
};
var elements = document.querySelectorAll('.address');
for ( var i = 0; i < elements.length; i++) {
var autocomplete = new google.maps.places.Autocomplete(elements[i],
self.options);
}
Run Code Online (Sandbox Code Playgroud)
小智 6
您可以,但必须在两个位置替换输入字段的值。
例子:
var autocomplete = new google.maps.places.Autocomplete(input, placesOptions);
var input = document.getElementById('searchTextField');
Run Code Online (Sandbox Code Playgroud)
在“place_changed”事件中,您需要执行以下操作:
placeResult = autocomplete.getPlace();
//This will get only the address
input.value = placeResult.name;
Run Code Online (Sandbox Code Playgroud)
这会将搜索文本字段中的值更改为街道地址。
第二个地方有点棘手:
input.addEventListener('blur', function(){
// timeoutfunction allows to force the autocomplete field to only display the street name.
if(placeResult){ setTimeout(function(){ input.value = placeResult.name; }, 1); } });
Run Code Online (Sandbox Code Playgroud)
我们必须这样做的原因是,如果您只添加模糊事件侦听器,谷歌地方将使用完整地址填充输入字段,因此您必须“等待”谷歌更新,然后通过等待强制进行更改几毫秒。
尝试不使用 setTimeout 函数,您就会明白我的意思。
| 归档时间: |
|
| 查看次数: |
14729 次 |
| 最近记录: |