2012-02-29 62 views
-1

我正在设置不同机场位置的地图。我的标记位于正确的位置。Google Maps APIv3多个标记/多个信息窗口

我一直在尝试添加infowindows,但是每个InfoWindow都显示了数组中最后一个标记的相同内容。

我该如何解决这个问题,让每个标记都有自己独特的信息?

`setMarkers(map, airports); 
} 

    function setMarkers(map, locations) { 

    for (var i = 0; i < locations.length; i++) { 
var airport = locations[i]; 
var myLatLng = new google.maps.LatLng(airport[1], airport[2]); 
var marker = new google.maps.Marker({ 
    position: myLatLng, 
    map: map, 
html: airport[3], 
}); 

var contentString = 
<?php 
$num_rows = (count($locations) - 1); 

for($i = 0; $i < count($locations); $i++){ 

    $query = "SELECT * FROM airports WHERE ident='$locations[$i]'"; 
    $result = mysql_query($query, $link) 
or die('Error querying database.'); 

    while ($row = mysql_fetch_array($result)) { 

if ($i == $num_rows){ 

echo "'<div id=\"content\">'+"; 
echo "'<div id=\"siteNotice\">'+"; 
    echo "'</div>'+"; 
echo "'<b>" . $row['ident'] . "</b></br>'+"; 
echo "'" . $row['latitude_deg'] . ", " . $row['longitude_deg'] . "' +"; 
echo "'</div>'+"; 
    echo "'</div>';"; 
} 
} 
} 
?> 



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

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

回答

1

您需要将每个infowindow所需的数据存储在可通过索引编号访问的某种数组中。

当用户点击标记时访问索引号。索引号然后用于动态构建infowindow内容并打开infowindow。

google.maps.event.addListener(marker, 'click', (function(event, index) { 
    return function(){ 
    infowindow.content = markerArray[index].label + "<br>" + markerArray[index].text; 
    infowindow.open(map,this); 
    } 
})(marker,i)); 

代码来自:http://srsz750b.appspot.com/api3/polylines-multiple.html

相关问题