Question

I am using the google places api in my website.When i focus on the input field and start writing the autocomplete works fine but when i press enter it gets submitted without the value.I get the error: "cannot read property "location" of undefined".

Javascript:

<script type="text/javascript">
google.maps.event.addDomListener(window, 'load', initialize);

function initialize() {
    var input = document.getElementById('main_search');
    var defaultBounds = new google.maps.LatLngBounds(
        new google.maps.LatLng(22.4667, 88.3067),
        new google.maps.LatLng(22.8667, 88.467));
    var options = {
        bounds: defaultBounds,
        types: ['establishment'],
        componentRestrictions: {
            country: 'in'
        }
    };
    var autocomplete = new google.maps.places.Autocomplete(input, options);



    google.maps.event.addListener(autocomplete, 'place_changed', function() {
        var place = autocomplete.getPlace();
        document.getElementById('city').value = place.name;
        document.getElementById('cityLat').value = place.geometry.location.lat();
        document.getElementById('cityLng').value = place.geometry.location.lng();
    });
}
</script>

i am using the get method and i don't get any value.when i use mouse and click on submit button everything works fine.

Was it helpful?

Solution

Temporary fix

Disable the enter default when pac-container (autocomplete dropdown) is visible

 $('#search-entry').keydown(function(e) {
if (e.which == 13 && $('.pac-container:visible').length) return false;
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top