Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function setupListeners() {
- // google.maps.event.addDomListener(window, 'load', initialize);
- // searchbox is the var for the google places object created on the page
- google.maps.event.addListener(searchbox, 'place_changed', function() {
- var place = searchbox.getPlace();
- if (!place.geometry) {
- // Inform the user that a place was not found and return.
- return;
- } else {
- // migrates JSON data from Google to hidden form fields
- populateResult(place);
- }
- });
- };
- function populateResult(place) {
- var componentForm = {
- locality: 'long_name',
- administrative_area_level_1: 'short_name',
- administrative_area_level_2: 'short_name',
- country: 'long_name',
- postal_code: 'short_name'
- };
- // 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];
- //console.log(place); // Uncomment this line to view the full object returned by Google API.
- if (componentForm[addressType]) {
- var val = place.address_components[i][componentForm[addressType]];
- $('#register-form-'+addressType).val(val);
- }
- }
- $('#register-form-latitude').val(place.geometry.location.lat());
- $('#register-form-longitude').val(place.geometry.location.lng());
- $('#register-form-place_id').val(place.place_id);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement