Place infoWindow on a static position away from th

2019-06-27 09:49发布

问题:

What I want to acchieve (and couldn't find anything so far) is that the infoWindow is not attached to the marker. I'd like to place it on a static position in my viewport and just exchange the content based on what marker is clicked.

Q: How can I place (and not just offset) a google maps marker infowindow on a fixed location.

回答1:

Answering my own Q - in case someone needs this:

// Add somewhere before creating your map to hide the info window as long as it got no content:
<script type="text/javascript">
jQuery( '#info' ).hide();
</script>

<script type="text/javascript">
function createMarker( lat, lng, whatever ) {
    // map_object_name = the name of your map when you init()
    html = "<strong>" + whatever + "</strong>";

    var marker = new google.maps.Marker( {
        map: map_object_name,
        position: new google.maps.LatLng( lat, lng )
    } );

    // This snippet adds the content on the fly (when you click the marker)
    google.maps.event.addListener( marker, 'click', function() {
        // This pans the viewport/centers the marker in your viewport
        map_object_name.panTo( new google.maps.LatLng( lat, lng ) );

        // This shows the html-element with the id "info" and adds the html content
        jQuery( '#info' ).show();
        // This clears the content before you add new one
        jQuery( '#info' ).empty();
        jQuery( '#info' ).append( html );

        // Commented out - just as reference
        // infoWindow.setContent( html ); // the original infoWindow as you know it from Google Maps
        // infoWindow.open( map_object_name, marker ); // the same, just the callback
    } );
}
</script>

The "placing"/positioning of the #info element can than be done with normal html & css.