以下是我当前的代码:http://jsfiddle.net/9B84H/1/
var current_place;
function autosuggest() {
var input = document.getElementById('location');
var options = { types: [], };
var autocomplete = new google.maps.places.Autocomplete(input, options);
google.maps.event.addListener(autocomplete, 'place_changed', function() {
var place = autocomplete.getPlace();
if (!place.geometry) {
current_place = null;
alert('Cannot find place');
return;
} else {
current_place = place;
alert('Place is at:' + place.geometry.location);
}
});
}
单击autosuggest中的选项时,它可以正常工作。但是,如果您只是输入内容并单击搜索,我需要它来运行与从autosuggest中选择选项时相同的地理编码操作。我该怎么做?
P.s您必须单击搜索按钮才能激活自动提示脚本
答案 0 :(得分:0)
由于current_place
是GLOBAL,因此可以在函数外部访问
var current_place;
function autosuggest() {
var input = document.getElementById('location');
var options = {
types: [],
};
var autocomplete = new google.maps.places.Autocomplete(input, options);
google.maps.event.addListener(autocomplete, 'place_changed', function() {
var place = autocomplete.getPlace();
if (!place.geometry) {
current_place = null;
alert('Cannot find place');
return;
} else {
current_place = place;
alert('Place is at:' + place.geometry.location);
doIt();
}
});
}
function doIt(){
alert(current_place.geometry.location);
}
请参阅JSFiddle