Created
March 29, 2018 15:42
-
-
Save ProfAndreaPollini/1d7d6c0f3dbd7fcdb1a72cf0d5532f43 to your computer and use it in GitHub Desktop.
A HTML page that uses Google Place API Autocomplete to update some div content
This file contains hidden or 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
<html> | |
<head> | |
</head> | |
<body> | |
<input type="text" id="search-input" placeholder="Inserire la località"> | |
<div id="city"> | |
città | |
</div> | |
<div id="country"> | |
stato | |
</div> | |
<script> | |
function initAutocomplete() { | |
console.log("entering initAutocomplete()"); | |
var input = document.getElementById('search-input'); | |
var autocomplete = new google.maps.places.Autocomplete(input); | |
google.maps.event.addListener(autocomplete,"place_changed",onPlaceChanged); | |
function onPlaceChanged() { | |
console.log("[*] on place changed event"); | |
var place = autocomplete.getPlace(); | |
if(!place.address_components) | |
return; | |
var city = place.address_components[0].long_name; | |
var n = place.address_components.length; | |
var country = place.address_components[n-1].long_name; | |
document.getElementById('city').innerHTML = city; | |
document.getElementById('country').innerHTML = country; | |
} | |
} | |
</script> | |
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAOJ_xtO9BpG_9VDtSeGttVXSrzT0bq-uo&libraries=places&callback=initAutocomplete" | |
async defer></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment