This is the code I’m working from:
The weird thing is, the autosuggest only seems to activate on the last input box after I click on the geocode button. This is the code im working from:
<html>
<head>
<script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>
<script>
function getLatLng() {
var geocoder = new google.maps.Geocoder();
var address = document.getElementById('address').value;
var input = document.getElementById('searchTextField');
var options = {
types: [],
};
var autocomplete = new google.maps.places.Autocomplete(input, options);
geocoder.geocode({
'address': address
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latLng = results[0].geometry.location;
$('#lat').val(results[0].geometry.location.lat());
$('#lng').val(results[0].geometry.location.lng());
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
</script>
</head>
<body>
<div>
<input id="address" type="textbox" value="Sydney, NSW">
<input type="button" value="Geocode" onclick="getLatLng()">
<input id="lat" type="textbox" value="lat">
<input id="lng" type="textbox" value="lng">
<input id="searchTextField" type="text" size="50" value="">
</div>
</body>
</html>
What gives? I’ve never had this before, but this is the first script I have tried to put together.
This is due to the following line:
inputlinks it to:which is the last input box.
In addition, you have to click on the
geocodebutton before Google autocomplete links because you have bound thegetLatLng()call to the click of the Geocode button:Because the Google autocomplete activates from within this function, you cannot initialize it without pressing the button.