-
Notifications
You must be signed in to change notification settings - Fork 112
/
map.js
134 lines (108 loc) · 3.48 KB
/
map.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
var url = 'https://raw.githubusercontent.com/okfn/opendataday/master/databags/events-2024.json';
var map = L.map('map');
// on load map event
map.on('load', function () {
$.getJSON(url).done(function (data) {
var events = data.events || [];
var geojson = {
type: 'FeatureCollection',
features: []
};
events.forEach(function (event) {
var lat, lng, title;
try {
lat = Number(event.latitude);
lng = Number(event.longitude);
title = truncate(event.event_name, 20);
} catch (error) {
console.error(error);
console.log('Error processing event "' + event.event_name + '"');
}
if (lng && lat && title) {
// add it to the map
let marker = L.marker([lat, lng]).addTo(map);
// set up a description for on mouse over
marker.bindPopup(getDescription(event));
geojson.features.push({
type: 'Feature',
properties: {
title: title,
icon: "marker",
description: getDescription(event)
},
geometry: {
type: 'Point',
coordinates: [lng, lat]
}
});
}
});
$("#event-number").text(events.length);
map.on('click', function (e) {
var features = map.queryRenderedFeatures(e.point, { layers: ['points'] });
if (!features.length) {
return;
}
var feature = features[0];
var descriptions = features.map(function (f) { return f.properties.description; });
// create a leaflet popup whit descriptions as HTML
var popup = L.popup()
.setLatLng([e.lngLat.lat, e.lngLat.lng]).setContent(descriptions.join('<br><br>')).openOn(map);
});
});
});
map.setView([0, 0], 2);
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
function truncate(str, n) {
return (str.length > n) ? str.substr(0, n - 1) + '…' : str;
}
function isWorkingUrl(url) {
return new RegExp('^' + 'http').test(url);
}
function getLink(url) {
return '<a href="' + url + '" target="_blank">' + url + '</a>';
}
function getDescription(event) {
var htmlStr = '<strong>Event:</strong> ' + event.event_name;
var date = event.event_date ? new Date(event.event_date).toDateString() : '';
if (date) {
htmlStr += '<br><strong>Date:</strong> ' + date;
}
if (isWorkingUrl(event.url)) {
htmlStr += '<br><strong>URL:</strong> ' + getLink(event.url);
}
if (event.event_time) {
htmlStr += '<br><strong>Time:</strong> ' + event.event_time;
if (event.timezone) {
htmlStr += ' (' + event.timezone + ')';
}
}
if (event.online) {
htmlStr += '<br><strong>Location:</strong> Online';
if (event.country) {
htmlStr += ', ' + event.country;
}
if (event.world_region_text) {
htmlStr += ' (' + event.world_region_text + ')';
}
if (isWorkingUrl(event.online_event_url)) {
htmlStr += '<br><strong>Online Event:</strong> ' + getLink(event.online_event_url);
}
} else {
if (event.place) {
htmlStr += '<br><strong>Location:</strong> ' + event.place;
if (event.country) {
htmlStr += ', ' + event.country;
}
if (event.world_region_text) {
htmlStr += ' (' + event.world_region_text + ')';
}
}
}
return htmlStr;
}
// Add control to map
L.control.scale().addTo(map);
L.control.zoom({ position: 'topright' }).addTo(map);