Advertisement
michaelyuen

Untitled

Jan 3rd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3.   <head>
  4.     <title>Place Autocomplete Address Form</title>
  5.     <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
  6.     <meta charset="utf-8">
  7.     <style>
  8.       html, body {
  9.         height: 100%;
  10.         margin: 0;
  11.         padding: 0;
  12.       }
  13.       #map {
  14.         height: 100%;
  15.       }
  16.     </style>
  17.     <link type="text/css" rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500">
  18.     <style>
  19.       #locationField, #controls {
  20.         position: relative;
  21.         width: 480px;
  22.       }
  23.       #autocomplete {
  24.         position: absolute;
  25.         top: 0px;
  26.         left: 0px;
  27.         width: 99%;
  28.       }
  29.       .label {
  30.         text-align: right;
  31.         font-weight: bold;
  32.         width: 100px;
  33.         color: #303030;
  34.       }
  35.       #address {
  36.         border: 1px solid #000090;
  37.         background-color: #f0f0ff;
  38.         width: 480px;
  39.         padding-right: 2px;
  40.       }
  41.       #address td {
  42.         font-size: 10pt;
  43.       }
  44.       .field {
  45.         width: 99%;
  46.       }
  47.       .slimField {
  48.         width: 80px;
  49.       }
  50.       .wideField {
  51.         width: 200px;
  52.       }
  53.       #locationField {
  54.         height: 20px;
  55.         margin-bottom: 2px;
  56.       }
  57.     </style>
  58.   </head>
  59.  
  60.   <body>
  61.     <div id="locationField">
  62.       <input id="autocomplete" placeholder="Enter your address" onFocus="geolocate()" type="text" />
  63.     </div>
  64.         <form name="address" action="get_distance.php" method="POST">
  65.             <table id="address">
  66.                 <tr>
  67.                     <td class="label">Street address</td>
  68.                     <td class="slimField"><input class="field" name="street_number" id="street_number" disabled="true" /></td>
  69.                     <td class="wideField" colspan="2"><input class="field" name="street_name" id="route" disabled="true" /></td>
  70.                 </tr>
  71.                 <tr>
  72.                     <td class="label">Postal Town</td>
  73.                     <td class="slimField"><input class="field" name="postal_town" id="postal_town" disabled="true" /></td>
  74.                     <td class="wideField" colspan="2"></td>
  75.                 </tr>
  76.                 <tr>
  77.                     <td class="label">City</td>
  78.                     <td class="wideField" colspan="3"><input class="field" name="city" id="locality" disabled="true" /></td>
  79.                 </tr>
  80.                 <tr>
  81.                     <td class="label">State</td>
  82.                     <td class="slimField"><input class="field" id="administrative_area_level_1" disabled="true"></input></td>
  83.                     <td class="label">Zip code</td>
  84.                     <td class="wideField"><input class="field" id="postal_code" disabled="true" /></td>
  85.                 </tr>
  86.                 <tr>
  87.                     <td class="label">Country</td>
  88.                     <td class="wideField" colspan="3"><input class="field" id="country" disabled="true" /></td>
  89.                 </tr>
  90.                 <tr>
  91.                     <td class="label">others1</td>
  92.                     <td class="wideField" colspan="3"><input class="field" id="sublocality" disabled="true" /></td>
  93.                 </tr>
  94.                 <tr>
  95.                     <td class="label">others2</td>
  96.                     <td class="wideField" colspan="3"><input class="field" id="administrative_area_level_2" disabled="true" /></td>
  97.                 </tr>
  98.             </table>
  99.             <input type="submit" name="submit" value="continue" />
  100.         </form>
  101.     <script>
  102. // This example displays an address form, using the autocomplete feature
  103. // of the Google Places API to help users fill in the information.
  104.  
  105. var placeSearch, autocomplete;
  106. var componentForm = {
  107.   street_number: 'short_name',
  108.     postal_town: 'long_name',
  109.   route: 'long_name',
  110.   locality: 'long_name',
  111.   administrative_area_level_1: 'long_name',
  112.   country: 'long_name',
  113.   postal_code: 'short_name',
  114.     sublocality: 'long_name',
  115.     administrative_area_level_2: 'long_name'
  116. };
  117.  
  118. function initAutocomplete() {
  119.   // Create the autocomplete object, restricting the search to geographical
  120.   // location types.
  121.   autocomplete = new google.maps.places.Autocomplete(
  122.       /** @type {!HTMLInputElement} */(document.getElementById('autocomplete')),
  123.       {types: ['geocode']});
  124.  
  125.   // When the user selects an address from the dropdown, populate the address
  126.   // fields in the form.
  127.   autocomplete.addListener('place_changed', fillInAddress);
  128. }
  129.  
  130. // [START region_fillform]
  131. function fillInAddress() {
  132.   // Get the place details from the autocomplete object.
  133.   var place = autocomplete.getPlace();
  134.  
  135.   for (var component in componentForm) {
  136.     document.getElementById(component).value = '';
  137.     document.getElementById(component).disabled = false;
  138.   }
  139.  
  140.   // Get each component of the address from the place details
  141.   // and fill the corresponding field on the form.
  142.     // console.log(place.address_components);
  143.     console.log(place.address_components);
  144.   for (var i = 0; i < place.address_components.length; i++) {
  145.     var addressType = place.address_components[i].types[0];
  146.     if (componentForm[addressType]) {
  147.       var val = place.address_components[i][componentForm[addressType]];
  148.       document.getElementById(addressType).value = val;
  149.     }
  150.   }
  151. }
  152. // [END region_fillform]
  153.  
  154. // [START region_geolocation]
  155. // Bias the autocomplete object to the user's geographical location,
  156. // as supplied by the browser's 'navigator.geolocation' object.
  157. function geolocate() {
  158.   if (navigator.geolocation) {
  159.     navigator.geolocation.getCurrentPosition(function(position) {
  160.       var geolocation = {
  161.         lat: position.coords.latitude,
  162.         lng: position.coords.longitude
  163.       };
  164.       var circle = new google.maps.Circle({
  165.         center: geolocation,
  166.         radius: position.coords.accuracy
  167.       });
  168.       autocomplete.setBounds(circle.getBounds());
  169.     });
  170.   }
  171. }
  172. // [END region_geolocation]
  173.  
  174.     </script>
  175.     <script src="https://maps.googleapis.com/maps/api/js?key=__api_key__&libraries=places&language=en&callback=initAutocomplete"
  176.         async defer></script>
  177.   </body>
  178. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement