var map = new google.maps.Map(document.getElementById('map'),{
\t zoom: 15,
\t center: {lat:44.6478627,lng:-63.6116746}
});
var m1 = new google.maps.Marker({
\t map: map,
\t position: {lat:44.6566246,lng:-63.6202576}
});
var m2 = new google.maps.Marker({
\t map: map,
\t position: {lat:44.6566475,lng:-63.6197212}
});
var m3 = new google.maps.Marker({
\t draggable:true,
\t map: map,
\t 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 = {
\t fromLatLngToPoint: function(latLng) {
\t \t var topRight = map.getProjection().fromLatLngToPoint(map.getBounds().getNorthEast());
\t \t var bottomLeft = map.getProjection().fromLatLngToPoint(map.getBounds().getSouthWest());
\t \t var scale = Math.pow(2, map.getZoom());
\t \t var worldPoint = map.getProjection().fromLatLngToPoint(latLng);
\t \t return new google.maps.Point((worldPoint.x - bottomLeft.x) * scale, (worldPoint.y - topRight.y) * scale);
\t },
\t onStartDragging: function() {
\t \t isDragging = true;
\t },
\t
\t onStopDragging: function() {
\t \t isDragging = false;
\t },
\t onMarkerMouseOver: function(marker) {
\t \t //set the hoveredMarker flag to the marker that is being hovered
\t \t hoveredMarker = marker;
\t \t
\t \t if(isDragging) {
\t \t \t //do something here when you mouse over `marker`, only if you are also dragging
\t \t \t console.log('mouse over while dragging');
\t \t }
\t \t
\t \t //do something here when you mouse over `marker`, regardless of if you are dragging or not
\t \t //console.log('mouse over');
\t },
\t
\t onMarkerMouseOut: function(marker) {
\t \t //set the hoveredMarker flag to null
\t \t hoveredMarker = null;
\t \t
\t \t if(isDragging) {
\t \t \t //do something here when you mouse out `marker`, only if you are also dragging
\t \t \t console.log('mouse out while dragging');
\t \t }
\t \t
\t \t //do something here when you mouse out `marker`, regardless of if you are dragging or not
\t \t //console.log('mouse out');
\t },
\t
\t arePointsClose: function(latLng1,latLng2) { \t
\t \t var
\t \t \t p1 = this.fromLatLngToPoint(latLng1),
\t \t \t p2 = this.fromLatLngToPoint(latLng2);
\t \t \t
\t \t //if the points are within `tolerance` return true, otherwise, return false
\t \t return Math.abs(p2.x - p1.x) <= tolerance && Math.abs(p2.y - p1.y) <= tolerance;
\t
\t },
\t
\t onDrag: function(e) {
\t \t //it's very unlikely that you're coordinates of \t m3 will ever be 100% identical to m1 or m2 when dragging,
\t \t //so use the maps projection to convert to latLngs to a point and allow a tolerance (see methods.arePointsClose())
\t \t if(methods.arePointsClose(e.latLng,m1.getPosition())) {
\t \t \t methods.onMarkerMouseOver(m1);
\t \t } else if(hoveredMarker === m1) {
\t \t \t methods.onMarkerMouseOut(m1);
\t \t }
\t \t
\t \t
\t \t if(methods.arePointsClose(e.latLng,m2.getPosition())) {
\t \t \t methods.onMarkerMouseOver(m2);
\t \t } else if(hoveredMarker === m2) {
\t \t \t methods.onMarkerMouseOut(m2);
\t \t }
\t
\t }
};
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>
你怎麼可以拖動標記,鼠標懸停(鼠標總是** **過你正在拖動標記)另一個標記與此同時?如果你解釋你要完成的是什麼,而不是你認爲你需要做什麼才能完成某件事,那麼可能會更容易。 – Adam 2015-03-31 17:14:26
我編輯了我最初的問題,並添加了一個我嘗試做的例子。日Thnx。 – user1176476 2015-04-01 18:00:15