我對我工作的一個網站兩個HTML頁面。第一頁發生在用戶輸入(起點和終點的位置),然後傳遞信息到谷歌地圖API的Javascript在爲了確定兩個地點之間的距離。如何維護用戶輸入調用window.history.back()時?
第二頁顯示該信息的用戶。
不過,我也有一個叫Edit
按鈕,調用onclick="window.history.back()"
。
我遇到的問題是,這兩個部分用戶輸入也使用了谷歌的自動完成地址,所以當我去到下一個頁面,點擊Edit
按鈕,用戶輸入從輸入框中刪除,而沒有Google自動填充功能,它仍然保留在該位置。我假設這個問題存在於Google Autocomplete本身中,但我該如何解決這個問題?
這裏是JavaScript的谷歌自動完成:
google.maps.event.addDomListener(window, 'load', initialize);
// ==========================================================================================================
// ==========================================================================================================
// ==========================================================================================================
// USES THE GOOGLE PLACES LIBRARY
// ==============================
// This example displays an address form, using the autocomplete feature
// of the Google Places API to help users fill in the information.
var placeSearch, autoCompleteOrigin, autoCompleteDest;
var componentForm = {
street_number: 'short_name',
route: 'long_name',
locality: 'long_name',
administrative_area_level_1: 'short_name',
country: 'long_name',
postal_code: 'short_name'
};
function initialize() {
// Create the autocomplete object, restricting the search
// to geographical location types.
autoCompleteOrigin = new google.maps.places.Autocomplete(
/** @type {HTMLInputElement} */(document.getElementById('start')),
{ types: ['geocode'] });
autoCompleteDest = new google.maps.places.Autocomplete(
/** @type {HTMLInputElement} */(document.getElementById('destination')),
{ types: ['geocode'] });
// When the user selects an address from the dropdown,
// populate the address fields in the form.
google.maps.event.addListener(autoCompleteOrigin, 'place_changed', function() {
fillInAddress();
});
google.maps.event.addListener(autoCompleteDest, 'place_changed', function() {
fillInAddress();
});
}
function fillInAddress() {
// Get the place details from the autocomplete object.
var place = autocomplete.getPlace();
for (var component in componentForm) {
document.getElementById(component).value = '';
document.getElementById(component).disabled = false;
}
// Get each component of the address from the place details
// and fill the corresponding field on the form.
for (var i = 0; i < place.address_components.length; i++) {
var addressType = place.address_components[i].types[0];
if (componentForm[addressType]) {
var val = place.address_components[i][componentForm[addressType]];
document.getElementById(addressType).value = val;
}
}
}
// Bias the autocomplete object to the user's geographical location,
// as supplied by the browser's 'navigator.geolocation' object.
function geolocate() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var geolocation = new google.maps.LatLng(
position.coords.latitude, position.coords.longitude);
var circle = new google.maps.Circle({
center: geolocation,
radius: position.coords.accuracy
});
autocomplete.setBounds(circle.getBounds());
});
}
}
這可能是因爲谷歌的自動完成API使用'自動完成= 「關閉」'在它們的輸入標籤的屬性。試着將這個添加到'componentForm'中的你自己的輸入中,以檢查它們是否停止工作。 –
@ErsinBasaran你能詳細說明一下嗎? – freddiev4
嘗試'window.history.back(0)' – Dimple