I have an HTML form input element that uses a SearchBox from the Google Maps Places API for auto-completion. In my JavaScript I have created a SearchBox instance as follows:
var input = $('#spotsearch')[0]; // Refers to a form input with type="text" in my HTML
searchBox = new google.maps.places.SearchBox(input);
Later in in my JavaScript, I have the following:
$('#searchform').submit(function () {
var places = searchBox.getPlaces();
if (places) {
var spotLat = searchBox.getPlaces()[0].geometry.location.lat();
var spotLng = searchBox.getPlaces()[0].geometry.location.lng();
$('#latitude').val(spotLat);
$('#longitude').val(spotLng);
}
});
Here is my issue: after selecting a completion candidate, when I submit the form by clicking the form's submit button, everything works as expected, and places will reference an array with a single object. However, if I submit the form by pressing ENTER, the anonymous function still gets called, but places returns an empty array. In both cases, it seems that the request being sent is exactly the same. What am I missing here?
I am new to JavaScript and to the Goggle Maps API, so I am not sure where my mistake originates. My intuition is that it may be related to timing in some way, but that is really just a vague hunch. Any suggestions would be appreciated. Thank you!
Edit:
To provide a bit more detail, here is my entire script:
var searchBox;
var places;
function initSearchBox() {
var KCLatLng = new google.maps.LatLng({lat: 39.093201, lng: -94.573419});
var bounds = new google.maps.LatLngBounds(KCLatLng);
var input = $('#spotsearch')[0];
searchBox = new google.maps.places.SearchBox(input);
searchBox.setBounds(bounds);
$('#searchform').submit(function () {
var places = searchBox.getPlaces();
if (places) {
var spotLat = searchBox.getPlaces()[0].geometry.location.lat();
var spotLng = searchBox.getPlaces()[0].geometry.location.lng();
$('#latitude').val(spotLat);
$('#longitude').val(spotLng);
}
});
}
initSearchBox is then passed as the callback parameter when I load the Goggle Maps API.
I have also tried doing it this way:
var searchBox;
var places;
function initSearchBox() {
var KCLatLng = new google.maps.LatLng({lat: 39.093201, lng: -94.573419});
var bounds = new google.maps.LatLngBounds(KCLatLng);
var input = $('#spotsearch')[0];
searchBox = new google.maps.places.SearchBox(input);
searchBox.setBounds(bounds);
searchBox.addListener('places_changed', function() {
var places = searchBox.getPlaces();
if (places) {
var spotLat = searchBox.getPlaces()[0].geometry.location.lat();
var spotLng = searchBox.getPlaces()[0].geometry.location.lng();
$('#latitude').val(spotLat);
$('#longitude').val(spotLng);
}
});
}
But this gives me the same result (hitting ENTER does not set the value of #latitude or #longitude, but clicking the submit button does).
Here is the HTML of my form (using Django templates and Bootstrap):
<form method="get" id="searchform" action="{% url 'spot_search' %}"
class="navbar-form navbar-left plain" role="search">
<div class="form-group">
<input id="spotsearch" type="text" class="form-control plain"
placeholder="Search spots" name="search_query">
</div>
<button type="submit" class="btn btn-default plain">
<span class="glyphicon glyphicon-search"></span> Search
</button>
<input id="latitude" name="latitude" type="hidden">
<input id="longitude" name="longitude" type="hidden">
</form>