2012-01-31 148 views
0

我从API获取一些信息。 我打电话给地理编码器以获取该点,然后创建我的标记,并为信息窗口显示一些文本。 我的标记放在地图上没有问题。Proplem与谷歌地图infowindow

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

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; 

}}

+0

您可以检查结果这里:www.pukkafish.com/api/linkedin。您将需要一个linkedIn帐户。 – Olivier 2012-02-01 08:51:51

回答

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