-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
83 lines (65 loc) · 3.57 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Singapore, taxified</title>
<link rel="stylesheet" href="index.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.8.0/leaflet.css" integrity="sha512-hoalWLoI8r4UszCkZ5kL8vayOGVae1oxXe/2A4AO6J9+580uKHDO3JdHb7NzwwzK5xr/Fs0W40kiNHxM9vyTtQ==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.8.0/leaflet.js" integrity="sha512-BB3hKbKWOc9Ez/TAwyWxNXeoV9c1v6FIeYiBieIWkpLjauysF18NzgR1MBNBXf8/KABdlkX68nAhlwcDFLGPCQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.2/axios.min.js"></script>
<link rel="apple-touch-icon" sizes="180x180" href="/assets/favicons/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/assets/favicons/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/assets/favicons/favicon-16x16.png">
<link rel="manifest" href="/assets/favicons/site.webmanifest">
</head>
<body>
<div class="container">
<div class="main">
<h1 class="title">Singapore, taxified</h1>
<p class="description">🚕 Visualize Singapore taxi locations using real-time taxi availability data from <a href="https://data.gov.sg/dataset/taxi-availability">data.gov.sg</a>. Refreshes every 30 seconds.</p>
<div id="map"></div>
</div>
</div>
<script>
// utility function to run a function immediately when setInterval is called
(function() {
var originalSetInterval = window.setInterval;
window.setInterval = function(fn, delay, runImmediately) {
if(runImmediately) fn();
return originalSetInterval(fn, delay);
};
})();
// map setup
var map = L.map("map").setView([1.366667, 103.800000], 11);
var tiles = L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
maxZoom: 20,
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
}).addTo(map);
// map markers
var markerGroup = L.layerGroup().addTo(map);
// custom marker icon
var taxiIcon = L.icon({
iconUrl: 'assets/taxi.png',
iconSize: [24, 15],
});
// get taxi data
setInterval(function() {
// clear marker group
markerGroup.clearLayers();
axios.get('https://api.data.gov.sg/v1/transport/taxi-availability')
.then(function (response) {
const data = response.data;
// add new markers
L.geoJSON(data, {
pointToLayer: function (feature, latlng) {
const marker = L.marker(latlng, {icon: taxiIcon});
return marker;
}
}).addTo(markerGroup);
console.log(`Updated the map with ${data.features[0].properties.taxi_count} taxis at ${data.features[0].properties.timestamp}`);
});
}, 30000, true);
</script>
</body>
</html>