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 autosuggest スクリプトを有効にするには、検索ボタンをクリックする必要があります

4

1 に答える 1

0

GLOBAL と同様current_placeに、関数の外部にアクセスできます

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 に答える