如果我将一个单击事件连接到我的地图,然后我连接了一个鼠标移动事件,则单击事件将不再起作用。我想没有人知道这件事吧?顺便说一句,这是 3.4 版本中的。
举个简单的例子:
var map;
function initialize() {
var myLatlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
zoom: 8,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var secondClick = false;
var firstClick = false;
var firstClickLatLng;
var secondClickLatLng;
var lines = [];
google.maps.event.addListener(map, 'mousemove', function (event) {
redrawLine(event);
});
google.maps.event.addListener(map, 'click', function (event) {
if (!firstClick && !secondClick) {
firstClick = true;
firstClickLatLng = event.latLng;
}
else if (firstClick && !secondClick) {
secondClick = true;
firstClick = false;
// draw the polyline here
secondClickLatLng = event.latLng;
//google.maps.event.removeListener(listener);
}
else if (!firstClick && secondClick) {
secondClick = false;
// clear the polyline here
alert("what");
//google.maps.event.removeListener(listener);
}
});
function redrawLine(event) {
if (firstClickLatLng != null) {
var lineCoords = [
firstClickLatLng,
event.latLng
];
var line = new google.maps.Polyline({
path: lineCoords,
strokeColor: "#FF0000",
strokeOpacity: 1.0,
strokeWeight: 2
});
// You need to clear the previous line, otherwise
// it draws loads and loads of lines. I did this
// in case it doesn't manage to clear the previous
// one for some reason.
for (var i = 0; i < lines.length; i++) {
lines[i].setMap(null);
}
line.setMap(map);
lines.push(line);
}
}
}
因此,每当您移动鼠标时,就会绘制一条线。问题是,如果我第二次单击,单击事件将不会触发。
Ideas?
EDIT
这个问题是相关的:http://www.mail-archive.com/[电子邮件受保护]/msg15878.html http://www.mail-archive.com/google-maps-js-api-v3@googlegroups.com/msg15878.html
虽然它并没有明确解决我的问题,但其他人已经测试并经历过这一点。