【问题标题】:Autocomplete for Two <Input>两个 <Input> 的自动完成
【发布时间】:2019-05-21 20:24:51
【问题描述】:

我从 Google 获得了用于自动完成地址字段的代码。它适用于一种“输入类型=文本”。我用它来“出发”

问题是当我复制这些代码以将第二个“输入类型=文本”作为“到达”时:它只适用于其中一个。

我尝试更改输入 id="autocomparr" 和一些 var 但没有成功。

问候

             <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>````

【问题讨论】:

    标签: forms googleplacesautocomplete


    【解决方案1】:

    回答我自己(以及其他有此问题的人)

    它现在适用于同一页面上的 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-07
      • 2012-01-19
      相关资源
      最近更新 更多