javascript Google Maps Api 3:未捕获的类型错误:无法调用未定义的方法“setCenter”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9636773/
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-10-26 07:18:04  来源:igfitidea点击:

Google Maps Api 3: Uncaught TypeError: Cannot call method 'setCenter' of undefined

javascriptmaps

提问by Mike Averto

I have been trying to get my geocoder working this morning for the following URL but can't seem to figure out what I am doing wrong: http://www.dlplaw.com/media/map.html

今天早上我一直试图让我的地理编码器为以下 URL 工作,但似乎无法弄清楚我做错了什么:http: //www.dlplaw.com/media/map.html

When clicking encode, the geocoder is doing it's job and returning the correct Lat / Long coordinates but it gets hung up on setCenter.

单击编码时,地理编码器正在执行其工作并返回正确的纬度/经度坐标,但它在 setCenter 上挂断了。

Uncaught TypeError: Cannot call method 'setCenter' of undefined

I tried to follow the Google example here and can't understand why this won't work: http://code.google.com/apis/maps/documentation/javascript/examples/geocoding-simple.html

我试图在此处遵循 Google 示例,但不明白为什么这不起作用:http: //code.google.com/apis/maps/documentation/javascript/examples/geocoding-simple.html

Thanks in advance.

提前致谢。

回答by josh3736

mapis not in scope. You declare maptwice – once in the global scope and once in initialize, but you only set the mapin initialize(the global mapis shadowed).

map不在范围内。你声明了map两次——一次在全局范围内,一次在 中initialize,但你只设置了mapin initialize(全局map被遮蔽)。

Remove the varfrom var map = new google.maps.Map(...);(but not the var map;at the top).

删除varfrom var map = new google.maps.Map(...);(但不是var map;顶部)。