Google Maps: Get click or marker (x,y) pixel coordinates inside marker click listener

前端 未结 4 1356
星月不相逢
星月不相逢 2021-01-18 21:28

I am trying to display completely custom info windows over map markers on marker click. I have successfully implemented this answe

4条回答
  •  终归单人心
    2021-01-18 22:18

    Here is how you get the pixel location:

    function getPixelLocation(currentLatLng) {
    
            var scale = Math.pow(2, map.getZoom());
            // The NorthWest corner of the current viewport corresponds
            // to the upper left corner of the map.
            // The script translates the coordinates of the map's center point
            // to screen coordinates. Then it subtracts the coordinates of the
            // coordinates of the map's upper left corner to translate the 
            // currentLatLng location into pixel values in the 
    element that hosts the map. var nw = new google.maps.LatLng( map.getBounds().getNorthEast().lat(), map.getBounds().getSouthWest().lng() ); // Convert the nw location from geo-coordinates to screen coordinates var worldCoordinateNW = map.getProjection().fromLatLngToPoint(nw); // Convert the location that was clicked to screen coordinates also var worldCoordinate = map.getProjection().fromLatLngToPoint(currentLatLng); var currentLocation = new google.maps.Point( Math.floor((worldCoordinate.x - worldCoordinateNW.x) * scale), Math.floor((worldCoordinate.y - worldCoordinateNW.y) * scale) ); console.log(currentLocation); return currentLocation; }

提交回复
热议问题