将InfoWindows置于Google地图标记上的问题

时间:2012-06-08 15:44:46

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

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

这是代码:

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);

    }

对这些问题的任何见解都非常感激。

1 个答案:

答案 0 :(得分: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);
    }
}

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

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