Skip to content

Instantly share code, notes, and snippets.

@jmatt
Created April 4, 2012 20:54
Show Gist options
  • Select an option

  • Save jmatt/2305561 to your computer and use it in GitHub Desktop.

Select an option

Save jmatt/2305561 to your computer and use it in GitHub Desktop.
Part of map.js that adds and filters markers on the map.
// Removes all markers on the map.
function clearOverlays(category) {
if (markersArray) {
for (i in markersArray) {
if (category === undefined) {
markersArray[i].setMap(null);
}
else {
if (markersArray[i].val.Category.Name == category) {
markersArray[i].setMap(null);
}
}
}
}
markersArray.length = 0;
}
// Add all markers to the map.
function allMap() {
categoryArray.length = 0;
$.getJSON("/Books/Map", function (data) {
clearOverlays();
$.each(data, function (key, val) {
createMarker(map, new google.maps.LatLng(val.Latitude, val.Longitude), val.Title + " \n" + val.Place, val);
});
});
}
allMap(); // This adds all markers when map.js is first interpreted onload.
// Change the icons based on the zoomlevel
google.maps.event.addListener(map, 'zoom_changed', function () {
if (markersArray) {
for (i in markersArray) {
setInfoboxPixelOffset();
markersArray[i].setIcon(RATLL.get_icon(markersArray[i].val, map.getZoom()));
}
}
document.body.style.cursor = "default";
});
// Filter markers and only add them if their category is in the categoryArray.
function filterMap() {
$.getJSON("/Books/Map", function (data) {
clearOverlays();
$.each(data, function (key, val) {
if ($.inArray(val.Category.Name, categoryArray) !== -1) {
createMarker(map, new google.maps.LatLng(val.Latitude, val.Longitude), val.Title + "\n" + val.Place, val);
}
});
});
}
// Assign the map object to an element in the dom. Then it'll be accessible later on, outside of this closure.
document.getElementById("map").map = map;
// Click event and filtering for the fiction category / checkbox.
$("#chk_fiction:checkbox").click(function () {
var checked = $("#chk_fiction:checked");
//console.log($("input[id=chk_all]").is(":checked"));
if ($("input[id=chk_all]").is(":checked")) {
$("input[id=chk_all]").attr("checked", false);
clearOverlays();
}
if (checked.length > 0) {
if ($.inArray("Fiction", categoryArray) === -1) {
categoryArray.push("Fiction");
}
filterMap();
} else {
var index = $.inArray("Fiction", categoryArray);
if (index > -1) {
categoryArray.splice(index, 1);
}
filterMap();
}
});
// Click event and filtering for the nonfiction category / checkbox.
$("#chk_nonfiction:checkbox").click(function () {
var checked = $("#chk_nonfiction:checked");
if ($("input[id=chk_all]").is(":checked")) {
$("input[id=chk_all]").attr("checked", false);
clearOverlays();
}
if (checked.length > 0) {
if ($.inArray("Nonfiction", categoryArray) === -1) {
categoryArray.push("Nonfiction");
}
filterMap();
} else {
var index = $.inArray("Nonfiction", categoryArray);
if (index > -1) {
categoryArray.splice(index, 1);
}
filterMap();
}
});
// Click event and filtering for the teen category / checkbox.
$("#chk_teen:checkbox").click(function () {
var checked = $("#chk_teen:checked");
if ($("input[id=chk_all]").is(":checked")) {
$("input[id=chk_all]").attr("checked", false);
clearOverlays();
}
if (checked.length > 0) {
if ($.inArray("Teen", categoryArray) === -1) {
categoryArray.push("Teen");
}
filterMap();
} else {
var index = $.inArray("Teen", categoryArray);
if (index > -1) {
categoryArray.splice(index, 1);
}
filterMap();
}
});
// Click event and filtering for the children category / checkbox.
$("#chk_children:checkbox").click(function () {
var checked = $("#chk_children:checked");
if ($("input[id=chk_all]").is(":checked")) {
$("input[id=chk_all]").attr("checked", false);
clearOverlays();
}
if (checked.length > 0) {
if ($.inArray("Children", categoryArray) === -1) {
categoryArray.push("Children");
}
filterMap();
} else {
var index = $.inArray("Children", categoryArray);
if (index > -1) {
categoryArray.splice(index, 1);
}
filterMap();
}
});
// Click event and filtering for the View All category / checkbox.
$("#chk_all:checkbox").click(function () {
var checked = $("#chk_all:checked");
if (checked.length > 0) {
clearOverlays();
categoryArray.length = 0;
$("input[id=chk_fiction]").attr("checked", false);
$("input[id=chk_nonfiction]").attr("checked", false);
$("input[id=chk_teen]").attr("checked", false);
$("input[id=chk_children]").attr("checked", false);
allMap();
} else {
categoryArray.length = 0;
clearOverlays();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment