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

后端 未结 6 558
名媛妹妹
名媛妹妹 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 16:56

    This is an extension of my previous answer regarding the computation of the pixel positions (Google maps API v3). Introduce a "global" variable overlay:

    var overlay = new google.maps.OverlayView();
    overlay.draw = function() {};
    overlay.setMap(map); // 'map' is new google.maps.Map(...)
    

    Use overlay in the listener to get the projection and the pixel coordinates:

    google.maps.event.addListener(marker, 'mouseover', function() {
        var projection = overlay.getProjection(); 
        var pixel = projection.fromLatLngToContainerPixel(marker.getPosition());
        // use pixel.x, pixel.y ... (after some rounding)
    }); 
    

    You may also have a look at projection.fromLatLngToDivPixel().

    0 讨论(0)
  • 2020-12-01 17:02

    The solution that works for me is more straight forward:

    map.data.addListener('mouseover', function (event) {
        posX = event.ub.clientX;
        posY = event.ub.clientY;
    });
    
    0 讨论(0)
  • 2020-12-01 17:05
    mapObject.marker.addListener("mouseover", (event) => {
      //USE THE COORDINATES HERE = { lat: event.latLng.lat(), lng: event.latLng.lng();
    }
    
    0 讨论(0)
  • 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;
    }); 
    
    0 讨论(0)
  • 2020-12-01 17:11

    Add the following code into the listener function:

    // to display a tooltip:
    marker.setTitle("Hi");
    
    // to get the geographical position:
    var pos = marker.getPosition();
    var lat = pos.lat();
    var lng = pos.lng();
    // ...
    
    0 讨论(0)
  • 2020-12-01 17:15

    Here's a solution that uses the MouseEvent of the click event but does not rely on accessing that via an undocumented property that can change at any time like 'ub' or 'wb' (I think it's 'ya' currently).

    map.data.addListener('mouseover', function (e) {
        var keys = Object.keys(e);
        var x, y;
        for (var i = 0; i < keys.length; i++) {
            if (MouseEvent.prototype.isPrototypeOf(e[keys[i]])) {
                x = e[keys[i]].clientX;
                y = e[keys[i]].clientY;
            }
        }
    });
    
    0 讨论(0)
提交回复
热议问题