我从Google收到了用于自动填写地址字段的代码。它对于一种“输入类型=文本”效果很好。我将其用于“出发”
问题是当我重复这些代码以使第二个“输入类型=文本”作为“到达”时:它仅适用于其中一个。
我试图更改输入id =“ autocomparr”和一些变量,但未成功。
致谢
<input id="autocompdep"
placeholder="Enter your address"
onFocus="geolocate()"
type="text"/>
<script>
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'
};
var options = {
componentRestrictions: {country: 'fr'}
};
function initAutocomplete() {
// Create the autocomplete object, restricting the search predictions to
// geographical location types.
autocomplete = new google.maps.places.Autocomplete(
document.getElementById('autocompdep'), options, {types: ['geocode']});
// Avoid paying for data that you don't need by restricting the set of
// place fields that are returned to just the address components.
autocomplete.setFields(['address_component']);
// When the user selects an address from the drop-down, populate the
// address fields in the form.
autocomplete.addListener('place_changed', 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 then fill-in 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 = {
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>
<script src="https://maps.googleapis.com/maps/api/js?key=***&libraries=places&callback=initAutocomplete"
async defer></script>````
答案 0 :(得分:0)
对我自己(以及其他有此问题的人)的答案
它现在适用于同一页面上的2个输入文本字段。 Geolococate可以在法国和法国里维埃拉使用,以满足我的需求。
致谢
<html>
<head>
<script src="https://maps.googleapis.com/maps/api/js?key=****&sensor=true&libraries=places"></script>
</head>
<body>
<label for="locationTextField1">Departure</label>
<input id="locationTextField1" type="text" size="50" >
<script>
var defaultBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(43.093595, 6.153839),
new google.maps.LatLng(43.467646, 6.237595));
var options = {
bounds: defaultBounds,
componentRestrictions: {country: 'fr'}
};
function initDep() {
var autocomplete = new google.maps.places.Autocomplete(
document.getElementById('locationTextField1'), options, {types: ['geocode']});
}
google.maps.event.addDomListener(window, 'load', initDep);
</script>
<br />
<br />
<label for="locationTextField2">Arrival</label>
<input id="locationTextField2" type="text" size="50">
<script>
function initArr() {
var autocomplete = new google.maps.places.Autocomplete(
document.getElementById('locationTextField2'), options, {types: ['geocode']});
}
google.maps.event.addDomListener(window, 'load', initArr);
</script>
<script>
</script>
</body>
</html>