Reputation: 936
I am using the Google Place service to obtain the place details for a result coming out from an auto complete. The problem is that executing getPlace() request on my autocomplete object returns undefined for the variable (var place). Have been around this issue for days now, can't get on it.
My page is : here, and the example I am following is here.
error:
TypeError: place is undefined [testdebug.php:232]
main pieces of code:
window['auto_'+inputFieldID+'_autocomplete'] = new google.maps.places.Autocomplete(document.getElementById(inputFieldID));
window['auto_'+inputFieldID+'_autocomplete'].bindTo('bounds', map);
var place = window['auto_'+GoogleMapItems[LoopIndex]+'_autocomplete'].getPlace();
if (!place.geometry)
{
console.log('cannot resolve rendering.');
}
Thanks for your help.
Upvotes: 0
Views: 1329
Reputation: 11258
Closure issue: loop index is used as 1 (value when loop finishes) instead of 0.
You can fix it using closure for loop index around event listener:
(function(LoopIndex) {
google.maps.event.addListener(window['auto_'+GoogleMapItems[LoopIndex]+'_autocomplete'], 'place_changed', function()
... {
});
})(LoopIndex);
This is one fix. Now another issue occurs: points is not defined
You are using variable points
which seems has to contain information about markers. It is not defined in your code.
Update: Code as it is written now sets event listener to departure
autocomplete only. See for
loop. The only valid index is 0. If I load the page and write for example s
into departure input I can select for example Sydney, New South Wales, Australia. After selection I get in console index 1 (which is wrong), GoogleMapItems[LoopIndex]
returns arrival
(which is wrong) and places
are undefined which is correct because there is nothing in arrival autocomplete input. This is typical closure issue.
With my code change and selection from departure autocomplete I get loop index 0, GoogleMapItems[LoopIndex]
returns departure
, I get complete information for place, map is zoomed to Sydney and code fails because variable points
is undefined.
So, variable points
has to be defined somewhere and for loop has to be expanded to handle also other autocomplete parts of page.
Upvotes: 1