Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Events other than 'place_changed' for Google Maps Autocomplete

I have an app that currently fires correctly on place_changed.

However, I want to branch search to behave differently when a user has selected an autocomplete entry, and when they have entered text on their own without the assistance of autocomplete.

What kind of event listener should I use to make the distinction? I cannot find any documentation on other events for Google Maps Autocomplete.

what I have now:

var gmaps = new google.maps.places.Autocomplete($("#searchproperties").get(0),  { types: ['geocode'], componentRestrictions: {country: 'us'} });  google.maps.event.addListener(gmaps, 'place_changed', function () {     //FIRE SEARCH }); 
like image 504
Wesley Avatar asked Aug 24 '15 22:08

Wesley


People also ask

How do I restrict Google Maps autocomplete to certain cities?

It is currently not possible to restrict results to a specific locality. You can use bounds as you have done so above to bias results towards, but not restricted to places contained within the specified bounds. If you believe restriction by locality would be a useful feature please file a Places API - Feature Request.

Is Google address autocomplete API free?

First of all, the new Google Place Autocomplete API is not free (after exceeding the US$200 monthly credit equivalent free usage). Now coming to the pricing, Google has introduced three possible pricing criteria: Autocomplete without Places Details – Per Session. Autocomplete (included with Places Details) – Per ...

Is Google Maps autocomplete free?

The autocomplete request is available at no charge, and the subsequent Place Details call gets charged based on regular Place Details pricing. A Place Details request generates Data SKUs (Basic, Contact, and/or Atmosphere) – depending on the fields that are specified in the request.


2 Answers

There is only one documented event in the Google Maps Javascript API v3 for the google.maps.places.Autocomplete class, place_changed

You can add standard HTML event listeners to it (not sure if that will affect the Autocomplete functionality).

like image 170
geocodezip Avatar answered Sep 18 '22 22:09

geocodezip


This comes down to checking whether you receive a place.geometry object, as it is shown in their official example. As simple as that!

function initialize() {      var ac = new google.maps.places.Autocomplete(      (document.getElementById('autocomplete')), {        types: ['geocode']      });      ac.addListener('place_changed', function() {        var place = ac.getPlace();        if (!place.geometry) {        // User entered the name of a Place that was not suggested and        // pressed the Enter key, or the Place Details request failed.        // Do anything you like with what was entered in the ac field.        console.log('You entered: ' + place.name);        return;      }        console.log('You selected: ' + place.formatted_address);    });  }    initialize();
#autocomplete {    width: 300px;  }
<input id="autocomplete" placeholder="Enter your address" type="text"></input>  <script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>
like image 28
MrUpsidown Avatar answered Sep 18 '22 22:09

MrUpsidown