Google Maps API v3 - Mousemove和Click事件组合

时间:2011-02-23 11:19:08

标签: google-maps-api-3

如果我有一个连接到我的地图的点击事件,然后我挂了一个mousemove事件,则点击事件不再有效。我不认为有人对此有所了解吗?顺便提一下,这是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);
        }
    }
}

因此,无论何时移动鼠标,都会绘制一条线。问题是,如果我第二次点击,则不会触发点击事件。

想法?

修改

此问题与您有关:http://www.mail-archive.com/google-maps-js-api-v3@googlegroups.com/msg15878.html

虽然它没有明确地解决我的问题,但其他人已经测试并经历过这个。

2 个答案:

答案 0 :(得分:3)

排序,如该链接所示,您需要声明clickable:false

http://code.google.com/apis/maps/documentation/javascript/reference.html#PolylineOptions

答案 1 :(得分:0)

    google.maps.event.addListener(poly,"mousemove",function(e){

        var _tooltipPolys = $("#tooltipPolys");
        if(_tooltipPolys.length == 0) {
            _tooltipPolys = $(' \
                <div id="tooltipPolys" class="tooltip top" role="tooltip"> \
                    <div class="tooltip-arrow"></div> \
                    <div class="tooltip-inner"></div> \
                </div> \
            ');
            $("body").append(_tooltipPolys);
            $("div.tooltip-inner", _tooltipPolys).text(this.title);
            _tooltipPolys.css({
                "opacity": ".9",
                "position":"absolute"
            });
        }

        var pageX = event.pageX;
        var pageY = event.pageY;
        if (pageX === undefined) {
            pageX = event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
            pageY = event.clientY + document.body.scrollTop + document.documentElement.scrollTop;
        }