嘿,我正在鬼混谷歌地图API,目前我正在努力实现一些我认为会相当直接的事情。使用谷歌地图API时的Javascript范围问题
我的朋友想要在他的网站上有一个访问者地图的东西,访问者可以在地图上的任何点添加一个标记,每个标记都会有与之相关的信息。诸如名称,日期,消息等的信息...
现在我正处于原型设计和学习阶段,但我在将infowindow
与对象关联时遇到问题。
var map;
var markers = []
function init(){
var latlng = new google.maps.LatLng(-44, 140);
var myOptions = {
zoom:8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
google.maps.event.addListener(map, 'click', function(event){
markers.push(new marker(event.latLng));
});
}
function marker(location){
this.location = location;
this.mark = new google.maps.Marker({position:this.location, map:map});
this.info = new google.maps.InfoWindow({content:'hi'});
google.maps.event.addListener(this.mark, 'click', function(){
this.info.open(map, this.mark);
});
}
当我测试此代码出了标志放置很好,但是当我点击,而不是显示的信息窗口在他们身上,我得到这个错误在JS控制台:
Uncaught TypeError: Cannot call method 'open' of undefined
当然这是范围问题,但我不明白为什么应该如此。类似这样的工作正常:
function test(a){
this.a = a
this.b = function(){
this.a+=1
}
}
非常感谢您的帮助!
感谢它的工作,但我注意到这个解决方案的一个信息框,当它出现是所有碎片...可能是由于它被称为以这种方式? http://jsfiddle.net/8xHw7/ – danem
刚试过它在你的链接,似乎它对我来说工作得很好:) – VNO
真的吗?你看不到通过infowindow的这些线条?也许这是铬... – danem