-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
181 lines (136 loc) · 4.63 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<!DOCTYPE html>
<html>
<head>
<title>Safe Cycle London App</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" type="image/x-icon" href="docs/images/favicon.ico" />
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A==" crossorigin=""></link>
<script src="https://unpkg.com/[email protected]/dist/leaflet.js" integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA==" crossorigin=""> </script>
<script src="./L.KML.js"></script>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet-routing-machine.css" />
<script src="https://unpkg.com/[email protected]/dist/leaflet-routing-machine.js"></script>
<style>
html, body {
height: 100%;
margin: 0;
}
#map {
width: 600px;
height: 400px;
}
/* path{stroke-width:1.5px!important} */
</style>
<style>body { padding: 0; margin: 0; } #map { height: 100%; width: 100vw; }</style>
</head>
<body>
<div id='map'></div>
<script>
var map = L.map("map"); //.fitWorld();
var latLon = L.latLng(51.5074, 0.1278);
var bounds = latLon.toBounds(20000); // 500 = metres
map.panTo(latLon).fitBounds(bounds);
var start, way;
L.tileLayer("https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}{r}.png", {
attribution:
'<a href="https://www.google.com/maps/d/u/0/viewer?hl=en&mid=1XlpvN9R-Wg7qZHyezO8y-eVlftr4e0WX&ll=51.516975804561284%2C-0.21828576419061996&z=10">SafeCycle</a> © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors © <a href="https://carto.com/attributions">CARTO</a>',
subdomains: "abcd",
maxZoom: 18,
opacity: 0.9,
id: "mapbox",
tileSize: 512,
zoomOffset: -1
}).addTo(map);
function truncateString(str, num) {
if (str.length <= num) {
return str
}
return str.slice(0, num)
}
function onLocationFound(e) {
var radius = e.accuracy / 2;
var greenIcon = new L.Icon({
iconUrl: 'https://raw.githubusercontent.com/pointhi/leaflet-color-markers/master/img/marker-icon-2x-green.png',
shadowUrl: 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.7/images/marker-shadow.png',
iconSize: [25, 41],
iconAnchor: [12, 41],
popupAnchor: [1, -34],
shadowSize: [41, 41]
});
var marker = L.marker(e.latlng,{icon: greenIcon})
.addTo(map)
.bindPopup("Accurate to ±" + truncateString(''+radius,4) + "m")
.openPopup();
L.circle(e.latlng, radius).addTo(map);
}
function onError(e) {
console.log("no gps, using IP ");
//
// fetch("https://ipapi.co/json/")
// .then(function(response) {
// console.log(response);
// return response.json();
// })
// .then(function(data) {
// console.log(data);
// start = L.latLng([data.latitude, data.longitude]);
// console.log("using", start);
// });
}
function getlocation() {
map.on("locationfound", onLocationFound);
map.on("locationerror", e => {
console.log("no geo");
});
map.locate({ setView: true, maxZoom: 16 });
}
fetch("Safe cycling in London.kml")
.then(res => res.text())
.then(kmltext => {
// Create new kml overlay
const parser = new DOMParser();
const kml = parser.parseFromString(kmltext, "text/xml");
const track = new L.KML(kml);
window.k = [kml, track];
map.addLayer(track);
[...document.querySelectorAll('path')].forEach(d=>d.style['stroke-width']='1.5px')
// Adjust map to show the kml
const bounds = track.getBounds();
map.fitBounds(bounds);
})
.then(getlocation)
.then(routing);
function routing() {
var locs = window.location.hash.split("#").filter(d => d != "");
console.log(locs, locs.length);
if (locs.length > 1) {
// #51.5014,-0.1419#51.4934,-0.0098
way = locs.map(d => {
var q = d.split(",").map(e => +e);
return L.latLng(q[0], q[1]);
});
console.log(way);
window.z = L.Routing.control({
waypoints: way,
routeWhileDragging: true
});
z.on("routesfound", function(e) {
try {
// document.querySelector('path[stroke="white"]').style['stroke-width']='8px'
document.querySelector(
".leaflet-routing-alt h3"
).textContent = document
.querySelector(".leaflet-routing-alt h3")
.textContent.split(",")[0];
} catch (r) {}
window.p = e.waypoints;
console.log(e);
window.location.hash =
"#" + p.map(d => d.latLng.lat + "," + d.latLng.lng).join("#");
});
z.addTo(map);
}
}
</script>
</body>
</html>