This repository was archived by the owner on Jun 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.js
More file actions
100 lines (80 loc) · 2.97 KB
/
route.js
File metadata and controls
100 lines (80 loc) · 2.97 KB
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
openrouteAccessToken = '5b3ce3597851110001cf6248ba1b7964630a48d9841d1336bd6686c7';
var startPoint = null;
function setupRoute() {
addRouteListener();
addGeocode();
}
/**
* long press implementation here is fairly manual
*/
function addRouteListener() {
map.on('contextmenu', fireRoute); //right click
let routeTimeout = null;
let clearRouteTimeout = () => clearTimeout(routeTimeout);
map.on('touchstart', (e) => {
if(e.originalEvent.touches.length > 1) {
return;
}
routeTimeout = setTimeout(() => {
window.navigator.vibrate(100);
setTimeout(() => fireRoute(e), 1);
}, 500);
});
map.on('touchend', clearRouteTimeout);
map.on('touchcancel', clearRouteTimeout);
map.on('touchmove', clearRouteTimeout);
map.on('pointerdrag', clearRouteTimeout);
map.on('pointermove', clearRouteTimeout);
map.on('moveend', clearRouteTimeout);
map.on('gesturestart', clearRouteTimeout);
map.on('gesturechange', clearRouteTimeout);
map.on('gestureend', clearRouteTimeout);
}
function fireRoute(e) {
if(openrouteAccessToken === null) {
return;
}
var point = e.lngLat;
addRoutePoint(point);
}
function addRoutePoint(point) {
if(startPoint === null) {
removeLayerButton('startPoint');
removeLayerButton('endPoint');
removeLayerButton('route');
startPoint = point;
var pointFeature = { type: 'Feature', geometry: { type: 'Point', coordinates: [point.lng, point.lat] }, properties: {'marker-symbol': 'marker', title: 'start'} };
addLayerHelper('startPoint', 'symbol', pointFeature);
return;
}
var endPoint = point;
var query = new URLSearchParams();
query.set('api_key', openrouteAccessToken);
query.set('start', reversedPointToString(startPoint));
query.set('end', reversedPointToString(endPoint));
var url = `https://api.openrouteservice.org/v2/directions/cycling-regular?${query}`;
ajaxGet(url, (data) => {
//sending in url directly here doesn't work because of somesuch header (Accept: application/json) made the server mad
addLayerHelper('route', 'line', data);
var summary = data.features[0].properties.summary;
var duration = (summary.duration / 3600).toFixed(1);
var distance = (summary.distance / 1000).toFixed(0);
var pointFeature = { type: 'Feature', geometry: { type: 'Point', coordinates: [point.lng, point.lat] }, properties: {'marker-symbol': 'marker', title: `end ${duration}h\n${distance}km`} };
addLayerHelper('endPoint', 'symbol', pointFeature);
});
startPoint = null;
}
function pointToString(point, accuracy = 6) {return `${point.lat.toFixed(accuracy)},${point.lng.toFixed(accuracy)}`;}
function reversedPointToString(point, accuracy = 6) {return `${point.lng.toFixed(accuracy)},${point.lat.toFixed(accuracy)}`;}
function addGeocode() {
map.addControl(new PeliasGeocoder({
params: {
'api_key': openrouteAccessToken,
},
marker: {icon: 'marker_11', anchor: 'bottom'},
flyTo: 'hybrid',
removeDuplicates: true,
url: 'https://api.openrouteservice.org/geocode',
useFocusPoint: true
}));
}