Google Places API - place_changed desktop does not work

I am trying to use autocomplete from the Google Places API. I have autocomplete configured and working, but I want to add a listener so that I can use the information from PlacesResult elsewhere.

To test the functionality, I am trying to prove that the listener works by creating a warning window and writing place.name to a. Nothing works, so the listener is clearly configured incorrectly, I suppose, but I cannot understand how I wrote it incorrectly.

<!-- Load Places Library for Google Maps-->
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=false">
</script>

<!-- Autocomplete Script -->
<script type="text/javascript">
function initialize(){
    var input = document.getElementById('name');
    var autocomplete = new google.maps.places.Autocomplete(input);
}
google.maps.event.addDomListener(window, 'load', initialize);

google.maps.event.addListener(autocomplete, 'place_changed', function(){
    var place = autocomplete.getPlace();
    alert("This function is working!");
    document.getElementById("receiver").innerHTML=place.name;
});
</script>
+5
source share
1 answer

Move the call to . addListener initialize

addListener initialize, autocomplete ( )

+4

All Articles