如何按组显示/隐藏Google地图标记,并从非HTML复选框触发事件?

时间:2012-07-27 05:39:56

标签: google-maps javascript-events google-maps-api-3 google-maps-markers

如何通过点击HTML元素(在这种情况下为复选框)来显示或隐藏不同组的标记(比如说条形图/电影院/停放处)?

我的标记从循环生成一个数组,如下所示:

markers[i] = new google.maps.Marker({
numero : i, 
position: latLng,
map: map,
    info: data.Description,
group: data.category,
}); 

我想我应该使用:

  • 点击我的HTML元素,使用Js函数。

  • 此Js函数应包含此Gmaps方法

  

调用setVisible

,像这样:

    google.maps.event.addListener(marker, 'click', function() {
    marker.setVisible(false); // maps API hide call
    });
  • 和事件触发器:

     google.maps.event.trigger(markers[i], 'click');
    

但是现在我怎么能把这些东西混在一起:)?

2 个答案:

答案 0 :(得分:2)

关闭。假设markers包含给定组中所有标记的数组,您可以为复选框创建一个onchange处理程序,该处理程序将隐藏组中的所有标记,如此。在您的HTML中:

<input id="myCheckbox" type="checkbox" checked="checked" />

稍后,在您的脚本中,

// handler
function onClickHandler (e) {
    var i = 0, marker;
    var visible = e.target.checked; // show if checked, otherwise hide

    while (marker = markers[i++]) {
        marker.setVisible(visible); // maps API hide call
    }
}

// bind handler to checkbox. 
document.getElementById('myCheckbox').onchange = onClickHandler;​

供参考,请查看this fiddle

答案 1 :(得分:0)

概念很简单。只需为标记定义全局数组。推送所有标记和onchange事件显示/隐藏标记。结帐以下代码 小号@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ NI

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Test</title>
     <script src='http://code.jquery.com/jquery.min.js' type='text/javascript'></script>
</head>
<body>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3&sensor=false"></script>

<script type="text/javascript">


function initialize() 
{

  //SANI: Show hide ker marker



  //SANI: Add ker Map
  var myLatlng   = new google.maps.LatLng(31.553710, 74.358446);  //SANI: Lahore di  location
  var mapOptions = {
                        zoom: 12,
                        center: myLatlng,
                        mapTypeControl: true,
                        mapTypeControlOptions: {  //SANI: Map da style ki hovy
                                                    //style: google.maps.MapTypeControlStyle.DROPDOWN_MENU,
                                                    //mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'map_style']
                                                    mapTypeIds: google.maps.MapTypeId.ROADMAP
                                                },
                        navigationControl: true,
                   }
  var currentmap = new google.maps.Map(document.getElementById('mapbySani'), mapOptions);
  /****************************************************************************************/
  var arrMarkers = [];
  var markers = [
                    [31.552174,74.360012,"Info for first marker"],
                    [31.550547,74.371599,"Info for second marker"],
                    [31.549943,74.380289,"Info for last marker"]
                ];

    for(var i=0; i < markers.length; i++)
    {

        var latLng          = new google.maps.LatLng(markers[i][0], markers[i][1]);  //SANI: sari location lai loop ich
        var contentString   =  markers[i][2];  //SANI: info text vi lya        
        var infowindow      = new google.maps.InfoWindow({content: contentString});   //SANI: info winsow bani 
       var marker           = new google.maps.Marker({
                                                        position: latLng,
                                                        map: currentmap
                                                    }); //SANI: marker lay sary
         google.maps.event.addListener(marker, 'click', function()
        {
            infowindow.open(currentmap,marker);
        });
        //SANI: marker dy click ty info window show krai
        //console.log(marker); 
        arrMarkers.push(marker);
    }

      //SANI: show hide ker on click listener ty
      $("#allmarkers").change(function()
      {
          if(this.checked) 
          {
            //marker.setVisible(true);
              if (arrMarkers) 
              {
                for( var i = 0, n = arrMarkers.length; i < n; ++i ) 
                {
                  arrMarkers[i].setVisible(true);
                }
              }
          }else{
                     if (arrMarkers) 
                      {
                        for( var i = 0, n = arrMarkers.length; i < n; ++i ) 
                        {
                          arrMarkers[i].setVisible(false);
                        }
                      }
               }
      });













  /******************************************************************************************/
  //SANI: add ker marker
  //addMarker(myLatlng, currentmap);
  /******************************************************************************************/
  //SANI: add ker polygon
  /* var zone1;
  var triangleCoords = [
                            new google.maps.LatLng(25.05730, 55.27144),
                            new google.maps.LatLng(25.05854, 55.28526),
                            new google.maps.LatLng(25.05676, 55.28741),
                            new google.maps.LatLng(25.05357, 55.28741),
                            new google.maps.LatLng(25.04486, 55.27213),
                            new google.maps.LatLng(25.04455, 55.25642),
                            new google.maps.LatLng(25.04284, 55.25342),
                            new google.maps.LatLng(25.05147, 55.24947),
                            new google.maps.LatLng(25.05528, 55.25419),
                            new google.maps.LatLng(25.05738, 55.27153)
                      ];

  addPolygon(zone1, triangleCoords, currentmap);*/




}

google.maps.event.addDomListener(window, 'load', initialize); //SANI: Window dy load ty map show ker dy


/* //SANI: S@@@@@@@@@@@@@@@@@@@@@@@@@@@@@  //SANI: Skeleton  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@NI */
function addMarker(keriJagaTy, KeryMapTy)
{
   var marker = new google.maps.Marker({
      position: keriJagaTy,
      map: KeryMapTy,
      title: 'Marker added by Sani',
      //SANI: jay apni merzi da icon show kerna wa ty
      icon:'images/client.png'
  });
}


function addPolygon(keraZone, KerijagaTy, KeryMapTy)
{
     keraZone = new google.maps.Polygon({
                                    paths: KerijagaTy,
                                    strokeColor: "#3299CC",
                                    strokeOpacity: 0.8,
                                    strokeWeight: 2,
                                    fillColor: "#3299CC",
                                    fillOpacity: 0.05
                                  });

  keraZone.setMap(KeryMapTy);
}
/* //SANI: S@@@@@@@@@@@@@@@@@@@@@@@@@@@@@  //SANI: END Skeleton  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@NI */



</script>
<div>
    <table>
        <tr>
            <td><input type="checkbox" value="marker" id="allmarkers" /><label>Marker</label></td>
        </tr>
    </table>
</div>
<div id="mapbySani" style="width: 100%; height: 600px;"></div>
</body>
</html>

取值@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ NI

感谢