【发布时间】:2010-02-21 17:50:51
【问题描述】:
我是 Javascript 和 Google 地图的新手。我的任务是:
- 允许用户在地图上添加标记
- 当用户点击标记时显示一个信息窗口
- 如果标记指向吉隆坡市中心,则在信息窗口中显示“吉隆坡市中心”,而其他位置则显示“未知位置”。
我的问题是添加标记后我什至无法显示信息窗口。我可以在同一个函数中添加一个新事件吗?该代码仅适用于添加标记。下面的代码是我从谷歌地图编辑后的:
函数 MyApplication() {
this.counter = 0;
this.map = new GMap2(document.getElementById("map_canvas"));
this.map.setCenter(new GLatLng(3.145423,101.696883), 13);
var myEventListener = GEvent.bind(this.map, "click", this, function(overlay, latlng) {
if (this.counter == 0) {
if (latlng) {
this.map.addOverlay(new GMarker(latlng))
this.counter++;
} else if (overlay instanceof GMarker) {
//This code is never executed as the event listener is
//removed the second time this event is triggered
this.removeOverlay(marker)
}
} else {
GEvent.removeListener(myEventListener);
}
});
}
function initialize() {
var application = new MyApplication();
}
函数 createMarker(latlng) {
var marker = new GMarker(latlng);
GEvent.addListener(marker,"click", function() {
marker.openInfoWindowHtml('Petronas Twin Tower');
});
return marker;
}
提前致谢。近 2 周以来,我一直在绞尽脑汁。 :(请帮帮我...
【问题讨论】:
标签: javascript html google-maps