【问题标题】:Google maps-api v3 InfoWindow automatically open on page loadGoogle maps-api v3 InfoWindow 在页面加载时自动打开
【发布时间】:2012-04-26 23:51:27
【问题描述】:

我正在做一个使用 Google Maps api-v3 的项目, 在地图上会有一些地点标记,其中包含我存储在 InfoWindow 中的信息。

我想知道您是否可以将 InfoWindow 设置为在页面加载时自动打开(即在没有用户交互的情况下自动打开)。

在网上搜索,我似乎发现它需要绑定到一个事件侦听器,但 InfoWindow 对象似乎拥有的所有事件都是鼠标事件。

有人知道某种解决方法吗?

【问题讨论】:

    标签: google-maps-api-3


    【解决方案1】:

    不确定我是否完全理解您的问题,但这对我来说适用于硬编码的 LatLng:

    var infoWindow = null;
    function initialize() 
    {
        infoWindow = new google.maps.InfoWindow();
        var windowLatLng = new google.maps.LatLng(43.25,-68.03);
        infoWindow.setOptions({
            content: "<div>This is the html content.</div>",
            position: windowLatLng,
        });
        infoWindow.open(map); 
    } // end initialize
    
    google.setOnLoadCallback(initialize);
    

    【讨论】:

    • 非常感谢,这正是我想要的!
    • 信息窗口正好落在我的标记上。有没有办法计算经度差以便将信息窗口放在标记上方?
    【解决方案2】:

    function initMap() {
        var cairo = {lat:18.519331, lng: 73.849421};
        var map = new google.maps.Map(document.getElementById('map'), {
            scaleControl: true,
            center: cairo,
            zoom: 15,
        });
    
        var infowindow = new google.maps.InfoWindow;
        infowindow.setContent('<b>adddress</b>');
    
        var marker = new google.maps.Marker({map: map, position: cairo});
        infowindow.open(map, marker);
        infoWindow.open(map); 
    }
    <script async defer 
        src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCrCByzXY6y5KMSOi9AuqWVf4VYZPyJ5SE&language=hi&region=EG&callback=initMap">
    </script>
    <div id="map"></div>

    【讨论】:

    • 最好能解释一下为什么你的代码可以工作。
    【解决方案3】:

    这是我的代码,它使用地图和鼠标悬停加载信息窗口。

    map.setCenter(results[0].geometry.location);
    var marker = new google.maps.Marker({
        map: map,
        icon: "/images/map-marker.png",
        draggable: true,
        position: results[0].geometry.location
    
    });
    var infowindow = new google.maps.InfoWindow({
        content: "Use pin to point your exact locality"
    });
    google.maps.event.addListener(marker, 'mouseover', function () {
        infowindow.open(map, marker);
    });
    infowindow.open(map, marker);
    

    【讨论】:

      【解决方案4】:

      这个问题我迟到了,但我想分享一些东西。我也搜索了它的解决方案,甚至没有从上述解决方案中得到工作。然后我尝试了自己并得到了自己的解决方案(我不是专家,我的想法可能不好但对我来说很好)。 我们一般都喜欢

      var infowindow = new google.maps.InfoWindow();
      google.maps.event.addListener(marker, 'click', (function(marker) {
      return function() {
          infowindow.setContent('some content here');
            infowindow.open(map, marker);
          }
      })(marker));
      

      将以上行替换为:

      var infowindow = new google.maps.InfoWindow();
      infowindow.setContent('some content here');
      infowindow.open(map, marker);
      

      不要等待标记的点击事件,只需设置内容并打开它(您必须定义地图和标记以及信息窗口指向您的标记)。

      【讨论】:

      • 最简单的解决方案,将其称为外部标记单击.. 谢谢 Roka
      【解决方案5】:

      只需获取 infowindow.open(map,marker);离开 google.maps.event.addListener。它应该是这样的:

            var marker = new google.maps.Marker({
            position: myLatlng1,
            map: map,
            title: 'Uluru (Ayers Rock)'
      
        });
      
        infowindow.open(map,marker);
      
        google.maps.event.addListener(marker, 'click', function() {
        });   
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-24
        • 1970-01-01
        • 2012-09-06
        • 2013-03-31
        • 2011-12-09
        • 2012-01-06
        • 2020-06-11
        相关资源
        最近更新 更多