【问题标题】:Issue placing InfoWindows on Google Map markers将 InfoWindows 放置在 Google 地图标记上的问题
【发布时间】:2012-06-08 15:44:45
【问题描述】:

我正在尝试使用 JSON 在 Google 地图上放置多个标记,并将 InfoWindows 绑定到每个标记。不幸的是,只出现了一个标记,即 JSON 中的最后一个值。不会显示其他标记,并且漫画气球样式的箭头不会出现在信息窗口的尾部。已经尝试了很长时间来解决此问题,但似乎没有任何效果。

这是代码:

var stories = {{story_Json|safe}};

var map;


function loadMarkers(stories){
    for (i=0;i<stories.length;i++) {
        var story = stories[i];
        var point = new google.maps.LatLng(story.latitude, story.longitude);
        var marker = new google.maps.Marker({position: point, map: map});
        var infowindow = new google.maps.InfoWindow({
            content: '<div >'+
                '<div >'+
                '</div>'+
                '<h2 class="firstHeading">'+story.headline+'</h2>'+
                '<div>'+
                '<p>'+story.copy+'</p>'+

                '</div>'+
                '</div>'

        });
        google.maps.event.addListener(marker, 'click', function() {
          infowindow.open(map,marker);
        });

    }
}


 function mainMap(position)
 {
       // Define the coordinates as a Google Maps LatLng Object
       var coords = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);

       // Prepare the map options
       var mapOptions =
      {
                  zoom: 15,
                  center: coords,
                  mapTypeControl: false,
                  navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
                  mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        // Create the map, and place it in the map_canvas div
        map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

        // Place the initial marker
        var marker = new google.maps.Marker({
                  position: coords,
                  map: map,
                  title: "Your current location!"
        });

        loadMarkers(stories);

    }

非常感谢您对这些问题的任何见解。

【问题讨论】:

    标签: javascript google-maps google-maps-api-3


    【解决方案1】:

    InfoWindow 使用 i 的最后一个值保持引用(因为循环结束,我们看到 JSON 的最后一个值)。这与 Javascript 中的函数作用域有关,而不是块作用域。

    获得所需结果的一种方法是引入一个新的story 来引用每个信息窗口的每个故事的值。这可以通过一个新的匿名函数作用域来完成:

    function loadMarkers(stories){
        for (i=0;i<stories.length;i++) {
            var story = stories[i];
    
            (function(story) {
              var point = new google.maps.LatLng(story.latitude, story.longitude);
              var marker = new google.maps.Marker({position: point, map: map});
              var infowindow = new google.maps.InfoWindow({
                content: '<div >'+
                    '<div >'+
                    '</div>'+
                    '<h2 class="firstHeading">'+story.headline+'</h2>'+
                    '<div>'+
                    '<p>'+story.copy+'</p>'+
    
                    '</div>'+
                    '</div>'
    
              });
              google.maps.event.addListener(marker, 'click', function() {
                infowindow.open(map,this);
              });
            })(story);
        }
    }
    

    忘记了信息窗口箭头,它是否显示不正确,例如模糊或变形?也许这是一个 CSS 的东西,添加这些行可能会对你有所帮助。

    #map_canvas label { width: auto; display:inline; }
    #map_canvas img { max-width: none; max-height: none; }
    

    【讨论】:

    • 谢谢,海托。这似乎行得通。无法关闭信息窗口,但这是另一个问题。箭头是唯一没有出现的东西,但我会尝试 CSS 解决方案。箭头目前只是丢失了;不知道为什么。
    • 如果您在infowindows的右上角没有看到x,并且您无法关闭它,这可能与没有出现气球箭头有关。您使用的是 Wordpress 还是 CMS?我读到谷歌地图组件在与 CMS 一起使用时会变形,所以我鼓励你尝试 CSS 修复,如果它无效,我建议发布图片。
    • 不,我没有看到 x。我正在使用 Django。也许这是相关的。
    • 刚刚将 CSS sn-ps 添加到 CSS 文件中 - 瞧 - 它正在工作!谢谢,海特!很棒的解决方案!
    猜你喜欢
    • 2015-08-16
    • 2013-04-24
    • 2023-03-15
    • 1970-01-01
    • 2011-01-22
    • 2011-10-09
    • 2015-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多