jQuery google.maps.places 及其自动完成方法未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18900249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 22:43:45  来源:igfitidea点击:

google.maps.places and its Autocomplete method are undefined

javascriptjquerygoogle-maps

提问by Luke

I'm loading the Google Maps API, jQueryand this Geocompleteplugin. Please note that I'm also specifying the libraries (libraries=places) and eventually my API key:

我正在加载Google Maps APIjQuery和这个Geocomplete插件。请注意,我还指定了库 ( libraries=places) 并最终指定了我的API 密钥

<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'></script>
<script src='http://maps.googleapis.com/maps/api/js?v=3&sensor=false&amp;libraries=places&key=xxx'></script>
<script src='js/vendor/jquery.geocomplete.js'></script>

I'm triggering the Geocomplete plugin on the page load

我在页面加载时触发 Geocomplete 插件

$(window).load(function () {
    $('#my-input').geocomplete();
});

but I always get an error:

但我总是收到一个错误:

Uncaught TypeError: Cannot read property 'Autocomplete' of undefined [jquery.geocomplete.js:153].

未捕获的类型错误:无法读取未定义 [jquery.geocomplete.js:153] 的属性“自动完成”

Inside the plugin...

插件里面...

this.autocomplete = new google.maps.places.Autocomplete(
 this.input, options
);

I tried to google.mapsand it returns a regular object, but places is undefined!

我尝试使用google.maps并返回一个常规对象,但地点未定义

回答by geocodezip

I tried to google.maps and it returns a regular object, but places is undefined!

我尝试使用 google.maps 并返回一个常规对象,但地点未定义!

That means the google.maps.places library is not loading (the line of code that you posted, shown below is either not correct or is not actually on your page):

这意味着 google.maps.places 库未加载(您发布的代码行,如下所示要么不正确,要么实际上不在您的页面上):

<script src='http://maps.googleapis.com/maps/api/js?v=3&sensor=false&amp;libraries=places&key=xxx'></script>

This is the example from the documentation:

这是文档中的示例:

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=false"></script>

The only obvious difference is the &amp;in yours, that should work, but you should probably use it consistently.

唯一明显的区别是&amp;你的,它应该可以工作,但你应该始终如一地使用它。

TL;DR

TL; 博士

Use &librariesnot &amp;librariesand it will work.

&libraries不使用&amp;libraries它会起作用。