0

这是我当前的代码: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);
        }
    });

}

当您单击自动建议中的选项时,它会完美运行。但是,如果您只是输入一些内容并单击搜索,我需要它来运行与从自动建议中选择一个选项时相同的地理编码操作。我该怎么做呢?

Ps 你必须点击搜索按钮才能激活自动提示脚本

4

1 回答 1

0

current_placeGLOBAL 一样,它可以在函数外部访问

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

于 2012-11-04T23:45:17.903 回答