Question

I have the below and want to sort the markers by the various category type being pulled under cat: dataMarkers.cat. Im a bit lost as to how to actually do this the best way. I'm thinking markerManager for each category? Would I need each category to be in its own markerManager array? How does this factor in with 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
                });

}
Was it helpful?

Solution

I recommend you store all the markers in an array. Create a field called category on each marker and give it the correct value.

Whenever the category changes, you run through your array of markers and set marker.visible = true on the ones in the correct category, and marker.visible = false on all the others.

In JavaScript, it is easy to dynamically assign new fields to an object. You would simply say:

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;

The marker will then have a field called category as long as it exists.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top