Last active
December 28, 2020 10:44
-
-
Save code-nation/eaf1fc1687949727f5f5 to your computer and use it in GitHub Desktop.
Script to use Google Maps API and run an address autocomplete function as part of a form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript" src="//maps.googleapis.com/maps/api/js?libraries=places"></script> | |
<script type="text/javascript"> | |
/* Create address autocomplete */ | |
var autocompleteOptions = { | |
types: ['address'], | |
// restrict to only Aussie addresses - change 'au' to any other 2-letter country code as you please | |
componentRestrictions: {country: 'au'} | |
}; | |
function initAutocomplete() { | |
// Change the ID on the line below as required | |
var input = document.getElementById('signup_submitted_address'); | |
autocomplete = new google.maps.places.Autocomplete(input, autocompleteOptions); | |
// Uncomment the line below if you wish to call the fillInAddress example function on user selection | |
// autocomplete.addListener('place_changed', fillInAddress); | |
} | |
google.maps.event.addDomListener(window, 'load', initAutocomplete); | |
// Optional function to split address components when a user selects a location | |
// For this example to work you should add 4 empty inputs to your form. | |
// Give the empty fields the IDs 'street', 'suburb', 'state' and 'postcode' | |
// Just for fun add a div with ID 'static-map' and we'll print a mapped address | |
// I should update this at some point to support unit numbers or whatevs | |
var componentForm = { | |
street_number: 'short_name', | |
route: 'long_name', | |
locality: 'long_name', | |
administrative_area_level_1: 'short_name', | |
country: 'short_name', | |
postal_code: 'short_name' | |
}; | |
function fillInAddress() { | |
var place = autocomplete.getPlace(); | |
if (place.geometry) { | |
var addressObj = {}; | |
for (var i = 0; i < place.address_components.length; i++) { | |
var addressType = place.address_components[i].types[0]; | |
var val = place.address_components[i][componentForm[addressType]]; | |
addressObj[addressType] = val; | |
} | |
addressObj.street_number ? $('#street').val(addressObj.street_number + ' ' + addressObj.route) : $('#street').val(addressObj.route); | |
$('#suburb').val(addressObj.locality); | |
$('#state').val(addressObj.administrative_area_level_1); | |
$('#postcode').val(addressObj.postal_code); | |
var staticMap = '<img src="//maps.googleapis.com/maps/api/staticmap?center='+place.geometry.location+'&zoom=16&size=360x190&maptype=roadmap&format=jpg&markers=color:0xe91547%7C'+place.geometry.location+'" />'; | |
$('#static-map').html(staticMap); | |
} else { | |
// set an error - the user didn't provide a complete address | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i am a dummy .. possible to have full html file ..
with the form include.
thanks