使用Google Maps v3自动填充表单时,如何使用javascript清除搜索输入字段

时间:2017-10-04 14:30:20

标签: javascript google-maps-api-3 autocomplete

我正在使用Google maps v3 api自动填写地址表单,但我不想使用示例中的常规搜索字段(https://google-developers.appspot.com/maps/documentation/javascript/examples/full/places-autocomplete-addressform)。

我正在尝试将街道地址字段设为搜索字段以及仅街道编号和街道名称字段。我在JS小提琴上找到了这个例子(https://jsfiddle.net/smartystreets/0c9fy73v/)我无法复制它。

我尝试将autocomplete =关闭但不适用于chrome(对于elementId('autocomplete'))

我尝试隐藏路径和街道输入元素,并将输入ID值设置为空。

document.getElementById('autocomplete').value = ''; 

然后使用隐藏的输入字段重建该表单输入w /

document.getElementById('autocomplete').value = document.getElementById('street_number') + ' ' + document.getElementById('route');

任何想法我做错了什么?为什么搜索字段不清楚?

这是我的搜索/地址字段的HTML:

<!-- address -->
   <div class="col-md-6 col-lg-6">
      <label class="light text-capitalize">address:</label>
      <input type="text"   id="autocomplete"
         name="street" placeholder="Street address, PO Box, etc.">
      <input type="hidden" id="route" disabled="false">
      <input type="hidden" id="street_number" disabled="false"> 
      <input type="text"  name="address1" placeholder="(Optional) Apartment, Suite, Floor, etc.">
   </div>
<!-- address end -->

这是我的内联JS

  var placeSearch, autocomplete;
  var componentForm = {
     street_number: 'short_name',
     route: 'long_name',
     locality: 'long_name',
     administrative_area_level_1: 'short_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);
  }

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


    for (var component in componentForm) {

    document.getElementById('street_number').value = '';
    document.getElementById('route').value = '';
    document.getElementById('locality').value = '';
    document.getElementById('administrative_area_level_1').value = '';
//  document.getElementById('country').value = '';
    document.getElementById('postal_code').value = '';

    }

    // 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 = {
          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>

1 个答案:

答案 0 :(得分:0)

如果您确实想在用户点击自动填充中的地址后清除搜索输入字段,请尝试在输入地点后将输入(id = autocomplete)值设置为空。

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

    /* Your other code */

    document.getElementById('autocomplete').value = '';
}

我刚做了一个示范,让你检查行为。你可以在http://jsbin.com/mugasav/1/edit?html,js,output

看到它