0

誰でも私を助けてください。

マップ内のすべてのマーカーに追加したいのです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>
4

1 に答える 1

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])
 }
于 2012-06-06T17:11:08.043 に答える