2011-02-23 98 views
4

如果我有一個點擊事件掛鉤到我的地圖,然後我鉤了一個mousemove事件,click事件不再起作用。我不認爲有人知道這件事嗎?順便說一句,這是3.4版本。谷歌地圖API v3 - 鼠標移動和點擊事件組合

舉個簡單的例子:

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/[email protected]/msg15878.html

它不明確,雖然解決我的問題,但其他人測試,並經歷過這個。

+0

另外,您需要添加一個條件爲段留後第二次點擊: 如果(firstClickLatLng != null && secondClickLatLng == null){ – Rsmusic 2014-05-30 21:19:18

+0

我知道我遲到了,但我對這個線程感興趣,爲什麼不在每個段的onmousemove監聽器之外設置映射? – Rsmusic 2014-05-30 21:37:53

回答

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; 
     }