0

Im trying to run a Function after the user select a address on the Google Address List. I tried with onBlur but the action would fire before the user could select the address giving me an incomplete one.

I read these topics:

How to fire place_changed event for Google places auto-complete on Enter key

Google Places API - place_changed listener not working

Events other than 'place_changed' for Google Maps Autocomplete

And I tried with place_changed but still I cant get to run my function.

HTML

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">    
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=EDITED(MY KEY)&libraries=places"></script><script src="mapa.js" language="Javascript" type="text/javascript"></script>
<script type="text/javascript" src="jquery-ui.custom.min.js"></script>
<script type="text/javascript" src="jquery.min.js"></script>

</head>

<body>


          <input type="text" name="street" id="0" onFocus="autoComplete(this);">


</body>
</html>

JavaScript

function autoComplete(adress)
{

    var input = document.getElementById(adress.id);
    var options = { componentRestrictions: {
      country: 'fr'

    }};

    new google.maps.places.Autocomplete(input, options);

    google.maps.event.addListener(gmaps, 'place_changed', function () {
       getPlace(adress); 
    });

}

function getPlace(adress)
{
   alert(adress.value);   
}

What I am missing?

For an alternative solution is there anyway to make the onBlur or onChange only fire after the user select something on the auto complete list? This would be a better solution to my problem I think.

Any help is appreciated.

Community
  • 1
  • 1
Adrian
  • 315
  • 2
  • 3
  • 17
  • How are you including the API? Are you including the places library? Please provide a [Minimal, Complete, Tested and Readable example](http://stackoverflow.com/help/mcve) that demonstrates your issue. – geocodezip Mar 07 '16 at 06:08
  • Sorry geocode. I edited to put the entire code into my question. I edited my Google Key for security. – Adrian Mar 07 '16 at 11:19

1 Answers1

0

Ok i saw what i was missing.

On the new google.maps.places.Autocomplete(input, options); i had to save it on a variable like gmaps = new google.maps.places.Autocomplete(input, options); and then call it on the Listener:

google.maps.event.addListener(gmaps, 'place_changed', function () {
   getPlace(adress); 
});

Rookie mistake.

Adrian
  • 315
  • 2
  • 3
  • 17