这是我的地方自动填充和搜索代码 .
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places">
</script>
<script>
var geocoder;
var map;
function initialize() {
var input = document.getElementById('address');
var options = {
componentRestrictions: {country: "in"}
};
var autocomplete = new google.maps.places.Autocomplete(input,options);
geocoder = new google.maps.Geocoder();
//var latlng = new google.maps.LatLng(18.52043030000, 73.85674369999);
var mapOptions = {
zoom: 15,
//center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
}
map = new google.maps.Map(document.getElementById('googleMap'), mapOptions);
}
function codeAddress() {
var address = document.getElementById('address').value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
这是我的HTML代码 .
<input id="address" type="textbox" size="30">
<input type="button" value="Search" onclick="codeAddress()">
它工作正常,但我不想用户单击按钮 . 当出现建议时,当用户选择任何建议选项时, Map 将必须导航到该位置 . 我该怎么做呢???
当用户从下拉列表中选择任何选项时, Map 应导航到该位置 .
3 回答
看一下this example和
place_changed
事件的实现 .这样,您可以将 Map 平移到自动完成结果的位置 .
这是我的工作代码..
现在,用户可以从下拉列表中选择选项, Map 将导航到该位置 . 并在搜索按钮上单击 .
Hope this might help you