With a few searches, a solution could be found.
The request was approved and released earlier this year: Google Request
You can read the documentation for this information here: Google Documentation
Implementation:
HTML:
<div id="text"> Search Place: </div> <div id="locationField"> <input id="autocomplete" placeholder="Enter text" type="text" /> </div> <div id="map"></div>
Limit in which country the global variable is used, UK for the UK, USA for the Americas, etc.
var countryRestrict = { 'country': 'uk' }; var autocomplete;
Then, automatic completion automatically ends using the following:
autocomplete = new google.maps.places.Autocomplete( ( document.getElementById('autocomplete')), { componentRestrictions: countryRestrict }); places = new google.maps.places.PlacesService(map); autocomplete.addListener('place_changed', onPlaceChanged);
When changing the text, we call the onPlacesChanged function:
function onPlaceChanged() { var place = autocomplete.getPlace(); if (place.geometry) { map.panTo(place.geometry.location); map.setZoom(5); var marker = new google.maps.Marker({ position: place.geometry.location, map: map, }); } else { document.getElementById('autocomplete').placeholder = 'Enter a Value'; } }
This calls the get place function and adds a marker to the value of your choice.
JSFIDDLE: https://jsfiddle.net/hn8091fk/
John m
source share