Advertisement
Bennd

web/js/main.js

Dec 10th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function setupListeners() {
  2. //  google.maps.event.addDomListener(window, 'load', initialize);
  3.     // searchbox is the var for the google places object created on the page
  4.     google.maps.event.addListener(searchbox, 'place_changed', function() {
  5.         var place = searchbox.getPlace();
  6.         if (!place.geometry) {
  7.             // Inform the user that a place was not found and return.
  8.             return;
  9.         }  else {
  10.             // migrates JSON data from Google to hidden form fields
  11.             populateResult(place);
  12.         }
  13.     });
  14. };
  15.  
  16. function populateResult(place) {
  17.     var componentForm = {
  18.         locality: 'long_name',
  19.         administrative_area_level_1: 'short_name',
  20.         administrative_area_level_2: 'short_name',
  21.         country: 'long_name',
  22.         postal_code: 'short_name'
  23.     };
  24.  
  25.     // Get each component of the address from the place details
  26.     // and fill the corresponding field on the form.
  27.     for (var i = 0; i < place.address_components.length; i++) {
  28.         var addressType = place.address_components[i].types[0];
  29.         //console.log(place);  // Uncomment this line to view the full object returned by Google API.
  30.  
  31.         if (componentForm[addressType]) {
  32.             var val = place.address_components[i][componentForm[addressType]];
  33.             $('#register-form-'+addressType).val(val);
  34.         }
  35.     }
  36.     $('#register-form-latitude').val(place.geometry.location.lat());
  37.     $('#register-form-longitude').val(place.geometry.location.lng());
  38.     $('#register-form-place_id').val(place.place_id);
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement