请任何人帮助我。
我想将GEvent.addListener
添加到地图中的每个标记但是它不起作用我尝试了很多方法但没有结果,你能帮忙吗。
我认为代码只是保留循环中的最后一个标记。
您可以通过以下网址查看我的工作:http://www.ermes.net/user/profile/zoommap.php
<script type="text/javascript">
function getIcon_Comunita() {
var icon = new GIcon();
icon.image = "/include/png/com_locali.png";
icon.iconAnchor = new GPoint(48, 48);
icon.infoWindowAnchor = new GPoint(16, 0);
icon.iconSize = new GSize(30, 34);
return icon;
}
var map1;
function map1_initialize( ) {
<?php
$db=new db_publish;
$db->connect();
$query="SELECT * FROM user_account ua LEFT JOIN user_details ud ON ua.user_id = ud.user_id WHERE ua.user_profileC LIKE '%$user_id%' LIMIT 0,10";
$result=$db->query($query);
while($db->next()) {
if (strlen($db->record["user_coordinates"]) > 0) {
$pieces = explode(",",$db->record["user_coordinates"]);
$resultArrayAd[$cnt]['Lat']=trim($pieces[0]);
$resultArrayAd[$cnt]['Lng']=trim($pieces[1]);
$cnt++;
}
}
?>
if ( google.maps.BrowserIsCompatible( ) )
{
map1 = new google.maps.Map2( document.getElementById( 'map1div' ) );
map1.addControl( new google.maps.LargeMapControl3D( ) );
map1.addControl( new google.maps.MenuMapTypeControl( ) );
map1.setCenter( new google.maps.LatLng( 0, 0 ), 0 );
var latlng = [
<? foreach($resultArrayAd as $doc) { ?>
new google.maps.LatLng(parseFloat(<?=$doc['Lat']?>),parseFloat(<?=$doc['Lng']?>)),
<? } ?>
];
for ( var i = 0; i < latlng.length; i++ )
{
var marker = new google.maps.Marker( latlng[ i ],getIcon_Comunita());
GEvent.addListener(marker, "click", function () {
map1.setZoom(map1.getZoom() + 1);
marker.openInfoWindowHtml("This the coordinate of this point"+marker+"yups");
});
map1.addOverlay( marker );
}
var latlngbounds = new google.maps.LatLngBounds( );
for ( var i = 0; i < latlng.length; i++ )
{
latlngbounds.extend( latlng[ i ] );
}
map1.setCenter( latlngbounds.getCenter( ), map1.getBoundsZoomLevel( latlngbounds ) );
}
}
google.maps.Event.addDomListener( window, 'load', map1_initialize );
google.maps.Event.addDomListener( window, 'unload', google.maps.Unload );
</script>
<div id="map1div" style="height: 600px;"></div>
答案 0 :(得分:1)
您在每次迭代时都会覆盖变量标记:
for ( var i = 0; i < latlng.length; i++ )
{
var marker = new google.maps.Marker( latlng[ i ],getIcon_Comunita());
GEvent.addListener(marker, "click", function () {
map1.setZoom(map1.getZoom() + 1);
marker.openInfoWindowHtml("This the coordinate of this point"+marker+"yups");
});
map1.addOverlay( marker );
}
要解决此问题,您可以在循环中使用匿名函数:
for ( var i = 0; i < latlng.length; i++ )
{
(
function(latlng){
var marker=new google.maps.Marker( latlng,getIcon_Comunita());
GEvent.addListener(marker, "click", function () {
map1.setZoom(map1.getZoom() + 1);
marker.openInfoWindowHtml("This the coordinate of this point:"
+latlng.toUrlValue());
});
map1.addOverlay(marker);
})(latlng[i])
}