Google地图v3 Infowindow无效

时间:2011-12-29 15:07:06

标签: javascript google-maps-api-3

我有以下JS代码来制作Google地图标记,我也尝试在每个标记上设置infowindow但是通过我的代码infowindow不在这里打开是我的代码

for(i=0; i<GPS.length; i++)
    {

          var image = 'ico/no.png';
          var infowindow = new google.maps.InfoWindow();
          var ContentString = GPS[i].TITLE
          markers[i] = new google.maps.Marker(
          { 
           position: GPS[i].GPS,
           map: map,
           draggable:true,
           icon:image,
           title:GPS[i].TITLE
           });                       

            google.maps.event.addListener(markers[i], 'click', function() {
            infowindow.setContent(ContentString);
            infowindow.open(map,markers[i]);
            });


    }    

2 个答案:

答案 0 :(得分:1)

如果我没弄错的话,看起来你在这里有封闭问题。

查看链接,了解您如何解决closure issue in google maps

谷歌的这个简单代码将在地图上显示marker

var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
var myOptions = {
  zoom: 4,
  center: myLatlng,
  mapTypeId: google.maps.MapTypeId.ROADMAP
}

var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

var contentString = '<div id="content">'+
    '<div id="siteNotice">'+
    '</div>'+
    '<h2 id="firstHeading" class="firstHeading">Uluru</h2>'+
    '<div id="bodyContent">'+
    '<p><b>Uluru</b>, also referred to as <b>Ayers Rock</b>, is a large ' +
    'sandstone rock formation in the southern part of the '+
    'Northern Territory, central Australia. It lies 335 km (208 mi) '+
    'south west of the nearest large town, Alice Springs; 450 km '+
    '(280 mi) by road. Kata Tjuta and Uluru are the two major '+
    'features of the Uluru - Kata Tjuta National Park. Uluru is '+
    'sacred to the Pitjantjatjara and Yankunytjatjara, the '+
    'Aboriginal people of the area. It has many springs, waterholes, '+
    'rock caves and ancient paintings. Uluru is listed as a World '+
    'Heritage Site.</p>'+
    '<p>Attribution: Uluru, <a href="http://en.wikipedia.org/w/index.php?title=Uluru&oldid=297882194">'+
    'http://en.wikipedia.org/w/index.php?title=Uluru</a> (last visited June 22, 2009).</p>'+
    '</div>'+
    '</div>';

var infowindow = new google.maps.InfoWindow({
    content: contentString
});

var marker = new google.maps.Marker({
    position: myLatlng,
    map: map,
    title:"Uluru (Ayers Rock)"
});

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

答案 1 :(得分:0)

你必须确保实例化map对象,然后是contentString,然后是marker然后是infowindow对象,最后添加一个click侦听器。代码按顺序执行,因此在逻辑查看时必须有意义。希望能帮助到你。