【问题标题】:add GEvent addListener to google map将 GEvent addListener 添加到谷歌地图
【发布时间】:2012-06-06 16:42:01
【问题描述】:

请任何人帮助我。

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

【问题讨论】:

    标签: php google-maps map


    【解决方案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])
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-19
      • 2012-06-14
      • 2015-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多