我正在尝试模仿谷歌地图的谷歌地图中的绘图路径的桌面版本。我所做的是当我尝试绘制路径时,我已禁用默认地图的可拖动事件并附加了一个mousedown事件。在mousedown事件之后,mousemove事件被触发并相应地工作。当鼠标释放时,事件被捕获在mouseup处理程序中,其中mousemove处理程序在mouseup事件侦听器中被删除。但是,在第二次mousedown和mouseup事件之后,这无法正常工作。
我的代码是:
var map = 'Already created map object';
var polyOptions = {
strokeColor: '#000000',
strokeOpacity: 1.0,
strokeWeight: 2,
map: map,
idx: 0
};
var mouseMoveHandler = null;
google.maps.event.addListener(map, 'mousedown', function(e) {
mouseMoveHandler = google.maps.event.addListener(map, 'mousemove', function(e) {
// Create a new polyline instance if it does not exists
if ("undefined" == typeof(GMap._poly[GMap._active_overlay])) {
GMap._poly[GMap._active_overlay] = new google.maps.Polyline(polyOptions);
}
var path = GMap._poly[GMap._active_overlay].getPath();
path.push(e.latLng);
}); // End of mousemove lister
return false;
});
google.maps.event.addListener(map, 'mouseup', function(e) {
google.maps.event.removeListener(mouseMoveHandler);
});
答案 0 :(得分:3)
将此添加到polyOptions:
clickable:false
没有这个,多边形将监听鼠标事件,而mouseup-event将在多边形上触发,而不是在地图上触发。
答案 1 :(得分:0)
带有bootstrap工具提示的示例:
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;
}