多个鼠标事件未触发

时间:2013-01-16 22:12:51

标签: google-maps events mouse

我正在使用google maps api。我想准备两个鼠标事件一次触发。下面是代码片段。

function initialize() {
var myLatlng = new google.maps.LatLng(37.4419, -122.1419);
var myOptions = {
  zoom: 13,
  center: myLatlng,
  mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    var polyOptions = {
        strokeColor: '#000000',
        strokeOpacity: 1.0,
        strokeWeight: 3  }
    poly = new google.maps.Polyline(polyOptions);
    poly.setMap(map);
    // Add a listener for the click event
    google.maps.event.addListener(map, 'click', addLatLng);
}

function addLatLng(event) {
var path = poly.getPath();

if(!draw)
{
    path.push(event.latLng);
    path.push(event.latLng);
    draw = true;
// Add a new marker at the new plotted point on the polyline.

    var marker = new google.maps.Marker({
            position: event.latLng,
            title: '#' + path.getLength(),
            map: map  });
    google.maps.event.addListener(map, 'mousemove', moveTrackLine);
}
else
{
        path.push(event.latLng);
        var marker = new google.maps.Marker({
                                                    position: event.latLng,
                                                    title: '#' + path.getLength(),
                                                    map: map  });
        draw = false;
}
}

function moveTrackLine(event) {

var path = poly.getPath();
// replace the old point with a new one to update the track
path.pop();
path.push(event.latLng);
}

当我第一次看到地图上放置的标记时点击地图。然后当鼠标移动时,我看到折线更新并正确地跟随我的光标。接下来,如果我点击地图,我看不到地图上放置的标记,也没有进入addLatLng函数。在此先感谢您的帮助和时间。 -Tim

1 个答案:

答案 0 :(得分:0)

将折线的clickable - 选项设置为false。

问题:只要将mousemove-event应用于地图,您将无法再点击地图,因为鼠标光标下面始终是折线。

当您将折线的可点击选项设置为false时,折线不响应鼠标事件,并且点击事件将传递给地图。

相关问题