Javascript 谷歌地图自动完成搜索框不工作
我正在尝试添加一个搜索框自动完成,这将使谷歌地图去的位置在搜索框中。到目前为止,还没有多少运气弄清楚它为什么不起作用 -----HTMLJavascript 谷歌地图自动完成搜索框不工作,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我正在尝试添加一个搜索框自动完成,这将使谷歌地图去的位置在搜索框中。到目前为止,还没有多少运气弄清楚它为什么不起作用 -----HTML <div class="input-group location" > <input type="text" id="location" placeholder="Enter Location"> <span class="input-group-addon"><i class="f
<div class="input-group location" >
<input type="text" id="location" placeholder="Enter Location">
<span class="input-group-addon"><i class="fa fa-map-marker geolocation" data-toggle="tooltip" data-placement="bottom" title="Find my position"></i></span>
</div>
您的代码正在工作,唯一的问题是您得到的输入元素,您使用了标记来获得输入,这是您的工作代码 需要改变的是 var input=document.getElementById'location'//删除标签
var input = document.getElementById('#location') ;
var autocomplete = new google.maps.places.Autocomplete(input, {
types: ["geocode"]
});
autocomplete.bindTo('bounds', map);
google.maps.event.addListener(autocomplete, 'place_changed', function()
{
var place = autocomplete.getPlace();
if (!place.geometry) {
return;
}
if (place.geometry.viewport) {
map.fitBounds(place.geometry.viewport);
map.setZoom(14);
} else {
map.setCenter(place.geometry.location);
map.setZoom(14);
}
marker.setPosition(place.geometry.location);
//marker.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(' ');
}
});