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

前端 未结 4 1394
星月不相逢
星月不相逢 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 21:59

    A marker click event returns a MouseClickEvent

    The only documented property of a MouseClickEvent is:

    latLng LatLng The latitude/longitude that was below the cursor when the event occurred.

    To convert that into a pixel position use the fromLatLngToContainerPixel method of MapCanvasProjection:

        google.maps.event.addListener(marker, 'click', function (e) {
           var point = overlay.getProjection().fromLatLngToDivPixel(e.latLng); 
                info.style.left = point.x + 'px';
                info.style.top = point.y + 'px';
                info.style.display = 'block';
        });
    

    working fiddle

    code snippet:

    var geocoder;
    var map;
    var overlay;
    
    function initialize() {
        var map = new google.maps.Map(
        document.getElementById("map_canvas"), {
            center: new google.maps.LatLng(37.4419, -122.1419),
            zoom: 13,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        });
        var marker = new google.maps.Marker({
            map: map,
            draggable: true,
            position: map.getCenter()
        });
    
        google.maps.event.addListener(map, 'projection_changed', function () {
            overlay = new google.maps.OverlayView();
            overlay.draw = function () {};
            overlay.setMap(map);
    
            var info = document.getElementById('myinfo');
            google.maps.event.addListener(marker, 'click', function (e) {
               var point = overlay.getProjection().fromLatLngToContainerPixel(e.latLng); 
                    info.style.left = (point.x - 100)+ 'px';
                    info.style.top = (point.y) + 'px';
                    info.style.display = 'block';
            });
            google.maps.event.addListener(map, 'center_changed', function (e) {
                var point = overlay.getProjection().fromLatLngToContainerPixel(marker.getPosition()); 
                    info.style.left = (point.x - 100)+ 'px';
                    info.style.top = (point.y) + 'px';
                    info.style.display = 'block';
            });
                    google.maps.event.addListener(marker, 'drag', function (e) {
               var point = overlay.getProjection().fromLatLngToContainerPixel(marker.getPosition()); 
                    info.style.left = (point.x - 100)+ 'px';
                    info.style.top = (point.y) + 'px';
                    info.style.display = 'block';
            });
            
        });
    }
    google.maps.event.addDomListener(window, "load", initialize);
    html,
    body,
    #map_canvas {
      height: 100%;
      width: 100%;
      margin: 0px;
      padding: 0px
    }
    div.info {
      position: absolute;
      z-index: 999;
      width: 200px;
      height: 50px;
      display: none;
      background-color: #fff;
      border: 3px solid #ebebeb;
      padding: 10px;
    }
    
    

    I am a div on top of a google map ..

提交回复
热议问题