简体   繁体   English

Google Maps Javascript API v3函数getPlace()返回未定义

[英]Google Maps Javascript API v3 function getPlace() returns undefined

I am using the Google Place service to obtain the place details for a result coming out from an auto complete. 我正在使用Google地方信息服务来获取地方信息,以获取自动完成功能产生的结果。 The problem is that executing getPlace() request on my autocomplete object returns undefined for the variable (var place). 问题是,在我的自动完成对象上执行getPlace()请求会返回未定义的变量(变量位置)。 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] TypeError:地点未定义[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. 谢谢你的帮助。

Closure issue: loop index is used as 1 (value when loop finishes) instead of 0. 封闭问题:循环索引用作1(循环结束时的值)而不是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 现在出现另一个问题: points is not defined

You are using variable points which seems has to contain information about markers. 您使用的变量points似乎必须包含有关标记的信息。 It is not defined in your code. 您的代码中未定义它。

Update: Code as it is written now sets event listener to departure autocomplete only. 更新:现在编写的代码将事件侦听器仅设置为departure自动完成。 See for loop. for循环。 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. 唯一有效的索引是0。如果我加载页面并将s写入出发输入,则可以选择例如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. 选择后,我得到的控制台指数1(这是错误的), GoogleMapItems[LoopIndex]返回arrival (这是错误的)和places是未定义这是正确的,因为没有什么在到达自动完成输入。 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. 通过更改代码和从出发自动完成中进行选择,我得到了循环索引0, GoogleMapItems[LoopIndex]返回departure ,获得了地点的完整信息,地图被缩放到了悉尼,并且代码失败,因为未定义可变points

So, variable points has to be defined somewhere and for loop has to be expanded to handle also other autocomplete parts of page. 因此,必须在某个位置定义变量points ,并且必须扩展for循环以处理页面的其他自动完成部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM