2015-11-05 113 views
4

我在我的頁面上使用Google Maps API,頁面要求用戶填寫您的「當前地址」和「新地址」。谷歌地圖API自動完成第2個地址字段在同一頁

我可以得到自動完成工作的第一個地址,但它不工作的第二個地址,我已經做了大量的研究,看着在stackoverflow simulate帖子,但我找不到任何人有同樣的問題。

這是我的代碼;

<div id="locationField"> 
    <input id="autocomplete" placeholder="Start typing your address" onFocus="geolocate()" type="text"></input> 
</div> 

<div id="addressone"> 
    <input type="text" id="street_number" name="street_number"></input> 
    <input type="text" id="route" name="street_name"></input> 
    <input type="text" id="locality" name="town_city"></input> 
    <input type="text" id="postal_code" name="postcode"></input> 
    <input type="text" id="country" name="country"></input> 
</div> 

<div id="locationField2"> 
    <input id="autocomplete2" placeholder="Start typing your address" onFocus="geolocate()" type="text"></input> 
</div> 

<div id="addresstwo"> 
    <input type="text" id="street_number2" name="street_number2"></input> 
    <input type="text" id="route2" name="street_name2"></input> 
    <input type="text" id="locality2" name="town_city2"></input> 
    <input type="text" id="postal_code2" name="postcode2"></input> 
    <input type="text" id="country2" name="country2"></input> 
</div> 
<script> 

    // This example displays an address form, using the autocomplete feature 
    // of the Google Places API to help users fill in the information. 

    var placeSearch, autocomplete; 
    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 initAutocomplete() { 
     // Create the autocomplete object, restricting the search to geographical 
     // location types. 
     autocomplete = new google.maps.places.Autocomplete(
      /** @type {!HTMLInputElement} */(document.getElementById('autocomplete')), 
      {types: ['geocode']}); 

     // When the user selects an address from the dropdown, populate the address 
     // fields in the form. 
     autocomplete.addListener('place_changed', fillInAddress); 

    } 

    // [START region_fillform] 
    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; 
     } 
     } 
    } 
    // [END region_fillform] 

    // [START region_geolocation] 
    // 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 = { 
      lat: position.coords.latitude, 
      lng: position.coords.longitude 
      }; 
      var circle = new google.maps.Circle({ 
      center: geolocation, 
      radius: position.coords.accuracy 
      }); 
      autocomplete.setBounds(circle.getBounds()); 
     }); 
     } 
    } 
    // [END region_geolocation] 

</script> 
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyC7XIOPnu4WS_fBaIDPkCBdYa3MxdIcdK4&signed_in=true&libraries=places&callback=initAutocomplete" async defer></script> 
</div> 
+0

你有2個自動完成fieldds但只有代碼來處理第一。 – geocodezip

+0

那麼我只是複製並粘貼下面的js代碼? –

+0

我提供了昨天處理這個問題的幾個問題的鏈接,但是您似乎已經刪除了該版本的問題,但直接複製/粘貼不起作用。此外,發佈的代碼也不適用於第一種形式。 – geocodezip

回答

12

您需要掌握兩個自動完成輸入。這裏是fillInAddress廣義版本,將處理與字段的多個自動完成的對象與(在表格的第二個版本的「2」)獨特的擴展:

function fillInAddress(autocomplete, unique) { 
    // Get the place details from the autocomplete object. 
    var place = autocomplete.getPlace(); 

    for (var component in componentForm) { 
     if(!!document.getElementById(component + unique)){ 
      document.getElementById(component + unique).value = ''; 
      document.getElementById(component + unique).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] && document.getElementById(addressType + unique)) { 
      var val = place.address_components[i][componentForm[addressType]]; 
      document.getElementById(addressType + unique).value = val; 
     } 
    } 
} 

這樣稱呼它:

// Create the autocomplete object, restricting the search to geographical 
// location types. 
autocomplete = new google.maps.places.Autocomplete(
/** @type {!HTMLInputElement} */ 
(document.getElementById('autocomplete')), { 
    types: ['geocode'] 
}); 

// When the user selects an address from the dropdown, populate the address 
// fields in the form. 
autocomplete.addListener('place_changed', function() { 
    fillInAddress(autocomplete, ""); 
}); 

// Create the second autocomplete object, restricting the search to geographical 
// location types. 
autocomplete2 = new google.maps.places.Autocomplete(
/** @type {!HTMLInputElement} */ 
(document.getElementById('autocomplete2')), { 
    types: ['geocode'] 
}); 
autocomplete2.addListener('place_changed', function() { 
    fillInAddress(autocomplete2, "2"); 
}); 

工作代碼片段:

// This example displays an address form, using the autocomplete feature 
 
