拖动标记Google Maps API时不会触发鼠标悬停

时间:2015-03-31 17:11:47

标签: javascript google-maps events google-maps-api-3 draggable

在我的网页中,我正在尝试使用谷歌地图,我可以在其中拖放标记。问题是,当我拖动一个特殊标记时,不会触发鼠标悬停和其他标记的鼠标移除事件。

嗯,我知道Google地图使用以下图层: floatPane,overlayMouseTarget,floatShadow,overlayImage,overlayShadow,overlayLayer,mapPane,

并且标记属于overlayImage图层。 此外,未触发鼠标过度和事件事件的原因是因为 可拖动的标记"吸收"从未到达标记背后的事件。

示例:我有标记m1,m2和m3。 Marker m3是可拖动的。我想拖动m3,当它通过m1和m2顶部时,鼠标悬停在m1和m2的事件上。

问题1:可拖动对象属于哪一层? (我假设overlayMouseTarget)。

问题2:我可以更改标记所属的图层吗?这样我就可以修改它的z-index。

感谢您的时间。

1 个答案:

答案 0 :(得分:1)

这是一个很好的例子,你问的是如何做一些你不需要做的事情。您根本不想使用图层,您希望监视drag事件并检查正在拖动的标记与其他标记的接近程度。

您实际上并不想要trigger the events,但是您想要调用附加到这些事件的函数。

警告 - 前面有很多代码

var map = new google.maps.Map(document.getElementById('map'),{
	zoom: 15,
	center: {lat:44.6478627,lng:-63.6116746}
});
var m1 = new google.maps.Marker({
	map: map,
	position: {lat:44.6566246,lng:-63.6202576}
});

var m2 = new google.maps.Marker({
	map: map,
	position: {lat:44.6566475,lng:-63.6197212}
});

var m3 = new google.maps.Marker({
	draggable:true,
	map: map,
	position: {lat:44.6544724,lng:-63.6296561}
});

//some useful flags
var isDragging = false;
var hoveredMarker = null;

//a tolerance amount - how close the dragging marker has to be to another marker in order for you to consider it to be a mouseover
//you may need to play with this number to achieve the effect you want
var tolerance = 30;


var methods = {

	fromLatLngToPoint: function(latLng) {
		var topRight = map.getProjection().fromLatLngToPoint(map.getBounds().getNorthEast());
		var bottomLeft = map.getProjection().fromLatLngToPoint(map.getBounds().getSouthWest());
		var scale = Math.pow(2, map.getZoom());
		var worldPoint = map.getProjection().fromLatLngToPoint(latLng);
		return new google.maps.Point((worldPoint.x - bottomLeft.x) * scale, (worldPoint.y - topRight.y) * scale);
	},

	onStartDragging: function() {
		isDragging = true;
	},
	
	onStopDragging: function() {
		isDragging = false;
	},

	onMarkerMouseOver: function(marker) {
		//set the hoveredMarker flag to the marker that is being hovered
		hoveredMarker = marker;
		
		if(isDragging) {
			//do something here when you mouse over `marker`, only if you are also dragging
			console.log('mouse over while dragging');
		}
		
		//do something here when you mouse over `marker`, regardless of if you are dragging or not
		//console.log('mouse over');
	},
	
	onMarkerMouseOut: function(marker) {
		//set the hoveredMarker flag to null
		hoveredMarker = null;
		
		if(isDragging) {
			//do something here when you mouse out `marker`, only if you are also dragging
			console.log('mouse out while dragging');
		}
		
		//do something here when you mouse out `marker`, regardless of if you are dragging or not
		//console.log('mouse out');
	},
	
	arePointsClose: function(latLng1,latLng2) {	
		var 
			p1 = this.fromLatLngToPoint(latLng1),
			p2 = this.fromLatLngToPoint(latLng2);
			
		//if the points are within `tolerance` return true, otherwise, return false
		return Math.abs( p2.x - p1.x ) <= tolerance && Math.abs( p2.y - p1.y ) <= tolerance;
	
	},
	
	onDrag: function(e) {

		//it's very unlikely that you're coordinates of 	m3 will ever be 100% identical to m1 or m2 when dragging,
		//so use the maps projection to convert to latLngs to a point and allow a tolerance ( see methods.arePointsClose() )
		if( methods.arePointsClose(e.latLng,m1.getPosition()) ) {
			methods.onMarkerMouseOver(m1);
		} else if( hoveredMarker === m1 ) {
			methods.onMarkerMouseOut(m1);
		}
		
		
		if( methods.arePointsClose(e.latLng,m2.getPosition()) ) {
			methods.onMarkerMouseOver(m2);
		} else if( hoveredMarker === m2 ) {
			methods.onMarkerMouseOut(m2);
		}
	
	}

};

google.maps.event.addListener(m1,'mouseover',function() { methods.onMarkerMouseOver(m1); });
google.maps.event.addListener(m2,'mouseover',function() { methods.onMarkerMouseOver(m2); });
google.maps.event.addListener(m1,'mouseout',function() { methods.onMarkerMouseOut(m1); });
google.maps.event.addListener(m2,'mouseout',function() { methods.onMarkerMouseOut(m2); });

google.maps.event.addListener(m3,'dragstart',function() { methods.onStartDragging(); });
google.maps.event.addListener(m3,'dragend',function() { methods.onStopDragging(); });

google.maps.event.addListener(m3,'drag',function(event) { methods.onDrag(event); });
html,body { height: 100%; }
#map { height: 100%; }
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?libraries=drawing,geometry"></script>
<div id="map"></div>