我有一個小表單,當前使用按鈕從Google Maps API獲取經度和緯度,並將結果放入輸入字段。觸發器是按鈕上的事件偵聽器。我想擺脫按鈕,並在城市添加後觸發事件。我不確定這是如何完成的。 這是我在JS小號手中的代碼。 enter link description here我想在輸入字段輸入後運行函數
function showResult(result) {
document.getElementById('latitude').value = result.geometry.location.lat() +' '+ result.geometry.location.lng();
}
function getLatitudeLongitude(callback, address) {
// If adress is not supplied, use default value 'Ferrol, Galicia, Spain'
address = address || 'Ferrol, Galicia, Spain';
// Initialize the Geocoder
geocoder = new google.maps.Geocoder();
if (geocoder) {
geocoder.geocode({
'address': address
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
callback(results[0]);
}
});
}
}
var button = document.getElementById('btn');
button.addEventListener("click", function() {
var address = document.getElementById('street').value +' '+ document.getElementById('city').value;
getLatitudeLongitude(showResult, address)
});
嘗試google maps autocomplete places api。 – RemyaJ