Javascript - 谷歌地图 - 自动填充

时间:2016-06-15 20:49:56

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

我有一个表格,我正在整理,我试图让它自动填充我的字段。我已经取得了95%的成功,但是,我很难将Lat / Long绳索自动填入适当的表格区域。

这是我的HTML

            <div class="control-group">
            <label class="control-label">Address *</label>
                <div class="controls">
                    <input value="" placeholder="Enter the customers full address" class="form-control" id="autocomplete" type="text">
                </div>
        </div>
        <div class="control-group">
            <label class="control-label">House Number</label>
                <div class="controls">
                    <input value="" class="form-control" id="street_number" disabled="true">
                </div>
        </div>
        <div class="control-group">
            <label class="control-label">Street Address</label>
                <div class="controls">                
                    <input value="" class="form-control" id="route" disabled="true">
                </div>
        </div>
        <div class="control-group">
            <label class="control-label">City</label>
                <div class="controls">
                    <input value="" class="form-control" id="locality" disabled="true">
                </div>
        </div>
        <div class="control-group">
            <label class="control-label">State</label>
                <div class="controls">
                    <input value="" class="form-control" id="administrative_area_level_1" disabled="true">
                </div>
        </div>      
        <div class="control-group">
            <label class="control-label">Zipcode</label>
                <div class="controls">
                    <input value="" class="form-control" id="postal_code" disabled="true">
                </div>
        </div>
        <div class="control-group">
            <label class="control-label">Country</label>
                <div class="controls">
                    <input value="" class="form-control" id="country" disabled="true">  
                </div>
        </div>
        <div class="control-group">
            <label class="control-label">Lat</label>
                <div class="controls">
                    <input value="" class="form-control" id="LatLngBounds" disabled="true">
                </div>
        </div>
        <div class="control-group">
            <label class="control-label">Long</label>
                <div class="controls">
                    <input value="" class="form-control" id="LatLngBounds" disabled="true">
                </div>
        </div> 

这是我的Javascript。

    var placeSearch, autocomplete;
// Need to add Lat / Long to populate field.
    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() {
    autocomplete = new google.maps.places.Autocomplete((document.getElementById('autocomplete')),{ types: ['geocode']});
    autocomplete.addListener('place_changed', fillInAddress);
}

    function fillInAddress() {
        var place = autocomplete.getPlace();
            for (var component in componentForm) {
                document.getElementById(component).value = "";
                document.getElementById(component).disabled = true;
            }
            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;
                    }
            }
    }

1 个答案:

答案 0 :(得分:2)

  1. 您的HTML无效。你有两个元素,id =&#34; LatLngBounds&#34;。
  2. 您没有任何代码可以填写表单中的纬度和经度值。
  3. 修复这些使它对我有用:

    代码段

    &#13;
    &#13;
        var placeSearch, autocomplete;
         // Need to add Lat / Long to populate field.
        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() {
          autocomplete = new google.maps.places.Autocomplete((document.getElementById('autocomplete')), {
            types: ['geocode']
          });
          autocomplete.addListener('place_changed', fillInAddress);
        }
    
        function fillInAddress() {
          var place = autocomplete.getPlace();
          for (var component in componentForm) {
            document.getElementById(component).value = "";
            document.getElementById(component).disabled = true;
          }
          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;
            }
          }
          document.getElementById("Latitude").value = place.geometry.location.lat();
          document.getElementById("Longitude").value = place.geometry.location.lng();
        }
        google.maps.event.addDomListener(window, "load", initAutocomplete);
    &#13;
    <script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>
    <div class="control-group">
      <label class="control-label">Address *</label>
      <div class="controls">
        <input value="Beacon St, Boston, MA" placeholder="Enter the customers full address" class="form-control" id="autocomplete" type="text">
      </div>
    </div>
    <div class="control-group">
      <label class="control-label">House Number</label>
      <div class="controls">
        <input value="" class="form-control" id="street_number" disabled="true">
      </div>
    </div>
    <div class="control-group">
      <label class="control-label">Street Address</label>
      <div class="controls">
        <input value="" class="form-control" id="route" disabled="true">
      </div>
    </div>
    <div class="control-group">
      <label class="control-label">City</label>
      <div class="controls">
        <input value="" class="form-control" id="locality" disabled="true">
      </div>
    </div>
    <div class="control-group">
      <label class="control-label">State</label>
      <div class="controls">
        <input value="" class="form-control" id="administrative_area_level_1" disabled="true">
      </div>
    </div>
    <div class="control-group">
      <label class="control-label">Zipcode</label>
      <div class="controls">
        <input value="" class="form-control" id="postal_code" disabled="true">
      </div>
    </div>
    <div class="control-group">
      <label class="control-label">Country</label>
      <div class="controls">
        <input value="" class="form-control" id="country" disabled="true">
      </div>
    </div>
    <div class="control-group">
      <label class="control-label">Lat</label>
      <div class="controls">
        <input value="" class="form-control" id="Latitude" disabled="true">
      </div>
    </div>
    <div class="control-group">
      <label class="control-label">Long</label>
      <div class="controls">
        <input value="" class="form-control" id="Longitude" disabled="true">
      </div>
    </div>
    &#13;
    &#13;
    &#13;