Javascript 未捕获的类型错误:无法设置未定义的属性“位置”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8483023/
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
Uncaught TypeError: Cannot set property 'position' of undefined
提问by jfriend00
I have this code giving me the strange error message
我有这个代码给了我奇怪的错误信息
Uncaught TypeError: Cannot set property 'position' of undefined
This is the inside of a jQuery plugin to show a google map in a popup. I was using the code somewhere else, where it worked fine - the only difference here seems to be that I'm now using it in a Popup window. Am I missing a scope issue or something? All the variables like geocoderParams and latlng are filled like they should. Googling the the error message turned up nothing valuable.
这是 jQuery 插件的内部,用于在弹出窗口中显示谷歌地图。我在其他地方使用了代码,在那里它工作得很好 - 这里唯一的区别似乎是我现在在弹出窗口中使用它。我错过了范围问题还是什么?像 geocoderParams 和 latlng 这样的所有变量都按应有的方式填充。谷歌搜索错误消息没有发现任何有价值的信息。
The error message gets fired when the google.maps.Map() is called.
调用 google.maps.Map() 时会触发错误消息。
self = $(this)
self.hide()
geocoder = new google.maps.Geocoder
geocoderParams =
address: self.data('address') || settings.address
region: settings.region
results = geocoder.geocode geocoderParams, (results, status) ->
if status == google.maps.GeocoderStatus.OK
latlng = results[0].geometry.location
mapOptions =
mapTypeControl: false
overviewMapControl: false
zoom: settings.zoomLevel
center: latlng
mapTypeId: google.maps.MapTypeId.ROADMAP
map = new google.maps.Map(self, mapOptions)
self.show()
回答by jfriend00
I looked up google.maps.Map()
and the Google referencesays that the first parameter should be a mapDiv:Node
which is the container for the map and is typically a div
element.
我查了一下google.maps.Map()
,谷歌参考说第一个参数应该是 a mapDiv:Node
,它是地图的容器,通常是一个div
元素。
You are passing $(this)
which is likely a jQuery object which is not what Google maps is expecting. I don't know the rest of your code, but perhaps you should just be passing this
instead of $(this)
.
您正在传递$(this)
这可能是一个 jQuery 对象,这不是 Google 地图所期望的。我不知道您的其余代码,但也许您应该只通过this
而不是$(this)
.
回答by Ryan
If you're using jQuery, you need to pass the vanilla HTMLElement
(e.g. Node
) instead of the jQuery selection (e.g. Array
/NodeList
) which the $()
query returns.
如果您使用 jQuery,则需要传递 vanilla HTMLElement
(例如Node
)而不是查询返回的 jQuery 选择(例如Array
/ NodeList
)$()
。
if you add a console command:
如果添加控制台命令:
console.log(self);
it will return something like:
它会返回类似的东西:
[ ? <div id="map_canvas"></div> ]
to get the value from the jQuery array, replace:
要从 jQuery 数组中获取值,请替换:
map = new google.maps.Map(self, mapOptions)
with:
和:
map = new google.maps.Map(self[0], mapOptions)
回答by mattsoave
I encountered this error when trying to initiate the map with something like this:
尝试使用以下内容启动地图时遇到此错误:
map = new google.maps.Map($('#map-canvas'), mapOptions);
I was able to solve it by selecting the first (and only) item that the selector returns by adding [0]
after the selector:
我能够通过在选择器[0]
之后添加选择器返回的第一个(也是唯一一个)项目来解决它:
map = new google.maps.Map($('#map-canvas')[0], mapOptions);