自动完成功能可在多个静态输入字段上完美运行。但是当我通过按钮添加输入字段时,自动完成功能对这些输入字段不起作用。也许问题是关闭,但我不确定,因为我的弱Javascript。任何人都可以帮助我吗?
以下是代码:
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script>
<script>
var map = null;
var marker = [];
var autocomplete = [];
var inputs = [];
var types = [];
var autocompleteOptions = {
componentRestrictions: {country: "az"}
};
此函数动态创建最多5个输入字段(我将此函数放在initialize方法中,并且它也不起作用):
$(document).ready(function(){
var count = 2;
$("#addInputField").click(function(){
count++;
$("#inputlar").append("<input id='pac-input" + count + "' class='controlsInput' type='text' placeholder='Enter your destination' /> <br />");
var newInput = [];
var newEl = document.getElementById('pac-input' + count);
newInput[count-1] = newEl;
setupAutocomplete(autocomplete, newInput, count-1);
if (count === 7) {
$("#addInputField").remove();
}
});
});
从初始化方法调用此函数,以便在多个输入上维护自动完成。完美运作:
function setupAutocomplete(autocomplete,inputs,i) {
autocomplete[i] = new google.maps.places.Autocomplete(inputs[i], autocompleteOptions);
autocomplete[i].bindTo('bounds', map);
google.maps.event.addListener(autocomplete[i], 'place_changed', function() {
var infowindow = new google.maps.InfoWindow();
if (marker[i] && marker[i].setMap) {
marker[i].setMap(null);
marker[i] = null;
}
marker[i] = new google.maps.Marker({
map: map
});
infowindow.close();
marker[i].setVisible(false);
var place = autocomplete[i].getPlace();
if (!place.geometry) {
return;
}
// If the place has a geometry, then present it on a map.
if (place.geometry.viewport) {
map.fitBounds(place.geometry.viewport);
} else {
map.setCenter(place.geometry.location);
map.setZoom(17); // Why 17? Because it looks good.
}
marker[i].setPosition(place.geometry.location);
marker[i].setVisible(true);
var address = '';
if (place.address_components) {
address = [
(place.address_components[0] && place.address_components[0].short_name || ''),
(place.address_components[1] && place.address_components[1].short_name || ''),
(place.address_components[2] && place.address_components[2].short_name || '')
].join(' ');
}
infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
infowindow.open(map, marker[i]);
});
}
最终初始化方法:
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(40.4700, 50.0000),
zoom: 10,
zoomControl:true,
zoomControlOptions: {
style:google.maps.ZoomControlStyle.SMALL
},
mapTypeControl:true,
mapTypeControlOptions: {
style:google.maps.MapTypeControlStyle.DROPDOWN_MENU
}
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
types = document.getElementById('type-selector');
map.controls[google.maps.ControlPosition.TOP_LEFT].push(types);
inputs = document.getElementsByClassName("controlsInput");
for(var i = 0; i < inputs.length; i++) {
setupAutocomplete(autocomplete,inputs,i);
}
// Sets a listener on a radio button to change the filter type on Places
// Autocomplete.
function setupClickListener(id, types) {
var radioButton = document.getElementById(id);
google.maps.event.addDomListener(radioButton, 'click', function() {
for (var i=0 ; i<autocomplete.length; i++) {
autocomplete[i].setTypes(types);
}
});
}
setupClickListener('changetype-all', []);
setupClickListener('changetype-establishment', ['establishment']);
setupClickListener('changetype-geocode', ['geocode']);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
HTML:
<body>
<table>
<tr>
<td>
<div>
<div id="inputlar">
<input id="pac-input" class="controlsInput" type="text" placeholder="Marşrutunuzu haradan başlayırsınız" /> <br />
<input id="pac-input2" class="controlsInput" type="text" placeholder="Haraya gedirsiniz"> <br />
</div> <br />
<input id="addInputField" type="button" value="Yolüstü dayanacaq əlavə et" class="styled-button-10"/>
<div hidden="hidden" id="type-selector" class="controls">
<input type="radio" name="type" id="changetype-all" checked="checked">
<label for="changetype-all">All</label>
<input type="radio" name="type" id="changetype-establishment">
<label for="changetype-establishment">Establishments</label>
<input type="radio" name="type" id="changetype-geocode">
<label for="changetype-geocode">Geocodes</label>
</div>
</div>
</td>
<td>
<div id="map-canvas" style="width:540px;height:380px;"></div>
</td>
</tr>
</table>
</body>
</html>
答案 0 :(得分:6)
问题是在添加新元素后不要调用setupAutocomplete()
。你必须做这样的事情(可以使用jQuery进行改进):
$(document).ready(function(){
var count = 2;
$("#addInputField").click(function(){
count++;
console.log('add new input field');
$("#inputlar").append("<input id='pac-input" + count + "' class='controlsInput' type='text' placeholder='Enter your destination' /> <br />");
// added
var newInput = [];
var newEl = document.getElementById('pac-input' + count);
newInput.push(newEl);
setupAutocomplete(autocomplete, newInput, 0);
if (count === 7) {
$("#addInputField").remove();
}
});
});
更新: example at jsbin。 setupAutocomplete()
必须更改一些注释后的代码,以查看差异,idx
代替参数i
。
function setupAutocomplete(autocomplete, inputs, i) {
// autocomplete[i] = new google.maps.places.Autocomplete(inputs[i], autocompleteOptions);
autocomplete.push(new google.maps.places.Autocomplete(inputs[i], autocompleteOptions));
var idx = autocomplete.length - 1;
//autocomplete[i].bindTo('bounds', map);
autocomplete[idx].bindTo('bounds', map);
//google.maps.event.addListener(autocomplete[i], 'place_changed', function() {
google.maps.event.addListener(autocomplete[idx], 'place_changed', function() {
...
注意:我没有检查当您达到7个自动完成输入时会发生什么。也许你必须做一些更新。
答案 1 :(得分:3)
我开发了一个脚本,可以为开始位置,结束位置以及路标提供Google地方搜索输入,如果您尝试一次,它就很漂亮了。 这是代码。下载并在任何浏览器中运行并享受..!