谷歌地图中的infowindow问题

时间:2012-01-31 23:55:04

标签: google-maps google-maps-markers infowindow

我从API获取了一些信息。 我正在调用地理编码器来获取该点,然后创建我的标记,并为信息窗口添加一些文本。 我的标记放在地图上没有问题。

但是当我点击标记时,它总是显示相同的文字。 我无法弄清楚如何“预先填充”信息窗口,所以当我点击标记时它会显示正确的信息......

function createMarker(point, text) {
alert('point: ' + point + 'text: ' + text)
var html ="<div class='infowindow'><strong>"+ text + "<\/strong><\/div>";
//var marker = new GMarker(point, markerOptions);
var marker = new google.maps.Marker({    
    map: map,  
    position: point
});  

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

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

return marker;
}

我希望它足够清楚! 我正在使用谷歌API V3

使用以下代码解决它:

function createMarker(point, text) {
//var html ="<div class='infowindow'><strong>"+ text + "<\/strong><\/div>";
//var marker = new google.maps.Marker({    
//    map: map,  
//    position: point
//});  

//infowindow = new google.maps.InfoWindow({  
//  content: html  
//});  

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

//});       
//return marker;

var title = 'LinkedIn Connection';
var html = "<div class='infowindow'><strong>"+ text + "<\/strong><\/div>";

var marker = new google.maps.Marker({
    title:title,
    content:html,
    map:map,
    draggable:false,
    position:point
});

google.maps.event.addListener(marker, 'click', function() {

    /* close the previous info-window */
    closeInfos();

    /* the marker's content gets attached to the info-window: */
    var info = new google.maps.InfoWindow({content: this.content});

    /* trigger the infobox's open function */
    info.open(map,this);

    /* keep the handle, in order to close it on next click event */
    infos[0]=info;

});

}

function closeInfos(){

   if(infos.length > 0){

  /* detach the info-window from the marker */
  infos[0].set("marker",null);

  /* and close it */
  infos[0].close();

  /* blank the array */
  infos.length = 0;

} }

1 个答案:

答案 0 :(得分:0)

function createMarker(point, text) {
//var html ="<div class='infowindow'><strong>"+ text + "<\/strong><\/div>";
//var marker = new google.maps.Marker({    
//    map: map,  
//    position: point
//});  

//infowindow = new google.maps.InfoWindow({  
//  content: html  
//});  

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

//});       
//return marker;

var title = 'LinkedIn Connection';
var html = "<div class='infowindow'><strong>"+ text + "<\/strong><\/div>";

var marker = new google.maps.Marker({
    title:title,
    content:html,
    map:map,
    draggable:false,
    position:point
});

google.maps.event.addListener(marker, 'click', function() {

    /* close the previous info-window */
    closeInfos();

    /* the marker's content gets attached to the info-window: */
    var info = new google.maps.InfoWindow({content: this.content});

    /* trigger the infobox's open function */
    info.open(map,this);

    /* keep the handle, in order to close it on next click event */
    infos[0]=info;

});
}

function closeInfos(){

   if(infos.length > 0){

  /* detach the info-window from the marker */
  infos[0].set("marker",null);

  /* and close it */
  infos[0].close();

  /* blank the array */
  infos.length = 0;
} }