My goal is to retrieve the data from autocomplete.getPlace()
when triggering this event manually.
Essentially, I have a function that captures the user's location in Lat/Lng
format, and afterward, I aim to access other useful data using getPlace() such as place_id, photos, reference, etc.
Currently, I can perform almost all of the tasks successfully, including manually firing the event. However, it returns undefined
. Here is the snippet of my code:
var latLng = response; //Obtained from another function
geocoder.geocode({'location': latLng}, function(results, status) {
if (status === 'OK') {
if (results[1]) {
var fullAddress = results[0].formatted_address;
var input = document.createElement("input");
input.setAttribute("value", fullAddress);
var autocomplete = new google.maps.places.Autocomplete(input);
google.maps.event.addListener(autocomplete, 'place_changed', function () {
var thisPlace = autocomplete.getPlace();
console.log(thisPlace);
});
google.maps.event.trigger(autocomplete, 'place_changed');
}
}
});
While everything seems to be working fine here, the result from autocomplete.getPlace()
shows up as undefined
.
Interestingly, running the same function but with a native autocomplete element yields the correct result.
What could possibly be causing this issue?