"initAutocomplete is not a function"
用张贴的代码(
fiddle
InitAutocomplete
是jquery运行的匿名函数的本地
ready
函数,但从未在那里调用。
fiddle
):
<div id="map-container2" class="">
<input id="pac-input" class="controls" type="text" placeholder="Ville - Recherche">
<div id="map" class="map2"></div>
</div>
<script>
function initAutocomplete() {
var map = new google.maps.Map(document.getElementById('map'), {});
// Create the search box and link it to the UI element.
var input = document.getElementById('pac-input');
alert(input);
var searchBox = new google.maps.places.SearchBox(input);
alert(searchBox);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
alert("1");
// Bias the SearchBox results towards current map's viewport.
map.addListener('bounds_changed', function() {
searchBox.setBounds(map.getBounds());
});
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?libraries=places&callback=initAutocomplete"></script>
&callback=initAutocomplete
从脚本include中,只需在jquery中使用它
准备好的
函数,并在那里调用它(
fiddle
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>
<div id="map-container2" class="">
<input id="pac-input" class="controls" type="text" placeholder="Ville - Recherche">
<div id="map" class="map2"></div>
</div>
<script>
$(document).ready(function() {
function initAutocomplete() {
var map = new google.maps.Map(document.getElementById('map'), {
});
// Create the search box and link it to the UI element.
var input = document.getElementById('pac-input');
alert(input);
var searchBox = new google.maps.places.SearchBox(input);
alert(searchBox);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
alert("1");
// Bias the SearchBox results towards current map's viewport.
map.addListener('bounds_changed', function() {
searchBox.setBounds(map.getBounds());
});
}
initAutocomplete();
});
</script>