2012-06-06 119 views
0

請任何人都可以幫助我。將GEvent addListener添加到谷歌地圖

我想將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> 

回答

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]) 
} 
+0

這個男人它工作得很好 – Boumaili