首页 > 解决方案 > Google 地方信息自动填充功能有时不工作

问题描述

地方自动完成(javascript)在很多方面都很奇怪。我的观点是,它在某些浏览器上似乎一直有效,但在其他浏览器上却断断续续。为了到达这里,我不得不做一些与其他建议相反的事情,(尽管“正确”的方法更有效)所以我有很多东西要列出。

具体问题(在任何浏览器上)是回调 initAutocomplete() 确实完全发生,但有时输入字段不会出现建议。但是,没有记录错误。当它失败时,我发现了两件值得注意的事情:没有创建 pac-container,并且没有为 AutocompletionService.GetPredictions 发送请求。

这些错误似乎从未在 FireFox 或 Edge 上发生过。它大部分时间都发生在 Chrome 上。它永远不会在 iOS Chrome 上运行,但很少在 Safari 上运行......到目前为止,这不是一个有用的模式。

页面上多次调用 Maps API,导致“您已在此页面上多次包含 Google Maps JavaScript API。这可能会导致意外错误。” 错误。但是,这似乎没有实际问题,因为调用是针对不同的库(几何和位置)。至少,地方脚本的回调函数绝对总是起作用。

除此之外,没有任何类型的错误报告。值得注意的是,我目前正在附加一个实际上基于会话的会话令牌(对于用户,因此对于多个请求。)这与stackoverflow.com/a/50452233/5140781等信息直接相反(我搜索了一个发布前很多)说不需要 sessiontoken 并将自动处理。这是相反的,因为不包括它会使服务中断的频率更高. 如果没有它,它会在 Firefox 和 Edge 上占用大量时间,目前还不错,Chrome 在每个用户会话中只能工作一次,在一次刷新或任何后续形式之后,它肯定不会再工作了。我还尝试在每个页面加载时附加一个随机会话令牌,但这也无济于事。尽管在所有情况下,错误都是相当随机的,但错误可能与会话令牌无关,而我所看到的一切都只是人类模式寻找和 [坏] 运气。据我所知,这可能只是不同 API 调用的竞争条件。如果我执行“空缓存和硬重新加载”而不仅仅是 F5,我可以让它在 Chrome 上运行得更多;将其添加到它通常在会话中第一次工作,也许您认为从缓存中加载脚本比实际加载时更有可能导致问题?我没有想法,或者至少我认为是合理的。

initAutocomplete 的代码几乎与示例中给出的完全一样,除了添加 sessiontoken (同样,没有它会失败更多)。

任何帮助,将不胜感激。

标签: javascriptgoogle-chromegoogle-maps-api-3google-places-apigoogleplacesautocomplete

解决方案


It was, in fact, the double inclusion of a google API. This is despite the fact one include library was geometry and this was places, and that the callback on the Place script was happening even in fail states..

Simply removing the geometry include on the relevant pages fixed the Autocomplete functionality 100% (and we could drop the sessiontoken), but geometry was needed for other functions on the page. That was solved by adding it to the include made for the place library. Simply use commas to target multiple libraries; I didn't see that mentioned in the documentation, but is was an easy guess:

<script src="https://maps.googleapis.com/maps/api/js?key=********&libraries=places,geometry&callback=initAutocomplete" async defer></script>

Now, if you need geometry (or whatever other library) higher and sooner than your place include, that is a new problem I did not have. But for anyone finding this, my most simple recommendation would be moving the whole autocomplete chunk up there and combining the include in the same way. There are then 2 possible issues: The other script already having a callback, and the form potentially not existing yet.

The callbacks can be combined trivially if only the first point is your issue; just do the other callback code then the autocomplete. For just the second, there are two options: First, if you can have your form appearing trigger an event, then have the callback set a listener for it to then to its usual (watch for the form possibly loading first though!). Second, a setInterval that checks for the element existing and does the work when it is found (and stops checking)--that is dirty, but will work with just vanilla js; there is probably a better option if whatever framework you have. If you have both problems, just have the listener/interval at the start of the callback.


推荐阅读