【问题标题】:Automatic Address Fill In Form Using Geolocation使用地理位置自动填写表格
【发布时间】:2015-01-13 15:06:30
【问题描述】:

我正在尝试使用地理位置自动为我的用户填写地址字段。我正在苦苦挣扎的地方是(邮寄地址 1:) 和(物理地址 1:) 到目前为止,一旦用户开始输入,它就会显示用户地址,但是当他们选择并选择他们的地址时,我不想要整个地址加上Mailing Address 1: 上的城市州和邮政编码。我希望它显示街道号码和街道地址,然后填写其他字段(城市、州、邮编)留下 Mailing Address 2: 空白,以防他们有公寓或套房。

那么,如果您选择物理地址和邮寄地址显然是相同的,我希望它像上面一样填写。

如果地址不相同,我希望它遵循与上述相同的过程。

任何帮助将不胜感激!

var placeSearch, autocomplete, autocomplete2;
var componentForm = {
  route: 'long_name',
  locality: 'long_name',
  administrative_area_level_1: 'short_name',
  postal_code: 'short_name'
};
var componentForm2 = {
  route2: 'long_name',
  locality2: 'long_name',
  administrative_area_level_12: 'short_name',
  postal_code2: 'short_name'
};

function initialize() {
  // Create the autocomplete object, restricting the search
  // to geographical location types.
  autocomplete = new google.maps.places.Autocomplete(
    /** @type {HTMLInputElement} */
    (document.getElementById('autocomplete')), {
      types: ['geocode']
    });
  autocomplete2 = new google.maps.places.Autocomplete(
    /** @type {HTMLInputElement} */
    (document.getElementById('autocomplete2')), {
      types: ['geocode']
    });
  // When the user selects an address from the dropdown,
  // populate the address fields in the form.
  google.maps.event.addListener(autocomplete, 'place_changed', function() {
    fillInAddress();
  });
  google.maps.event.addListener(autocomplete2, 'place_changed', function() {
    fillInAddress2();
  });
}

// [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;
    }
  }
}

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

    for (var component in componentForm2) {

      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 (componentForm2[addressType + '2']) {
        var val = place.address_components[i][componentForm2[addressType + '2']];
        document.getElementById(addressType + '2').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 = new google.maps.LatLng(
        position.coords.latitude, position.coords.longitude);
      var circle = new google.maps.Circle({
        center: geolocation,
        radius: position.coords.accuracy
      });
      autocomplete.setBounds(circle.getBounds());
    });
  }
}

function geolocate2() {
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(function(position) {
        var geolocation = new google.maps.LatLng(
          position.coords.latitude, position.coords.longitude);
        var circle = new google.maps.Circle({
          center: geolocation,
          radius: position.coords.accuracy
        });
        autocomplete2.setBounds(circle.getBounds());
      });
    }
  }
  // [END region_geolocation]
initialize();

document.querySelector('#chbSame').addEventListener('change', checkedAddr);


function checkedAddr() {
  if (document.getElementById('chbSame').checked) {
    document.getElementById('route2').value = document.getElementById('route').value;
    document.getElementById('locality2').value = document.getElementById('locality').value;
    document.getElementById('administrative_area_level_12').value = document.getElementById('administrative_area_level_1').value;
    document.getElementById('postal_code2').value = document.getElementById('postal_code').value;
    

  } else {
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places"></script>
    <body onload="initialize()">
    <div id="locationField">
      <div class="clearfix">
    	<label for="street_<cfoutput>#Add#</cfoutput>">Mailing Address 1:</label>
    	<input type="text" name="street_#Add#" validateat="onSubmit" validate="maxlength" required="yes" id="autocomplete" size="54" maxlength="120" message="Please enter owner #Peoplecount#'s mailing address." onFocus="geolocate()" value="">
    </div>

    <div class="clearfix">
    	<label for="m2street_<cfoutput>#Add#</cfoutput>">Mailing Address 2:</label>
    	<input type="text" name="m2street_#Add#" validateat="onSubmit" required="no" validate="maxlength" id="route" size="54" maxlength="120" value="">
    </div>
      
    <div class="clearfix">
    	<label for="city_<cfoutput>#Add#</cfoutput>">City:</label>
    	<input type="text" name="city_#Add#" validateat="onSubmit" validate="maxlength" required="yes" id="locality" size="30" maxlength="50" message="Please enter owner #Peoplecount#'s mailing city." value="">
        </div>
        
        <div class="clearfix">
    	<label for="state_<cfoutput>#Add#</cfoutput>">State:</label>
    	<input type="text" name="state_#Add#" required="yes" id="administrative_area_level_1" size="8" maxlength="12" message="Please enter owner #Peoplecount#'s mailing state." value="">
    </div>
            
            <div class="clearfix">
    	<label for="street_<cfoutput>#Add#</cfoutput>">Zip Code:</label>
    	<input type="text" name="postal_#Add#" required="yes" id="postal_code" size="8" maxlength="12" message="Please enter owner #Peoplecount#'s mailing zip code." value="">
    </div>
      </div>

          <div class="">
  <input type="checkbox" name="billingtoo_<cfoutput>#Add#</cfoutput>" id="chbSame" />
  <em>Check this box if Physical Address and Mailing Address are the same.</em>
</div>
 
        

          <div id="locationField2">
            <div class="clearfix">
    	<label for="pstreet_<cfoutput>#Add#</cfoutput>">Physical Address 1:</label>
    	<input type="text" name="pstreet_#Add#" validateat="onSubmit" validate="maxlength" required="yes" id="autocomplete2" size="53" maxlength="120" message="Please enter owner #Peoplecount#'s physical address." onFocus="geolocate2()" value="">
    </div> 

    <div class="clearfix">
    	<label for="p2street_<cfoutput>#Add#</cfoutput>">Physical Address 2:</label>
    	<input type="text" name="p2street_#Add#" validateat="onSubmit" required="no" validate="maxlength" id="route2" size="53" maxlength="120" value="">
    </div>
            
    <div class="clearfix">
    	<label for="pcity_<cfoutput>#Add#</cfoutput>">City:</label>
    	<input type="text" name="pcity_#Add#" validateat="onSubmit" validate="maxlength" required="yes" id="locality2" size="30" maxlength="50" message="Please enter owner #Peoplecount#'s physical city." value="">
      </div>
            
            <div class="clearfix">
    	<label for="pstate_<cfoutput>#Add#</cfoutput>">State:</label>
    	<input type="text" name="pstate_#Add#" required="yes" id="administrative_area_level_12" size="8" maxlength="12" message="Please enter owner #Peoplecount#'s physical state." value="">
    </div>
                
            <div class="clearfix">
    	<label for="pstreet_<cfoutput>#Add#</cfoutput>">Zip Code:</label>
    	<input type="text" name="ppostal_#Add#" required="yes" id="postal_code2" size="8" maxlength="12" message="Please enter owner #Peoplecount#'s physical zip code." value="">
    </div>​​
            </div>

【问题讨论】:

    标签: javascript geolocation


    【解决方案1】:

    试试这个,

    对我来说很好。

    var placeSearch, autocomplete, autocomplete2;
    var componentForm = {
      route: 'long_name',
      locality: 'long_name',
      administrative_area_level_1: 'short_name',
      postal_code: 'short_name'
    };
    var componentForm2 = {
      route2: 'long_name',
      locality2: 'long_name',
      administrative_area_level_12: 'short_name',
      postal_code2: 'short_name'
    };
    
    function initialize() {
      // Create the autocomplete object, restricting the search
      // to geographical location types.
      autocomplete = new google.maps.places.Autocomplete(
        /** @type {HTMLInputElement} */
        (document.getElementById('autocomplete')), {
          types: ['geocode']
        });
      autocomplete2 = new google.maps.places.Autocomplete(
        /** @type {HTMLInputElement} */
        (document.getElementById('autocomplete2')), {
          types: ['geocode']
        });
      // When the user selects an address from the dropdown,
      // populate the address fields in the form.
      google.maps.event.addListener(autocomplete, 'place_changed', function() {
        fillInAddress();
      });
      google.maps.event.addListener(autocomplete2, 'place_changed', function() {
        fillInAddress2();
      });
    }
    
    // [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;
        }
      }
      //var keys=[];for (var key in place.address_components[0]) keys.push(key);
      //alert(keys):
      document.getElementById('autocomplete').value = 
        place.address_components[0]['long_name'] + ' ' +
        place.address_components[1]['long_name'];
      
      /*document.getElementById('route').value = (document.getElementById('chbSame').checked ? document.getElementById('autocomplete').value : '');*/
      document.getElementById('route').value = '';
    }
    
    function fillInAddress2() {
        // Get the place details from the autocomplete object.
        var place = autocomplete2.getPlace();
    
        for (var component in componentForm2) {
    
          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 (componentForm2[addressType + '2']) {
            var val = place.address_components[i][componentForm2[addressType + '2']];
            document.getElementById(addressType + '2').value = val;
          }
        }
      document.getElementById('autocomplete2').value = 
        place.address_components[0]['long_name'] + ' ' +
        place.address_components[1]['long_name'];
      
      /*document.getElementById('route2').value = (document.getElementById('chbSame').checked ? document.getElementById('autocomplete2').value : '');*/
      document.getElementById('route2').value = '';
    }
      // [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 = new google.maps.LatLng(
            position.coords.latitude, position.coords.longitude);
          var circle = new google.maps.Circle({
            center: geolocation,
            radius: position.coords.accuracy
          });
          autocomplete.setBounds(circle.getBounds());
        });
      }
    }
    
    function geolocate2() {
        if (navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(function(position) {
            var geolocation = new google.maps.LatLng(
              position.coords.latitude, position.coords.longitude);
            var circle = new google.maps.Circle({
              center: geolocation,
              radius: position.coords.accuracy
            });
            autocomplete2.setBounds(circle.getBounds());
          });
        }
      }
      // [END region_geolocation]
    initialize();
    
    document.querySelector('#chbSame').addEventListener('change', checkedAddr);
    
    
    function checkedAddr() {
      if (document.getElementById('chbSame').checked) {
        document.getElementById('route2').value = document.getElementById('route').value;
        document.getElementById('locality2').value = document.getElementById('locality').value;
        document.getElementById('administrative_area_level_12').value = document.getElementById('administrative_area_level_1').value;
        document.getElementById('postal_code2').value = document.getElementById('postal_code').value;
        
    document.getElementById('autocomplete2').value = document.getElementById('autocomplete').value;
      } else {
        }
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places"></script>
        <body onload="initialize()">
        <div id="locationField">
          <div class="clearfix">
        	<label for="street_<cfoutput>#Add#</cfoutput>">Mailing Address 1:</label>
        	<input type="text" name="street_#Add#" validateat="onSubmit" validate="maxlength" required="yes" id="autocomplete" size="54" maxlength="120" message="Please enter owner #Peoplecount#'s mailing address." onFocus="geolocate()" value="">
        </div>
    
        <div class="clearfix">
        	<label for="m2street_<cfoutput>#Add#</cfoutput>">Mailing Address 2:</label>
        	<input type="text" name="m2street_#Add#" validateat="onSubmit" required="no" validate="maxlength" id="route" size="54" maxlength="120" value="">
        </div>
          
        <div class="clearfix">
        	<label for="city_<cfoutput>#Add#</cfoutput>">City:</label>
        	<input type="text" name="city_#Add#" validateat="onSubmit" validate="maxlength" required="yes" id="locality" size="30" maxlength="50" message="Please enter owner #Peoplecount#'s mailing city." value="">
            </div>
            
            <div class="clearfix">
        	<label for="state_<cfoutput>#Add#</cfoutput>">State:</label>
        	<input type="text" name="state_#Add#" required="yes" id="administrative_area_level_1" size="8" maxlength="12" message="Please enter owner #Peoplecount#'s mailing state." value="">
        </div>
                
                <div class="clearfix">
        	<label for="street_<cfoutput>#Add#</cfoutput>">Zip Code:</label>
        	<input type="text" name="postal_#Add#" required="yes" id="postal_code" size="8" maxlength="12" message="Please enter owner #Peoplecount#'s mailing zip code." value="">
        </div>
          </div>
    
              <div class="">
      <input type="checkbox" name="billingtoo_<cfoutput>#Add#</cfoutput>" id="chbSame" />
      <em>Check this box if Physical Address and Mailing Address are the same.</em>
    </div>
     
            
    
              <div id="locationField2">
                <div class="clearfix">
        	<label for="pstreet_<cfoutput>#Add#</cfoutput>">Physical Address 1:</label>
        	<input type="text" name="pstreet_#Add#" validateat="onSubmit" validate="maxlength" required="yes" id="autocomplete2" size="53" maxlength="120" message="Please enter owner #Peoplecount#'s physical address." onFocus="geolocate2()" value="">
        </div> 
    
        <div class="clearfix">
        	<label for="p2street_<cfoutput>#Add#</cfoutput>">Physical Address 2:</label>
        	<input type="text" name="p2street_#Add#" validateat="onSubmit" required="no" validate="maxlength" id="route2" size="53" maxlength="120" value="">
        </div>
                
        <div class="clearfix">
        	<label for="pcity_<cfoutput>#Add#</cfoutput>">City:</label>
        	<input type="text" name="pcity_#Add#" validateat="onSubmit" validate="maxlength" required="yes" id="locality2" size="30" maxlength="50" message="Please enter owner #Peoplecount#'s physical city." value="">
          </div>
                
                <div class="clearfix">
        	<label for="pstate_<cfoutput>#Add#</cfoutput>">State:</label>
        	<input type="text" name="pstate_#Add#" required="yes" id="administrative_area_level_12" size="8" maxlength="12" message="Please enter owner #Peoplecount#'s physical state." value="">
        </div>
                    
                <div class="clearfix">
        	<label for="pstreet_<cfoutput>#Add#</cfoutput>">Zip Code:</label>
        	<input type="text" name="ppostal_#Add#" required="yes" id="postal_code2" size="8" maxlength="12" message="Please enter owner #Peoplecount#'s physical zip code." value="">
        </div>​​
                </div>

    编辑:根据下面的 cmets 更新

    编辑 2:根据下面的 cmets

    编辑 3:根据下面的另一个 cmets :)

    编辑 4:根据下面的另一个 cmets :)

    编辑 5:-||-

    【讨论】:

    • 对不起,也许我没有理解这句话的意思:“我必须在它从邮寄地址一开始计算的地方拥有它”。我认为这个演示可以很容易地调整。我只是不确定什么是期望的行为。
    • 我尝试了您的示例,但它没有填充子字段(城市、邮政、地址)。不确定我是否遵循 - 您想填写子字段并清除自动完成文本框本身吗? J
    • 好的,所以您开始在自动完成文本框上输入以查找某个位置。例如选择“纽约,布鲁克林,第 12 街”并拿起它。然后,应填写子字段(City=New York,State=NY 等等..),然后原来的自动完成字段应该只包含街道名称?那么在这种情况下是“第 12 街”?
    • @Alex 好的。我更新了答案。为我工作。
    • Ivan,你能解释一下你做了什么来帮助 Alex 解决他的问题吗?你的回答只是说“试试这个,对我有用”,但没有解释你为了让它工作而做了什么。谢谢!
    【解决方案2】:

    var placeSearch, autocomplete, autocomplete2;
    var componentForm = {
      route: 'long_name',
      locality: 'long_name',
      administrative_area_level_1: 'short_name',
      postal_code: 'short_name'
    };
    var componentForm2 = {
      route2: 'long_name',
      locality2: 'long_name',
      administrative_area_level_12: 'short_name',
      postal_code2: 'short_name'
    };
    
    function initialize() {
      // Create the autocomplete object, restricting the search
      // to geographical location types.
      autocomplete = new google.maps.places.Autocomplete(
        /** @type {HTMLInputElement} */
        (document.getElementById('autocomplete')), {
          types: ['geocode']
        });
      autocomplete2 = new google.maps.places.Autocomplete(
        /** @type {HTMLInputElement} */
        (document.getElementById('autocomplete2')), {
          types: ['geocode']
        });
      // When the user selects an address from the dropdown,
      // populate the address fields in the form.
      google.maps.event.addListener(autocomplete, 'place_changed', function() {
        fillInAddress();
      });
      google.maps.event.addListener(autocomplete2, 'place_changed', function() {
        fillInAddress2();
      });
    }
    
    // [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;
        }
      }
      //var keys=[];for (var key in place.address_components[0]) keys.push(key);
      //alert(keys):
      document.getElementById('autocomplete').value = 
        place.address_components[0]['long_name'] + ' ' +
        place.address_components[1]['long_name'];
      
      /*document.getElementById('route').value = (document.getElementById('chbSame').checked ? document.getElementById('autocomplete').value : '');*/
      document.getElementById('route').value = '';
    }
    
    function fillInAddress2() {
        // Get the place details from the autocomplete object.
        var place = autocomplete2.getPlace();
    
        for (var component in componentForm2) {
    
          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 (componentForm2[addressType + '2']) {
            var val = place.address_components[i][componentForm2[addressType + '2']];
            document.getElementById(addressType + '2').value = val;
          }
        }
      document.getElementById('autocomplete2').value = 
        place.address_components[0]['long_name'] + ' ' +
        place.address_components[1]['long_name'];
      
      /*document.getElementById('route2').value = (document.getElementById('chbSame').checked ? document.getElementById('autocomplete2').value : '');*/
      document.getElementById('route2').value = '';
    }
      // [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 = new google.maps.LatLng(
            position.coords.latitude, position.coords.longitude);
          var circle = new google.maps.Circle({
            center: geolocation,
            radius: position.coords.accuracy
          });
          autocomplete.setBounds(circle.getBounds());
        });
      }
    }
    
    function geolocate2() {
        if (navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(function(position) {
            var geolocation = new google.maps.LatLng(
              position.coords.latitude, position.coords.longitude);
            var circle = new google.maps.Circle({
              center: geolocation,
              radius: position.coords.accuracy
            });
            autocomplete2.setBounds(circle.getBounds());
          });
        }
      }
      // [END region_geolocation]
    initialize();
    
    document.querySelector('#chbSame').addEventListener('change', checkedAddr);
    
    
    function checkedAddr() {
      if (document.getElementById('chbSame').checked) {
        document.getElementById('route2').value = document.getElementById('route').value;
        document.getElementById('locality2').value = document.getElementById('locality').value;
        document.getElementById('administrative_area_level_12').value = document.getElementById('administrative_area_level_1').value;
        document.getElementById('postal_code2').value = document.getElementById('postal_code').value;
        
    document.getElementById('autocomplete2').value = document.getElementById('autocomplete').value;
      } else {
        }
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places"></script>
        <body onload="initialize()">
        <div id="locationField">
          <div class="clearfix">
        	<label for="street_<cfoutput>#Add#</cfoutput>">Mailing Address 1:</label>
        	<input type="text" name="street_#Add#" validateat="onSubmit" validate="maxlength" required="yes" id="autocomplete" size="54" maxlength="120" message="Please enter owner #Peoplecount#'s mailing address." onFocus="geolocate()" value="">
        </div>
    
        <div class="clearfix">
        	<label for="m2street_<cfoutput>#Add#</cfoutput>">Mailing Address 2:</label>
        	<input type="text" name="m2street_#Add#" validateat="onSubmit" required="no" validate="maxlength" id="route" size="54" maxlength="120" value="">
        </div>
          
        <div class="clearfix">
        	<label for="city_<cfoutput>#Add#</cfoutput>">City:</label>
        	<input type="text" name="city_#Add#" validateat="onSubmit" validate="maxlength" required="yes" id="locality" size="30" maxlength="50" message="Please enter owner #Peoplecount#'s mailing city." value="">
            </div>
            
            <div class="clearfix">
        	<label for="state_<cfoutput>#Add#</cfoutput>">State:</label>
        	<input type="text" name="state_#Add#" required="yes" id="administrative_area_level_1" size="8" maxlength="12" message="Please enter owner #Peoplecount#'s mailing state." value="">
        </div>
                
                <div class="clearfix">
        	<label for="street_<cfoutput>#Add#</cfoutput>">Zip Code:</label>
        	<input type="text" name="postal_#Add#" required="yes" id="postal_code" size="8" maxlength="12" message="Please enter owner #Peoplecount#'s mailing zip code." value="">
        </div>
          </div>
    
              <div class="">
      <input type="checkbox" name="billingtoo_<cfoutput>#Add#</cfoutput>" id="chbSame" />
      <em>Check this box if Physical Address and Mailing Address are the same.</em>
    </div>
     
            
    
              <div id="locationField2">
                <div class="clearfix">
        	<label for="pstreet_<cfoutput>#Add#</cfoutput>">Physical Address 1:</label>
        	<input type="text" name="pstreet_#Add#" validateat="onSubmit" validate="maxlength" required="yes" id="autocomplete2" size="53" maxlength="120" message="Please enter owner #Peoplecount#'s physical address." onFocus="geolocate2()" value="">
        </div> 
    
        <div class="clearfix">
        	<label for="p2street_<cfoutput>#Add#</cfoutput>">Physical Address 2:</label>
        	<input type="text" name="p2street_#Add#" validateat="onSubmit" required="no" validate="maxlength" id="route2" size="53" maxlength="120" value="">
        </div>
                
        <div class="clearfix">
        	<label for="pcity_<cfoutput>#Add#</cfoutput>">City:</label>
        	<input type="text" name="pcity_#Add#" validateat="onSubmit" validate="maxlength" required="yes" id="locality2" size="30" maxlength="50" message="Please enter owner #Peoplecount#'s physical city." value="">
          </div>
                
                <div class="clearfix">
        	<label for="pstate_<cfoutput>#Add#</cfoutput>">State:</label>
        	<input type="text" name="pstate_#Add#" required="yes" id="administrative_area_level_12" size="8" maxlength="12" message="Please enter owner #Peoplecount#'s physical state." value="">
        </div>
                    
                <div class="clearfix">
        	<label for="pstreet_<cfoutput>#Add#</cfoutput>">Zip Code:</label>
        	<input type="text" name="ppostal_#Add#" required="yes" id="postal_code2" size="8" maxlength="12" message="Please enter owner #Peoplecount#'s physical zip code." value="">
        </div>​​
                </div>

    var placeSearch, autocomplete, autocomplete2;
    var componentForm = {
      route: 'long_name',
      locality: 'long_name',
      administrative_area_level_1: 'short_name',
      postal_code: 'short_name'
    };
    var componentForm2 = {
      route2: 'long_name',
      locality2: 'long_name',
      administrative_area_level_12: 'short_name',
      postal_code2: 'short_name'
    };
    
    function initialize() {
      // Create the autocomplete object, restricting the search
      // to geographical location types.
      autocomplete = new google.maps.places.Autocomplete(
        /** @type {HTMLInputElement} */
        (document.getElementById('autocomplete')), {
          types: ['geocode']
        });
      autocomplete2 = new google.maps.places.Autocomplete(
        /** @type {HTMLInputElement} */
        (document.getElementById('autocomplete2')), {
          types: ['geocode']
        });
      // When the user selects an address from the dropdown,
      // populate the address fields in the form.
      google.maps.event.addListener(autocomplete, 'place_changed', function() {
        fillInAddress();
      });
      google.maps.event.addListener(autocomplete2, 'place_changed', function() {
        fillInAddress2();
      });
    }
    
    // [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;
        }
      }
      //var keys=[];for (var key in place.address_components[0]) keys.push(key);
      //alert(keys):
      document.getElementById('autocomplete').value = 
        place.address_components[0]['long_name'] + ' ' +
        place.address_components[1]['long_name'];
      
      /*document.getElementById('route').value = (document.getElementById('chbSame').checked ? document.getElementById('autocomplete').value : '');*/
      document.getElementById('route').value = '';
    }
    
    function fillInAddress2() {
        // Get the place details from the autocomplete object.
        var place = autocomplete2.getPlace();
    
        for (var component in componentForm2) {
    
          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 (componentForm2[addressType + '2']) {
            var val = place.address_components[i][componentForm2[addressType + '2']];
            document.getElementById(addressType + '2').value = val;
          }
        }
      document.getElementById('autocomplete2').value = 
        place.address_components[0]['long_name'] + ' ' +
        place.address_components[1]['long_name'];
      
      /*document.getElementById('route2').value = (document.getElementById('chbSame').checked ? document.getElementById('autocomplete2').value : '');*/
      document.getElementById('route2').value = '';
    }
      // [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 = new google.maps.LatLng(
            position.coords.latitude, position.coords.longitude);
          var circle = new google.maps.Circle({
            center: geolocation,
            radius: position.coords.accuracy
          });
          autocomplete.setBounds(circle.getBounds());
        });
      }
    }
    
    function geolocate2() {
        if (navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(function(position) {
            var geolocation = new google.maps.LatLng(
              position.coords.latitude, position.coords.longitude);
            var circle = new google.maps.Circle({
              center: geolocation,
              radius: position.coords.accuracy
            });
            autocomplete2.setBounds(circle.getBounds());
          });
        }
      }
      // [END region_geolocation]
    initialize();
    
    document.querySelector('#chbSame').addEventListener('change', checkedAddr);
    
    
    function checkedAddr() {
      if (document.getElementById('chbSame').checked) {
        document.getElementById('route2').value = document.getElementById('route').value;
        document.getElementById('locality2').value = document.getElementById('locality').value;
        document.getElementById('administrative_area_level_12').value = document.getElementById('administrative_area_level_1').value;
        document.getElementById('postal_code2').value = document.getElementById('postal_code').value;
        
    document.getElementById('autocomplete2').value = document.getElementById('autocomplete').value;
      } else {
        }
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places"></script>
        <body onload="initialize()">
        <div id="locationField">
          <div class="clearfix">
        	<label for="street_<cfoutput>#Add#</cfoutput>">Mailing Address 1:</label>
        	<input type="text" name="street_#Add#" validateat="onSubmit" validate="maxlength" required="yes" id="autocomplete" size="54" maxlength="120" message="Please enter owner #Peoplecount#'s mailing address." onFocus="geolocate()" value="">
        </div>
    
        <div class="clearfix">
        	<label for="m2street_<cfoutput>#Add#</cfoutput>">Mailing Address 2:</label>
        	<input type="text" name="m2street_#Add#" validateat="onSubmit" required="no" validate="maxlength" id="route" size="54" maxlength="120" value="">
        </div>
          
        <div class="clearfix">
        	<label for="city_<cfoutput>#Add#</cfoutput>">City:</label>
        	<input type="text" name="city_#Add#" validateat="onSubmit" validate="maxlength" required="yes" id="locality" size="30" maxlength="50" message="Please enter owner #Peoplecount#'s mailing city." value="">
            </div>
            
            <div class="clearfix">
        	<label for="state_<cfoutput>#Add#</cfoutput>">State:</label>
        	<input type="text" name="state_#Add#" required="yes" id="administrative_area_level_1" size="8" maxlength="12" message="Please enter owner #Peoplecount#'s mailing state." value="">
        </div>
                
                <div class="clearfix">
        	<label for="street_<cfoutput>#Add#</cfoutput>">Zip Code:</label>
        	<input type="text" name="postal_#Add#" required="yes" id="postal_code" size="8" maxlength="12" message="Please enter owner #Peoplecount#'s mailing zip code." value="">
        </div>
          </div>
    
              <div class="">
      <input type="checkbox" name="billingtoo_<cfoutput>#Add#</cfoutput>" id="chbSame" />
      <em>Check this box if Physical Address and Mailing Address are the same.</em>
    </div>
     
            
    
              <div id="locationField2">
                <div class="clearfix">
        	<label for="pstreet_<cfoutput>#Add#</cfoutput>">Physical Address 1:</label>
        	<input type="text" name="pstreet_#Add#" validateat="onSubmit" validate="maxlength" required="yes" id="autocomplete2" size="53" maxlength="120" message="Please enter owner #Peoplecount#'s physical address." onFocus="geolocate2()" value="">
        </div> 
    
        <div class="clearfix">
        	<label for="p2street_<cfoutput>#Add#</cfoutput>">Physical Address 2:</label>
        	<input type="text" name="p2street_#Add#" validateat="onSubmit" required="no" validate="maxlength" id="route2" size="53" maxlength="120" value="">
        </div>
                
        <div class="clearfix">
        	<label for="pcity_<cfoutput>#Add#</cfoutput>">City:</label>
        	<input type="text" name="pcity_#Add#" validateat="onSubmit" validate="maxlength" required="yes" id="locality2" size="30" maxlength="50" message="Please enter owner #Peoplecount#'s physical city." value="">
          </div>
                
                <div class="clearfix">
        	<label for="pstate_<cfoutput>#Add#</cfoutput>">State:</label>
        	<input type="text" name="pstate_#Add#" required="yes" id="administrative_area_level_12" size="8" maxlength="12" message="Please enter owner #Peoplecount#'s physical state." value="">
        </div>
                    
                <div class="clearfix">
        	<label for="pstreet_<cfoutput>#Add#</cfoutput>">Zip Code:</label>
        	<input type="text" name="ppostal_#Add#" required="yes" id="postal_code2" size="8" maxlength="12" message="Please enter owner #Peoplecount#'s physical zip code." value="">
        </div>​​
                </div>

    var placeSearch, autocomplete, autocomplete2;
    var componentForm = {
      route: 'long_name',
      locality: 'long_name',
      administrative_area_level_1: 'short_name',
      postal_code: 'short_name'
    };
    var componentForm2 = {
      route2: 'long_name',
      locality2: 'long_name',
      administrative_area_level_12: 'short_name',
      postal_code2: 'short_name'
    };
    
    function initialize() {
      // Create the autocomplete object, restricting the search
      // to geographical location types.
      autocomplete = new google.maps.places.Autocomplete(
        /** @type {HTMLInputElement} */
        (document.getElementById('autocomplete')), {
          types: ['geocode']
        });
      autocomplete2 = new google.maps.places.Autocomplete(
        /** @type {HTMLInputElement} */
        (document.getElementById('autocomplete2')), {
          types: ['geocode']
        });
      // When the user selects an address from the dropdown,
      // populate the address fields in the form.
      google.maps.event.addListener(autocomplete, 'place_changed', function() {
        fillInAddress();
      });
      google.maps.event.addListener(autocomplete2, 'place_changed', function() {
        fillInAddress2();
      });
    }
    
    // [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;
        }
      }
      //var keys=[];for (var key in place.address_components[0]) keys.push(key);
      //alert(keys):
      document.getElementById('autocomplete').value = 
        place.address_components[0]['long_name'] + ' ' +
        place.address_components[1]['long_name'];
      
      /*document.getElementById('route').value = (document.getElementById('chbSame').checked ? document.getElementById('autocomplete').value : '');*/
      document.getElementById('route').value = '';
    }
    
    function fillInAddress2() {
        // Get the place details from the autocomplete object.
        var place = autocomplete2.getPlace();
    
        for (var component in componentForm2) {
    
          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 (componentForm2[addressType + '2']) {
            var val = place.address_components[i][componentForm2[addressType + '2']];
            document.getElementById(addressType + '2').value = val;
          }
        }
      document.getElementById('autocomplete2').value = 
        place.address_components[0]['long_name'] + ' ' +
        place.address_components[1]['long_name'];
      
      /*document.getElementById('route2').value = (document.getElementById('chbSame').checked ? document.getElementById('autocomplete2').value : '');*/
      document.getElementById('route2').value = '';
    }
      // [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 = new google.maps.LatLng(
            position.coords.latitude, position.coords.longitude);
          var circle = new google.maps.Circle({
            center: geolocation,
            radius: position.coords.accuracy
          });
          autocomplete.setBounds(circle.getBounds());
        });
      }
    }
    
    function geolocate2() {
        if (navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(function(position) {
            var geolocation = new google.maps.LatLng(
              position.coords.latitude, position.coords.longitude);
            var circle = new google.maps.Circle({
              center: geolocation,
              radius: position.coords.accuracy
            });
            autocomplete2.setBounds(circle.getBounds());
          });
        }
      }
      // [END region_geolocation]
    initialize();
    
    document.querySelector('#chbSame').addEventListener('change', checkedAddr);
    
    
    function checkedAddr() {
      if (document.getElementById('chbSame').checked) {
        document.getElementById('route2').value = document.getElementById('route').value;
        document.getElementById('locality2').value = document.getElementById('locality').value;
        document.getElementById('administrative_area_level_12').value = document.getElementById('administrative_area_level_1').value;
        document.getElementById('postal_code2').value = document.getElementById('postal_code').value;
        
    document.getElementById('autocomplete2').value = document.getElementById('autocomplete').value;
      } else {
        }
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places"></script>
        <body onload="initialize()">
        <div id="locationField">
          <div class="clearfix">
        	<label for="street_<cfoutput>#Add#</cfoutput>">Mailing Address 1:</label>
        	<input type="text" name="street_#Add#" validateat="onSubmit" validate="maxlength" required="yes" id="autocomplete" size="54" maxlength="120" message="Please enter owner #Peoplecount#'s mailing address." onFocus="geolocate()" value="">
        </div>
    
        <div class="clearfix">
        	<label for="m2street_<cfoutput>#Add#</cfoutput>">Mailing Address 2:</label>
        	<input type="text" name="m2street_#Add#" validateat="onSubmit" required="no" validate="maxlength" id="route" size="54" maxlength="120" value="">
        </div>
          
        <div class="clearfix">
        	<label for="city_<cfoutput>#Add#</cfoutput>">City:</label>
        	<input type="text" name="city_#Add#" validateat="onSubmit" validate="maxlength" required="yes" id="locality" size="30" maxlength="50" message="Please enter owner #Peoplecount#'s mailing city." value="">
            </div>
            
            <div class="clearfix">
        	<label for="state_<cfoutput>#Add#</cfoutput>">State:</label>
        	<input type="text" name="state_#Add#" required="yes" id="administrative_area_level_1" size="8" maxlength="12" message="Please enter owner #Peoplecount#'s mailing state." value="">
        </div>
                
                <div class="clearfix">
        	<label for="street_<cfoutput>#Add#</cfoutput>">Zip Code:</label>
        	<input type="text" name="postal_#Add#" required="yes" id="postal_code" size="8" maxlength="12" message="Please enter owner #Peoplecount#'s mailing zip code." value="">
        </div>
          </div>
    
              <div class="">
      <input type="checkbox" name="billingtoo_<cfoutput>#Add#</cfoutput>" id="chbSame" />
      <em>Check this box if Physical Address and Mailing Address are the same.</em>
    </div>
     
            
    
              <div id="locationField2">
                <div class="clearfix">
        	<label for="pstreet_<cfoutput>#Add#</cfoutput>">Physical Address 1:</label>
        	<input type="text" name="pstreet_#Add#" validateat="onSubmit" validate="maxlength" required="yes" id="autocomplete2" size="53" maxlength="120" message="Please enter owner #Peoplecount#'s physical address." onFocus="geolocate2()" value="">
        </div> 
    
        <div class="clearfix">
        	<label for="p2street_<cfoutput>#Add#</cfoutput>">Physical Address 2:</label>
        	<input type="text" name="p2street_#Add#" validateat="onSubmit" required="no" validate="maxlength" id="route2" size="53" maxlength="120" value="">
        </div>
                
        <div class="clearfix">
        	<label for="pcity_<cfoutput>#Add#</cfoutput>">City:</label>
        	<input type="text" name="pcity_#Add#" validateat="onSubmit" validate="maxlength" required="yes" id="locality2" size="30" maxlength="50" message="Please enter owner #Peoplecount#'s physical city." value="">
          </div>
                
                <div class="clearfix">
        	<label for="pstate_<cfoutput>#Add#</cfoutput>">State:</label>
        	<input type="text" name="pstate_#Add#" required="yes" id="administrative_area_level_12" size="8" maxlength="12" message="Please enter owner #Peoplecount#'s physical state." value="">
        </div>
                    
                <div class="clearfix">
        	<label for="pstreet_<cfoutput>#Add#</cfoutput>">Zip Code:</label>
        	<input type="text" name="ppostal_#Add#" required="yes" id="postal_code2" size="8" maxlength="12" message="Please enter owner #Peoplecount#'s physical zip code." value="">
        </div>​​
                </div>

    【讨论】:

    • 当我运行这段代码时,它正在运行,但是当我使用这段代码时它不起作用,你能帮助先生吗?第一个文本框总是显示消息“有些东西已经磨损了”
    • 尝试将其作为另一个问题发布,而不是作为答案发布。 stackoverflow.com/questions/ask
    猜你喜欢
    • 1970-01-01
    • 2013-07-29
    • 2019-01-24
    • 1970-01-01
    • 1970-01-01
    • 2011-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多