Guest User

Google Place Auto Complete

a guest
Nov 5th, 2012
683
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.         input.className = '';
  28.            
  29.             if (place && place.geometry){
  30.                address_components = place.address_components;
  31.                 if (address_components){
  32.                     for (index in address_components){
  33.                         component = address_components[index];
  34.                         if (component.types.indexOf('locality') > -1){
  35.                             city = component.long_name;
  36.                         }
  37.                         if (component.types.indexOf('administrative_area_level_1') > -1){
  38.                             state = component.long_name;
  39.                             if (!state){
  40.                                 if (component.types.indexOf('administrative_area_level_2') > -1){
  41.                                     state = component.long_name;
  42.                                     if (!state){
  43.                                         if (component.types.indexOf('administrative_area_level_3') > -1){
  44.                                             state = component.long_name;
  45.                                         }
  46.                                     }
  47.                                 }
  48.                             }
  49.                         }
  50.                         if (component.types.indexOf('country') > -1){
  51.                             country = component.long_name;
  52.                         }
  53.                         if (component.types.indexOf('postal_code') > -1){
  54.                             postcode = component.long_name;
  55.                         }
  56.                     }
  57.                 }
  58.                 if (place.geometry.location){
  59.                     latitude = place.geometry.location.lat();
  60.                     longitude = place.geometry.location.lng();
  61.                 }
  62.             }else{
  63.                 // Inform the user that the place was not found and return.
  64.                 input.className = 'notfound';
  65.                 return;
  66.             }
  67.         });
  68.     }
  69.     google.maps.event.addDomListener(window, 'load', initialize);
  70. </script>
  71. </head>
  72. <body>
  73. <input type="text" id="location">
  74. </body>
  75. </html>
Add Comment
Please, Sign In to add comment