按类别切换开启/关闭Google地图标记

时间:2011-04-29 04:39:40

标签: google-maps google-maps-api-3 google-maps-markers markermanager

我有以下内容,并希望按cat: dataMarkers.cat下提取的各种类别类型对标记进行排序。我有点失去了如何以最佳方式实际做到这一点。我在考虑每个类别的markerManager?我是否需要将每个类别放在自己的markerManager数组中?这如何使用markerClusterer?

var map;

function initialize() {
        var center = new google.maps.LatLng(39.632906,-106.524591);
        var options = {
          'zoom': 8,
          'center': center,
          'mapTypeId': google.maps.MapTypeId.ROADMAP
        };

        var map = new google.maps.Map(document.getElementById("map_canvas"), options);

            <!--Load Markers-->
                    var markers = [];
                    for (var i = 0, dataMarkers; dataMarkers = data.markers[i]; i++) {
                      var latLng = new google.maps.LatLng(dataMarkers.lat, dataMarkers.lng);
                      var marker = new google.maps.Marker({
                        position: latLng,
                        title: dataMarkers.title,
                        date: dataMarkers.date,
                        time: dataMarkers.time,
                        desc: dataMarkers.desc,
                        img: dataMarkers.img,
                        add: dataMarkers.address,
                        cat: dataMarkers.cat,
                        map: map
                      });

            <!--Display InfoWindows-->    
                    var infowindow = new google.maps.InfoWindow({
                            content: " "
                          });
                          google.maps.event.addListener(marker, 'click', function() {
                            infowindow.setContent('<div id="mapCont"><img class="mapImg" src="'+this.img+'"/>' +
                                                  '<div class="mapTitle">'+this.title+'</div>' + 
                                                  '<div class="mapHead">Date: <div class="mapInfo">'+this.date+'</div>' +
                                                  '<div class="mapHead">Time: <div class="mapInfo">'+this.time+'</div>' +
                                                  '<p>'+this.desc+'</p></div>');
                            infowindow.open(map, this);
                          });

                      markers.push(marker);
                    }

            <!--Cluster Markers-->
                var markerClusterer = new MarkerClusterer(map, markers, {
                  maxZoom: 15,
                  gridSize: 50
                });

}

1 个答案:

答案 0 :(得分:3)

我建议您将所有标记存储在数组中。在每个标记上创建一个名为category的字段,并为其指定正确的值。

每当类别发生变化时,您都会浏览标记数组,并在正确类别中设置marker.visible = true,并在所有其他类别上设置marker.visible = false

在JavaScript中,可以轻松地为对象动态分配新字段。你只想说:

var marker = new google.maps.Marker({
    position: latLng,
    title: dataMarkers.title,
    date: dataMarkers.date,
    time: dataMarkers.time,
    desc: dataMarkers.desc,
    img: dataMarkers.img,
    add: dataMarkers.address,
    cat: dataMarkers.cat,
    map: map
});
marker.category = THE_CATEGORY_IT_BELONGS_TO;

只要标记存在,标记就会有一个名为category的字段。