谷歌地图API-带有位置详细信息的信息窗口自动打开,不包含内容

Google Maps API - InfoWindow with Place Details auto-opens without content

本文关键字:包含内 窗口 信息窗 API- 位置 详细信息 信息 谷歌地图      更新时间:2023-09-26

我是新来的,我是谷歌地图API或任何API的新手!我已经设法拼凑出了一些东西,但请耐心等待

我已经加载了我的基本地图,在我正在处理的业务(地方)上放置了一个标记,并在信息窗口中填充从Places API获取的相关业务详细信息。单击标记时,将显示信息窗口,并显示内容。到目前为止,很好

现在,我希望信息窗口在加载地图时自动显示。我有这个工作,但出现的信息窗口是空的。。。只出现一个带有X的小气泡。如果我单击该标记,则会显示包含业务详细信息的信息窗口

我的代码是从我在这里和那里找到的片段中拼凑而成的,所以我知道它一团糟,其中的某些东西没有触发我想要的信息窗口。有人能认出我哪里错了吗?

提前感谢

C


作为参考,当我点击标记时,我会得到(我已经清空了详细信息)

然而,当我把它运行到自动打开时,我会得到这个


    <script>      
  function initMap() {
    var map = new google.maps.Map(document.getElementById('map'), {
      center: {lat: 54.71891, lng: -8.716691},
      zoom: 16,
      disableDefaultUI: true,
      keyboardShortcuts: false,
      disableDoubleClickZoom: true,
      draggable: false
    });
    var infowindow = new google.maps.InfoWindow();
    var service = new google.maps.places.PlacesService(map);
    service.getDetails({
      placeId: 'ChIJBy4MUDc8X0gR7LGOUiAYQeQ'
    }, function(place, status) {
      if (status === google.maps.places.PlacesServiceStatus.OK) {
        var marker = new google.maps.Marker({
          map: map,
          position: place.geometry.location
        });
        google.maps.event.addListener(marker, 'click', function() {
          infowindow.open(map, marker);
          infowindow.setContent('<div><strong>' + place.name + '</strong><br>' +
            '<p>' + place.formatted_address + '</p>' +
            '<p><a href="https://www.google.com/maps?cid=16447453841236865516"><strong>Get Directions / Larger Map</strong> </a></p>' +
            '</div>');
        });
    google.maps.event.addListenerOnce(map, 'tilesloaded', function() {
      infowindow.open(map, marker);
    });
      }
    });
  }
</script>

在代码中,您可以通过单击标记来设置infowindow的内容。所以如果你在addListener之前设置内容,我认为这是可以的

<script>      
function initMap() {
    var map = new google.maps.Map(document.getElementById('map'), {
        center: {lat: 54.71891, lng: -8.716691},
        zoom: 16,
        disableDefaultUI: true,
        keyboardShortcuts: false,
        disableDoubleClickZoom: true,
        draggable: false
    });
    var service = new google.maps.places.PlacesService(map);
    service.getDetails({
        placeId: 'ChIJBy4MUDc8X0gR7LGOUiAYQeQ'
    }, function(place, status) {
        if (status === google.maps.places.PlacesServiceStatus.OK) {
            var marker = new google.maps.Marker({
                map: map,
                position: place.geometry.location
            });
            var infowindow = new google.maps.InfoWindow();
            infowindow.setContent('<div><strong>' + place.name + '</strong><br>' +
                '<p>' + place.formatted_address + '</p>' +
                '<p><a href="https://www.google.com/maps?cid=16447453841236865516"><strong>Get Directions / Larger Map</strong> </a></p>' +
                '</div>');
            google.maps.event.addListener(marker, 'click', function() {
                infowindow.open(map, marker);
            });
            google.maps.event.addListenerOnce(map, 'tilesloaded', function() {
                infowindow.open(map, marker);
            });
        }
    });
}
</script>

我希望这正是你所需要的。