Google地图标记分组

Avi*_*rix 10 javascript google-maps google-maps-api-3 google-maps-markers

我正在努力实现以下目标:在我的地图上有不同类型的标记,例如学校,图书馆,公共汽车站,我希望能够显示/隐藏每组标记.

我一直在搜索谷歌一段时间但没有出现:/

任何想法如何实现这一目标?

Dan*_*llo 18

有多种方法可以解决这个问题,但让我向您展示一种方法.

首先,让我们从一系列位置开始(借用Google Maps API教程):

var beaches = [
  ['Bondi Beach', -33.890542, 151.274856, 1],
  ['Coogee Beach', -33.923036, 151.259052, 1],
  ['Cronulla Beach', -34.028249, 151.157507, 2],
  ['Manly Beach', -33.800101, 151.287478, 2],
  ['Maroubra Beach', -33.950198, 151.259302, 2]
];
Run Code Online (Sandbox Code Playgroud)

这实际上是一个数组数组.它代表5个澳大利亚海滩,我们有名称,纬度,经度和类别.为简单起见,这种情况下的类别只是一个数字.

然后重要的是我们保留我们创建的标记的参考.为此,我们可以使用markers存储每个新标记的数组,并且我们还可以使用其类别ID扩充每个标记对象:

var markers = [];

var i, newMarker;

for (i = 0; i < beaches.length; i++) {
  newMarker = new google.maps.Marker({
    position: new google.maps.LatLng(beaches[i][1], beaches[i][2]),
    map: map,
    title: beaches[i][0]
  });     

  newMarker.category = beaches[i][3];     
  newMarker.setVisible(false);

  markers.push(newMarker);
}
Run Code Online (Sandbox Code Playgroud)

最后,当我们需要显示标记时,我们可以简单地迭代markers数组,并setVisible()根据我们想要显示的类别调用方法.

您可能需要查看以下完整示例:

<!DOCTYPE html>
<html> 
<head> 
   <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
   <title>Google Maps JavaScript API v3 Example: Marker Categories</title> 
   <script type="text/javascript" 
           src="http://maps.google.com/maps/api/js?sensor=false"></script>
</head> 
<body> 
   <div id="map" style="width: 400px; height: 300px;"></div> 

   <input type="button" value="Show Group 1" onclick="displayMarkers(1);">
   <input type="button" value="Show Group 2" onclick="displayMarkers(2);">

   <script type="text/javascript"> 

   var beaches = [
     ['Bondi Beach', -33.890542, 151.274856, 1],
     ['Coogee Beach', -33.923036, 151.259052, 1],
     ['Cronulla Beach', -34.028249, 151.157507, 2],
     ['Manly Beach', -33.800101, 151.287478, 2],
     ['Maroubra Beach', -33.950198, 151.259302, 2]
   ];

   var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 10,
      center: new google.maps.LatLng(-33.88, 151.28),
      mapTypeId: google.maps.MapTypeId.ROADMAP
   });

   var markers = [];

   var i, newMarker;

   for (i = 0; i < beaches.length; i++) {
     newMarker = new google.maps.Marker({
       position: new google.maps.LatLng(beaches[i][1], beaches[i][2]),
       map: map,
       title: beaches[i][0]
     });

     newMarker.category = beaches[i][3];
     newMarker.setVisible(false);

     markers.push(newMarker);
   }

   function displayMarkers(category) {
     var i;

     for (i = 0; i < markers.length; i++) {
       if (markers[i].category === category) {
         markers[i].setVisible(true);
       }
       else {
         markers[i].setVisible(false);
       }
     }
   }

   </script> 
</body> 
</html>
Run Code Online (Sandbox Code Playgroud)

点击"显示组2"按钮后,上面的示例截图:

Google Maps JavaScript API v3示例:标记类别http://img535.imageshack.us/img535/8485/markercat.jpg