// of the Google Places API to help users fill in the information. 
 

 
var placeSearch, autocomplete, autocomplete2; 
 
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 initAutocomplete() { 
 
    // Create the autocomplete object, restricting the search to geographical 
 
    // location types. 
 
    autocomplete = new google.maps.places.Autocomplete(
 
    /** @type {!HTMLInputElement} */ 
 
    (document.getElementById('autocomplete')), { 
 
     types: ['geocode'] 
 
    }); 
 

 
    // When the user selects an address from the dropdown, populate the address 
 
    // fields in the form. 
 
    autocomplete.addListener('place_changed', function() { 
 
    fillInAddress(autocomplete, ""); 
 
    }); 
 

 
    autocomplete2 = new google.maps.places.Autocomplete(
 
    /** @type {!HTMLInputElement} */ 
 
    (document.getElementById('autocomplete2')), { 
 
     types: ['geocode'] 
 
    }); 
 
    autocomplete2.addListener('place_changed', function() { 
 
    fillInAddress(autocomplete2, "2"); 
 
    }); 
 

 
} 
 

 
function fillInAddress(autocomplete, unique) { 
 
    // Get the place details from the autocomplete object. 
 
    var place = autocomplete.getPlace(); 
 

 
    for (var component in componentForm) { 
 
    if (!!document.getElementById(component + unique)) { 
 
     document.getElementById(component + unique).value = ''; 
 
     document.getElementById(component + unique).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] && document.getElementById(addressType + unique)) { 
 
     var val = place.address_components[i][componentForm[addressType]]; 
 
     document.getElementById(addressType + unique).value = val; 
 
    } 
 
    } 
 
} 
 
google.maps.event.addDomListener(window, "load", initAutocomplete); 
 

 
function geolocate() { 
 
    if (navigator.geolocation) { 
 
    navigator.geolocation.getCurrentPosition(function(position) { 
 
     var geolocation = { 
 
     lat: position.coords.latitude, 
 
     lng: position.coords.longitude 
 
     }; 
 
     var circle = new google.maps.Circle({ 
 
     center: geolocation, 
 
     radius: position.coords.accuracy 
 
     }); 
 
     autocomplete.setBounds(circle.getBounds()); 
 
    }); 
 
    } 
 
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script> 
 
<div id="locationField"> 
 
    <input id="autocomplete" placeholder="Start typing your address" onFocus="geolocate()" type="text" /> 
 
</div> 
 
<div id="addressone"> 
 
    <input type="text" id="street_number" name="street_number" /> 
 
    <input type="text" id="route" name="street_name" /> 
 
    <input type="text" id="locality" name="town_city" /> 
 
    <input type="text" id="administrative_area_level_1" name="administrative_area_level_1" /> 
 
    <input type="text" id="postal_code" name="postcode" /> 
 
    <input type="text" id="country" name="country" /> 
 
</div> 
 
<div id="locationField2"> 
 
    <input id="autocomplete2" placeholder="Start typing your address" onFocus="geolocate()" type="text" /> 
 
</div> 
 
<div id="addresstwo"> 
 
    <input type="text" id="street_number2" name="street_number2" /> 
 
    <input type="text" id="route2" name="street_name2" /> 
 
    <input type="text" id="locality2" name="town_city2" /> 
 
    <input type="text" id="administrative_area_level_12" name="administrative_area_level_12" /> 
 
    <input type="text" id="postal_code2" name="postcode2" /> 
 
    <input type="text" id="country2" name="country2" /> 
 
</div>

+0

它工作完美。非常感謝。這必須被接受。 –

0

這是一個古老的問題,但我想我會在我的解決方案上發佈以防萬一以後對其他人有所幫助。我的解決方案可處理頁面上任意數量的地圖api自動填充字段。你需要用類名來標記所有的自動填充字段,在我的例子中,我使用了類名「address」。

現在搶字段集合:

它調用了一個名爲「的addListener」功能

function addListener(el) { 
    var autocomplete = new google.maps.places.Autocomplete(el); 

    google.maps.event.addListener(autocomplete, 'place_changed', function() { 
// Do whatever you want in here e.g. 
// var place = autocomplete.getPlace(); 
    }); 
} 

因爲它是一個集合,你應該能夠任意數量的自動完成字段添加到頁面。雖然自然,你應該檢查集合中的未定義,以防萬一你在頁面上沒有自動填充字段。

0

這就像一個魅力:)

謝謝jQuery。

function initMultiComplete() { 
    jQuery('.maps-complete').each(function(){ 

    var id = jQuery(this).prop('id'); 
    var $this = jQuery(this); 
    var parent = jQuery(this).parent('div'); 

    var jautocomplete = new google.maps.places.Autocomplete(document.getElementById(id), {types: ['geocode']}); 

    jautocomplete.addListener('place_changed', function() { 

     var place = jautocomplete.getPlace(); 
     var address = $this.val(); 
     var lat = place.geometry.location.lat(); 
     var lng = place.geometry.location.lng(); 

     jQuery('.maps-autocomplete-lat', parent).val(lat); 
     jQuery('.maps-autocomplete-lng', parent).val(lng); 

    }); 

}); 
} 

腳本調用initMultiComplete回調,並加載W /異步延遲:

https://maps.googleapis.com/maps/api/js?key=YOUR_KEY&libraries=places&callback=initMultiComplete 
相關問題