Google Places Autocomplete Widget
by woosmap
HTML
<input id="my-input-searchbox" type="text" placeholder="Autocomplete Widget">
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&libraries=places"></script>
CSS
#map {
height: 100%;
}
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
#my-input-searchbox {
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.16), 0 0 0 1px rgba(0, 0, 0, 0.08);
font-size: 15px;
border-radius: 3px;
border: 0;
margin-top: 10px;
width: 270px;
height: 40px;
text-overflow: ellipsis;
padding: 0 1em;
}
JavaScript
function initAutocomplete() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {
lat: 48,
lng: 4
},
zoom: 4,
disableDefaultUI: true
});
// Create the search box and link it to the UI element.
var input = document.getElementById('my-input-searchbox');
var autocomplete = new google.maps.places.Autocomplete(input);
map.controls[google.maps.ControlPosition.TOP_CENTER].push(input);
var marker = new google.maps.Marker({
map: map
});
// Bias the SearchBox results towards current map's viewport.
autocomplete.bindTo('bounds', map);
// Set the data fields to return when the user selects a place.
autocomplete.setFields(
['address_components', 'geometry', 'name']);
// Listen for the event fired when the user selects a prediction and retrieve
// more details for that place.
autocomplete.addListener('place_changed', function() {
var place = autocomplete.getPlace();
if (!place.geometry) {
console.log("Returned place contains no geometry");
return;
}
var bounds = new google.maps.LatLngBounds();
marker.setPosition(place.geometry.location);
if (place.geometry.viewport) {
// Only geocodes have viewport.
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
map.fitBounds(bounds);
});
}
document.addEventListener("DOMContentLoaded", function(event) {
initAutocomplete();
});