سؤال

We are experiencing an issue related to Google Maps API V3. The issue is that while we drag the Marker the map also starts dragging.

We are experiencing this issue ONLY on Touch Screens in Windows 8 Environment + Internet Explorer, its fine on NORMAL screens / Mobile Screens - IPaid/ other browsers (Safari and FireFox).

We used below solution, but it throws error (eval javascript error) in Internet Explorer9 and 10:

google.maps.event.addListener(marker, 'dragstart', function(){
    mapObject.setOptions({ draggable: false });
});
google.maps.event.addListener(marker, 'dragend', function(){
    mapObject.setOptions({ draggable: true });
}); 

Sample code is here.

We have also reported this issue here: gmaps-api-issues

EDIT:

We have a posted a related question here also.

هل كانت مفيدة؟

المحلول

Some success At Last (the map still move a bit but can be ignored at the moment)!

Declared two variables:

var isAnyMarkerIsInDraggingState = false;// if a marker is in drag state this value will be TRUE otherwise FALSE
var mapCenterPositionAtTheTimeWhenMarkerWasDragged;// Map Center Position

When Marker is dragged:

   google.maps.event.addListener(objMarker, 'dragstart', function () {
        // Store map center position when a marker is dragged
        mapCenterPositionAtTheTimeWhenMarkerWasDragged = mapObject.getCenter();
        isAnyMarkerIsInDraggingState = true;
    });

When Marker is dropped (drag ends):

google.maps.event.addListener(objMarker, 'dragend', function () {
    // Make Map draggable
    // Set isAnyMarkerIsInDraggingState = false. Because no marker is in drag state
    mapObject.setOptions({ draggable: true });
    isAnyMarkerIsInDraggingState = false;
});

When the Map Drag starts:

google.maps.event.addListener(mapObject, 'dragstart', function () {
    // isAnyMarkerIsInDraggingState = true: means the user is dragging a marker.
    // If the user is dragging the Marker then don't allow the Map to be Dragged
    if (isAnyMarkerIsInDraggingState) {
        mapObject.setOptions({ draggable: false });
    }
});

When Map is in dragging state:

google.maps.event.addListener(mapObject, 'drag', function () {
    // isAnyMarkerIsInDraggingState = true: means the user is dragging a marker.
    // If the user is dragging the Marker then don't allow the Map to be Dragged and set its CenterPosition
    // to mapCenterPositionAtTheTimeWhenMarkerWasDragged

    if (isAnyMarkerIsInDraggingState) {
        mapObject.setCenter(mapCenterPositionAtTheTimeWhenMarkerWasDragged);
    }
});

Complete sample code is here.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top