Preventing Google Maps Move After Displaying Infowindow

后端 未结 2 1503
别跟我提以往
别跟我提以往 2020-12-05 08:16

I\'m trying to display a infowindow on a Google Maps. It displays perfect, when you hover over a marker it loads up a infowindow but the map jumps to fit in the window. I do

2条回答
  •  天涯浪人
    2020-12-05 08:45

    Suvi Vignarajah's answer is great, what I didn't like about it was how unpredictable the position of the window is near the edges.

    I tweaked it so that the bubble glues to the wall as it should be expected: http://jsfiddle.net/z5NaG/5/

    The only condition is that you need to know the width & height of the infoWindow, you can position it quite nicely. You would work it out through the maps' pixels. If you don't know it you could initialize the InfoWindow offscreen, get it's size and proceed opening it again at the right spot. Ugly but would work.

    First initialize on overlay at time of map initialization by running this function:

        ourOverlay:null,
        createOverlay:function(){
            this.ourOverlay = new google.maps.OverlayView();
            this.ourOverlay.draw = function() {};
            this.ourOverlay.setMap(this.map);
        },
    

    Then at open event adjust the offset like so:

    getInfowindowOffset: function (map, marker) {
        // Settings
        var iwWidth = 240; // InfoWindow width 
        var iwHeight = 190; // InfoWindow Height
        var xOffset = 0;
        var yOffset = 0;
    
        // Our point of interest
        var location = this.ourOverlay.getProjection().fromLatLngToContainerPixel(marker.getPosition()); 
    
        // Get Edges of map in pixels: Sout West corner and North East corner
        var swp = this.ourOverlay.getProjection().fromLatLngToContainerPixel(map.getBounds().getSouthWest()); 
        var nep = this.ourOverlay.getProjection().fromLatLngToContainerPixel(map.getBounds().getNorthEast()); 
    
        // Horizontal Adjustment
        if(location.xnep.x-iwWidth/2){
            xOffset = (nep.x-iwWidth/2)-location.x ;
        }
    
        // Vertical Adjustment
        if(location.y

    And the result is pretty nice, but this triangle seems out of place now.

    You can use InfoBox to remove the pointy arrow on bottom:

                GmapsManager.infowindow = new InfoBox({
                content:'',
                alignBottom: true,
                closeBoxURL: "",
            });
    

    and style the bubble with the following code:

    .infobox-popup{
        background: #fff;
        -webkit-border-radius: 3px;
        border-radius: 3px;
        padding: 5px;
        margin-left: -100px;
        margin-bottom: 30px;
        width: 200px;
        -webkit-box-shadow: 0 0 1px 0 #595959;
        box-shadow: 0 0 1px 0 #595959;
    }
    

提交回复
热议问题