自动打开谷歌地图信息窗口

Automatically opening Google maps infowindow

本文关键字:信息 信息窗 窗口 谷歌地图      更新时间:2023-09-26

我有一张带有信息窗口的地图。我需要在页面加载时打开此信息窗口。API 中有一个InfoWindow.open方法,但它无法正常工作。

这是我当前的代码

<html>
<head>
<link rel="stylesheet" href="/static/css/map.css" />
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
    <script>
        var infoWindow = null;
        var map = null;
        var marker;
        function getInfoWindowEvent(marker, content) {
            infoWindow.close()
            infoWindow.setContent(content);
            infoWindow.open(map, marker);
        }
        function initialiseMap() {
            infoWindow = new google.maps.InfoWindow();
            var myLatlng = new google.maps.LatLng({MAP_6_LATITUDE}, {MAP_6_LONGTITUDE});
            var myOptions = {
                  zoom: 12,
                  streetViewControl: false,
                  center: myLatlng,
                  mapTypeId: google.maps.MapTypeId.ROADMAP
            }
            map = new google.maps.Map(document.getElementById("map_side_canvas"), myOptions);
            var bounds = new google.maps.LatLngBounds();
            var image = new google.maps.MarkerImage(
                '/static/img/markers/image.png',
                new google.maps.Size(32,37),
                new google.maps.Point(0,0),
                new google.maps.Point(16,37)
            );
            var shadow = new google.maps.MarkerImage(
                '/static/img/markers/shadow.png',
                new google.maps.Size(54,37),
                new google.maps.Point(0,0),
                new google.maps.Point(16,37)
            );
            var shape = {
                coord: [29,0,30,1,31,2,31,3,31,4,31,5,31,6,31,7,31,8,31,9,31,10,31,11,31,12,31,13,31,14,31,15,31,16,31,17,31,18,31,19,31,20,31,21,31,22,31,23,31,24,31,25,31,26,31,27,31,28,31,29,30,30,29,31,23,32,22,33,21,34,20,35,19,36,12,36,11,35,10,34,9,33,8,32,2,31,1,30,0,29,0,28,0,27,0,26,0,25,0,24,0,23,0,22,0,21,0,20,0,19,0,18,0,17,0,16,0,15,0,14,0,13,0,12,0,11,0,10,0,9,0,8,0,7,0,6,0,5,0,4,0,3,0,2,1,1,2,0,29,0],
                type: 'poly'
            };
            var myLatlng = new google.maps.LatLng({MAP_6_LATITUDE}, {MAP_6_LONGTITUDE});
            marker = new google.maps.Marker({
                  position: myLatlng, 
                  map: map, 
                  icon: image,
                  shadow: shadow,
                  shape: shape,
                  title:"{TITLE}"
            });
            google.maps.event.addListener(marker, 'click', function() {
            getInfoWindowEvent(marker, "<a href='{LINK}'><img src='/system/scripts/i.php?f={MAIN_PHOTO_21}&w=90&h=70&crop=1' class='hotel-img'></a><div class='rating' data-rate_level='{STARS_52}'></div><br /><a href='{LINK}'><h3>{TITLE}</h3></a><div class='price'>From <div class='text'><?=number_format($minprice[0][0],0,',',' '); ?> USD</div></div>");
            });
    infoWindow.open(map);
    return map;
        }
    </script>
</head>
<body onLoad="initialiseMap(); "  id="map_side_canvas" style="margin:0;padding:0;width:100%;height:100%;">
</body>

function initialiseMap() {
  function getInfoWindowEvent(marker, content) {

呃....为什么你有两个不合时宜的功能?


我看到在第一个函数中你调用 infoWindow.close()。这会从 DOM 中删除信息窗口。然后,您必须使用new google.maps.infowindow(<params>);重新创建它