Guest User

Google Place Auto Complete

a guest
Nov 5th, 2012
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.15 KB | None | 0 0
  1. <html>
  2. <head>
  3. <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script>
  4.  
  5. <script>
  6.     function initialize() {    
  7.         var defaultBounds = new google.maps.LatLngBounds(
  8.             new google.maps.LatLng(-33.8902, 151.1759),
  9.             new google.maps.LatLng(-33.8474, 151.2631));
  10.  
  11.         var input = document.getElementById('location');
  12.         var options = {
  13.             bounds: defaultBounds,
  14.             types: ['(cities)', '(regions)']
  15.         };
  16.        
  17.         autocomplete = new google.maps.places.Autocomplete(input, options);
  18.    
  19.         google.maps.event.addListener(autocomplete, 'place_changed', function() {
  20.             var place = autocomplete.getPlace();
  21.             var city;
  22.             var state;
  23.             var country;
  24.             var postcode;
  25.             var latitude;
  26.             var longitude;
  27.            
  28.             if (!place.geometry) {
  29.                 // Inform the user that the place was not found and return.
  30.                 input.className = 'notfound';
  31.                 return;
  32.             }
  33.            
  34.             if (place && place.geometry){
  35.                address_components = place.address_components;
  36.                 if (address_components){
  37.                     for (index in address_components){
  38.                         component = address_components[index];
  39.                         if (component.types.indexOf('locality') > -1){
  40.                             city = component.long_name;
  41.                         }
  42.                         if (component.types.indexOf('administrative_area_level_1') > -1){
  43.                             state = component.long_name;
  44.                             if (!state){
  45.                                 if (component.types.indexOf('administrative_area_level_2') > -1){
  46.                                     state = component.long_name;
  47.                                     if (!state){
  48.                                         if (component.types.indexOf('administrative_area_level_3') > -1){
  49.                                             state = component.long_name;
  50.                                         }
  51.                                     }
  52.                                 }
  53.                             }
  54.                         }
  55.                         if (component.types.indexOf('country') > -1){
  56.                             country = component.long_name;
  57.                         }
  58.                         if (component.types.indexOf('postal_code') > -1){
  59.                             postcode = component.long_name;
  60.                         }
  61.                     }
  62.                 }
  63.                 if (place.geometry.location){
  64.                     latitude = place.geometry.location.lat();
  65.                     longitude = place.geometry.location.lng();
  66.                 }
  67.             }else{
  68.                 // Inform the user that the place was not found and return.
  69.                 input.className = 'notfound';
  70.                 return;
  71.             }
  72.         });
  73.     }
  74.     google.maps.event.addDomListener(window, 'load', initialize);
  75. </script>
  76. </head>
  77. <body>
  78. <input type="text" id="location">
  79. </body>
  80. </html>
Add Comment
Please, Sign In to add comment