blokmap/src/js/map.js

172 lines
5.7 KiB
JavaScript
Raw Normal View History

2014-12-11 17:26:15 +00:00
$(document).ready(function() {
2020-05-20 15:58:11 +00:00
L.Icon.Default.imagePath = "img/";
2020-05-20 15:58:11 +00:00
var popuptemplate = Handlebars.compile($("#popup-template").html());
2014-12-11 17:26:15 +00:00
var RedIcon = L.Icon.Default.extend({
options: {
2020-05-20 15:58:11 +00:00
iconUrl: "red-marker.png"
2014-12-11 17:26:15 +00:00
}
});
2015-12-24 16:50:50 +00:00
var ChristmasIcon = L.Icon.Default.extend({
options: {
2020-05-20 15:58:11 +00:00
iconUrl: "christmas-marker.png"
2015-12-24 16:50:50 +00:00
}
});
var blueIcon = new L.Icon.Default();
2014-12-11 17:26:15 +00:00
var redIcon = new RedIcon();
2015-12-24 16:50:50 +00:00
var christmasIcon = new ChristmasIcon();
var now = new Date();
var christmasSeason =
now.getTime() > new Date(now.getFullYear() + "-12-20").getTime() ||
now.getTime() < new Date(now.getFullYear() + "-01-04").getTime();
2014-12-11 17:26:15 +00:00
function onEachFeature(feature, layer) {
if (feature.properties) {
2019-02-17 15:57:08 +00:00
layer.bindPopup(popuptemplate(feature.properties), {
2020-05-20 15:58:11 +00:00
showOnMouseOver: true
2019-02-17 15:57:08 +00:00
});
2014-12-11 17:26:15 +00:00
}
}
function pointToLayer(feature, latlng) {
var icon = redIcon;
2020-05-20 15:58:11 +00:00
if (feature.properties) {
if (feature.properties.holidays && christmasSeason) {
icon = christmasIcon;
} else if (!feature.properties.hours.saturday && !feature.properties.hours.sunday) {
icon = blueIcon;
2020-05-20 15:58:11 +00:00
}
2014-12-11 17:26:15 +00:00
}
return new HoverMarker(latlng, { icon: icon, riseOnHover: true});
2014-12-11 17:26:15 +00:00
}
2020-05-20 15:58:11 +00:00
var map = L.map("map").setView([50.702, 4.335], 9);
var osm = L.tileLayer("https://{s}.tiles.mapbox.com/v3/feliciaan.keoaj8d5/{z}/{x}/{y}{r}.png", {
2020-05-20 15:33:11 +00:00
attribution: 'Achtergrondkaart © <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a>-bijdragers (data) en © <a href="https://www.mapbox.com/map-feedback/">Mapbox</a> (stijl) | <span lang="en">Made with ❤ by <a href="https://zeus.gent/">Zeus WPI</a></span>',
detectRetina: true
2015-02-13 17:52:57 +00:00
}).addTo(map);
2014-12-11 17:26:15 +00:00
2020-05-20 15:58:11 +00:00
$.getJSON("https://blokdata.zeus.gent/data.json")
.done(function(data) {
var geojson = L.geoJson(data, {
onEachFeature: onEachFeature,
pointToLayer: pointToLayer
});
map.addLayer(geojson);
2014-12-11 17:26:15 +00:00
});
2014-12-11 21:54:02 +00:00
var SimpleControl = L.Control.extend({
2020-05-20 15:58:11 +00:00
initialize: function(templateId, divClass, options) {
this.template = Handlebars.compile($(templateId).html());
this.divClass = divClass;
L.Util.setOptions(this, options);
},
onAdd: function (map) {
this._div = L.DomUtil.create("div", this.divClass);
this._div.innerHTML = this.template();
return this._div;
}
2014-12-11 21:54:02 +00:00
});
2020-05-20 15:58:11 +00:00
2019-02-17 15:57:08 +00:00
// code copied from http://jsfiddle.net/sowelie/3JbNY/
var HoverMarker = L.Marker.extend({
2020-05-20 15:58:11 +00:00
bindPopup: function(htmlContent, options) {
2020-05-20 14:12:27 +00:00
2020-05-20 15:58:11 +00:00
if (options && options.showOnMouseOver) {
2020-05-20 14:12:27 +00:00
2020-05-20 15:58:11 +00:00
L.Marker.prototype.bindPopup.apply(this, [htmlContent, options]);
2020-05-20 14:12:27 +00:00
2020-05-20 15:58:11 +00:00
this.off("click", this.openPopup, this);
2020-05-20 14:12:27 +00:00
2020-05-20 15:58:11 +00:00
this.on("mouseover", function(e) {
2020-05-20 14:12:27 +00:00
2020-05-20 15:58:11 +00:00
// get the element that the mouse hovered onto
var target = e.originalEvent.fromElement || e.originalEvent.relatedTarget;
var parent = this._getParent(target, "leaflet-popup");
2020-05-20 14:12:27 +00:00
2020-05-20 15:58:11 +00:00
// check to see if the element is a popup, and if it is this marker's popup
if (parent == this._popup._container)
return true;
2020-05-20 14:12:27 +00:00
2020-05-20 15:58:11 +00:00
this.openPopup();
2020-05-20 14:12:27 +00:00
2020-05-20 15:58:11 +00:00
}, this);
2020-05-20 14:12:27 +00:00
2020-05-20 15:58:11 +00:00
this.on("mouseout", function(e) {
2020-05-20 14:12:27 +00:00
2020-05-20 15:58:11 +00:00
// get the element that the mouse hovered onto
var target = e.originalEvent.toElement || e.originalEvent.relatedTarget;
2020-05-20 14:12:27 +00:00
2020-05-20 15:58:11 +00:00
// check to see if the element is a popup
if (this._getParent(target, "leaflet-popup")) {
L.DomEvent.on(this._popup._container, "mouseout", this._popupMouseOut, this);
return true;
}
2020-05-20 14:12:27 +00:00
2020-05-20 15:58:11 +00:00
this.closePopup();
2020-05-20 14:12:27 +00:00
2020-05-20 15:58:11 +00:00
}, this);
2019-02-17 15:57:08 +00:00
}
2020-05-20 15:58:11 +00:00
},
2020-05-20 14:12:27 +00:00
2020-05-20 15:58:11 +00:00
_popupMouseOut: function(e) {
// detach the event
L.DomEvent.off(this._popup, "mouseout", this._popupMouseOut, this);
2020-05-20 14:12:27 +00:00
2020-05-20 15:58:11 +00:00
// get the element that the mouse hovered onto
var target = e.toElement || e.relatedTarget;
2020-05-20 14:12:27 +00:00
2020-05-20 15:58:11 +00:00
// check to see if the element is a popup
if (this._getParent(target, "leaflet-popup"))
return true;
2020-05-20 14:12:27 +00:00
2020-05-20 15:58:11 +00:00
// check to see if the marker was hovered back onto
if (target == this._icon)
return true;
2020-05-20 14:12:27 +00:00
2020-05-20 15:58:11 +00:00
this.closePopup();
},
2020-05-20 14:12:27 +00:00
2020-05-20 15:58:11 +00:00
_getParent: function(element, className) {
var parent = element.parentNode;
2020-05-20 14:12:27 +00:00
2020-05-20 15:58:11 +00:00
while (parent) {
if (parent.className && L.DomUtil.hasClass(parent, className))
return parent;
parent = parent.parentNode;
}
2020-05-20 14:12:27 +00:00
2020-05-20 15:58:11 +00:00
return false;
2019-02-17 15:57:08 +00:00
}
});
2014-12-11 20:36:36 +00:00
2020-05-20 15:58:11 +00:00
var info = new SimpleControl("#info-template", "info", {
position: "topright"
2014-12-11 21:54:02 +00:00
}).addTo(map);
2014-12-11 20:36:36 +00:00
2020-05-20 15:58:11 +00:00
var sharePane = new SimpleControl("#share-template", "info", {
position: "bottomleft"
2014-12-11 21:54:02 +00:00
}).addTo(map);
2014-12-11 20:36:36 +00:00
var legend = new SimpleControl("#legend-template", christmasSeason ? "holiday-legend" : "legend", {
2020-05-20 15:58:11 +00:00
position: "bottomright"
2014-12-11 21:54:02 +00:00
}).addTo(map);
var monthNames = ["jan.", "feb.", "mrt.", "apr.", "mei", "juni", "juli", "aug.", "sep.", "okt.", "nov.", "dec."];
Handlebars.registerHelper("date", function (ddmyyyy) {
var parts = ddmyyyy.split("-");
var date = parts[0];
var month = monthNames[parts[1] - 1];
var year = parts[2];
return date + " " + month + " " + year;
})
2014-12-11 17:26:15 +00:00
});