将infoWindow放置在远离标记的静态位置(Google地图) [英] Place infoWindow on a static position away from the marker (Google Maps)

查看:170
本文介绍了将infoWindow放置在远离标记的静态位置(Google地图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现的目标(迄今为止找不到任何东西)是infoWindow没有附加到标记。我想把它放在我的视口中的一个静态位置,并根据点击的标记交换内容。

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.

问:如何放置(而不是只要抵消)一个谷歌地图标记infowindow在一个固定的位置。

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

推荐答案

回答我自己的Q - 万一有人需要这个:

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>

#info 元素可以用普通的html& css。

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

这篇关于将infoWindow放置在远离标记的静态位置(Google地图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