I am adding markers to a google map with the following:
function addMarkersToMap(map) {
// trigger resize on map in case canvas size is different
google.maps.event.trigger(map, 'resize');
// remove existing markers
google.maps.Map.prototype.clearMarkers();
// create a new bounds object (don't want existing bounds)
var bounds = new google.maps.LatLngBounds();
// If there is a map center, use it to center the map
$('.mapCenter').each(function() {
var lat = $(this).find('[itemprop="latitude"]').attr('content');
var lng = $(this).find('[itemprop="longitude"]').attr('content');
map.setCenter(new google.maps.LatLng(lat, lng));
});
$('[itemtype="http://schema.org/Place"], [itemtype="http://schema.org/LocalBusiness"]').each(function() {
var title = $(this).find('[itemprop="name"]').text();
var contentString = $(this).html();
var lat = $(this).find('[itemprop="latitude"]').attr('content');
var lng = $(this).find('[itemprop="longitude"]').attr('content');
var icon = $(this).attr('data-class');
var category = $(this).find('[itemprop="category"]').attr('content');
var marker = addMarker(map, title, contentString, lat, lng, icon, category);
bounds.extend(marker.position);
});
// add kml
$('.MapHolder[data-url]').each(function() {
var kml = $(this).attr('data-url');
var kmlLayer = new google.maps.KmlLayer(kml);
kmlLayer.setMap(map);
});
if(!bounds.isEmpty()) {
map.fitBounds(bounds);
}
}
function addMarker(map, title, contentString, lat, lng, icon, category) {
var image;
var shadow;
var shape;
if (icon) {
image = new google.maps.MarkerImage( icon ,
new google.maps.Size(31, 41),
new google.maps.Point(0,0),
new google.maps.Point(15, 40));
}
var marker = new google.maps.Marker({
position: new google.maps.LatLng(lat, lng),
map: map,
title: title,
icon: image,
shape: shape,
shadow: shadow
});
if(contentString != '') {
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent($('<div>' + contentString + '</div>').ajaxify().get(0));
infowindow.open(map, marker);
});
}
return marker;
}
I want to be able to specify a category for each of these markers so that I can toggle the visibility of the markers based on category (ie, show/hide points of interest or show/hide public toilets etc).
How can I modify my existing code to be able to achieve what I am after?
Here is an example (ported from Mike Williams’ v2 tutorial) that does that:
http://www.geocodezip.com/v3_MW_example_categories.html
code snippet: