Get Position of Mouse Cursor on Mouseover of Google Maps V3 API Marker

后端 未结 6 700
名媛妹妹
名媛妹妹 2020-12-01 16:39

I am trying to make a div visible at the position of the cursor when the cursor mouseover a marker using jQuery. Its kind of like a tooltip. However I cannot se

6条回答
  •  无人及你
    2020-12-01 17:10

    This gives the mouse position, works for me.

    function CanvasProjectionOverlay() { }
    
    CanvasProjectionOverlay.prototype = new google.maps.OverlayView();
    CanvasProjectionOverlay.prototype.constructor = CanvasProjectionOverlay;
    CanvasProjectionOverlay.prototype.onAdd = function () { };
    CanvasProjectionOverlay.prototype.draw = function () { };
    CanvasProjectionOverlay.prototype.onRemove = function () { };
    

    In your initialize function;

    canvasProjectionOverlay = new CanvasProjectionOverlay();
    canvasProjectionOverlay.setMap(map);
    

    and use it like this;

    google.maps.event.addListener(marker, 'mouseover', function(event) { 
       var divPixel = canvasProjectionOverlay.getProjection().fromLatLngToContainerPixel(event.latLng);
       x = divPixel.x;
       y = divPixel.y;
    }); 
    

提交回复
热议问题