Is there a way to Fix a Google Maps Marker to the Center of its Map always?

前端 未结 7 1383
野趣味
野趣味 2020-12-01 00:21

To be more precise, what I try to accomplish is that while I Drag a Google Map there\'s a Google Maps Marker that stays fixed

7条回答
  •  广开言路
    2020-12-01 00:47

    Rather than injecting an HTML component as suggested in @Dr.Molle's answer, why don't you use a CSS only solution.

    It is simpler, more efficient and doesn't require you to even touch the DOM (so there won't be any problems if Google changes their implementation).

    Also since Google Maps doesn't apply any styles on the container element (#map) the solution is pretty safe.

    #map {
        position: relative;
    }
    
    #map:after {
        width: 22px;
        height: 40px;
        display: block;
        content: ' ';
        position: absolute;
        top: 50%; left: 50%;
        margin: -40px 0 0 -11px;
        background: url('https://maps.gstatic.com/mapfiles/api-3/images/spotlight-poi_hdpi.png');
        background-size: 22px 40px; /* Since I used the HiDPI marker version this compensates for the 2x size */
        pointer-events: none; /* This disables clicks on the marker. Not fully supported by all major browsers, though */
    }
    

    Here is a jsFiddle.

提交回复
热议问题