Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google maps Places API V3 autocomplete - select first option on enter (and have it stay that way) [duplicate]

This question relates to the answer for this one: Google maps Places API V3 autocomplete - select first option on enter. Basically, it is to make the field use the first suggestion from the autocomplete list when the user presses enter. The answer for that question has a jsfiddle - http://jsfiddle.net/dodger/pbbhH/ - which works except when the text field loses focus, the field value returns to the partially entered value.

For example, a user clicks in the input field and types 'ox', the autocomplete box pops up with some suggestions, and the user presses enter. The map is then changed to show the location of the first item from the autocomplete box (with a marker), and the value of the input field is changed to the first item from the autocomplete box. The user then clicks somewhere outside the field and the value of the input field returns to 'ox'.

I would like the value of the input field to stay as the first autocomplete suggestion.

like image 485
Ben Avatar asked May 27 '12 07:05

Ben


People also ask

How do I limit Google autocomplete results to state only?

Alternatively, use types=establishment to restrict results to establishments only. You may also restrict results to the region defined by location and a radius parameter, by adding the strictbounds parameter. This instructs the Place Autocomplete service to return only results within that region.

Is Google Maps API no longer free?

Note that the Maps Embed API, Maps SDK for Android, and Maps SDK for iOS currently have no usage limits and are at no charge (usage of the API or SDKs is not applied against your $200 monthly credit).


2 Answers

Try this: http://jsfiddle.net/pbbhH/60/

Basically abstracted the selection logic to a new function selectFirstResult(). Then called this function on both pressing enter and losing focus on text.

 function selectFirstResult() {
    infowindow.close();
    var firstResult = $(".pac-container .pac-item:first").text();

    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({"address":firstResult }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var lat = results[0].geometry.location.lat(),
                lng = results[0].geometry.location.lng(),
                placeName = results[0].address_components[0].long_name,
                latlng = new google.maps.LatLng(lat, lng);

            moveMarker(placeName, latlng);
            $("input").val(firstResult);
        }
    });   
 }

EDIT: made minor change per @Ben's comment below.

like image 79
Mandar Limaye Avatar answered Oct 18 '22 21:10

Mandar Limaye


This is right but if u press enter and you have already selected an item this will select the first. So use this code:

function selectFirstResult() {
infowindow.close();
if ( $('.pac-selected').length < 0){ // this line

var firstResult = $(".pac-container .pac-item:first").text();
var geocoder = new google.maps.Geocoder();
geocoder.geocode({"address":firstResult }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        var lat = results[0].geometry.location.lat(),
            lng = results[0].geometry.location.lng(),
            placeName = results[0].address_components[0].long_name,
            latlng = new google.maps.LatLng(lat, lng);

        moveMarker(placeName, latlng);
        $("input").val(firstResult);
    }
});
}   
}
like image 1
Dtnand Avatar answered Oct 18 '22 19:10

Dtnand