-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
147 lines (126 loc) · 3.61 KB
/
script.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
var map, polyline = [],
markers = [];
setTimeout(() => {
map = new MapmyIndia.Map("map", {
center: [12.8949688, 77.6735508],
zoomControl: true,
hybrid: true
});
}, 1000);
function randomColor() {
let color = "#",
charset = "0123456789ABCDEF";
for (let i = 1; i <= 6; i++)
color += charset[Math.floor(Math.random() * 16)];
return color;
}
let form = document.forms[0];
form.addEventListener('submit', ev => {
ev.preventDefault();
let posA = form.posA.value,
posB = form.posB.value;
posA = posA.split(", ");
posB = posB.split(", ");
let points = [{
x: 0,
lat: parseFloat(posA[0]),
lng: parseFloat(posA[1])
},
{
x: 1,
lat: parseFloat(posB[0]),
lng: parseFloat(posB[1])
}
]
points.forEach(pt => {
addMarker(pt, `Point ${(pt.x == 0)? "A": "B"}`)
})
let output = document.querySelector('#output > b')
let intPts = [];
let d1 = new Date().getTime();
for (let i = 0; i <= 1; i += 0.01)
intPts.push(lagrangeInterpolation(points, parseFloat(i.toFixed(2))))
/* let ctr=0;
intPts.forEach(pt => {
addMarker(pt, `Point ${ctr++}`)
}); */
createPolyline(intPts, "Interpolated Polyline")
getDirections(points)
let d2 = new Date().getTime();
output.innerText = `${d2-d1}ms`
form.posA.value = "";
form.posB.value = "";
return false;
})
function addMarker(data, content) {
let marker = L.marker([data.lat, data.lng]).addTo(map);
marker.bindPopup(content);
markers.push(marker)
}
function createPolyline(pts, content) {
var poly = new L.Polyline(pts, {
weight: 4, // The thickness of the polyline
opacity: 0.75, //The opacity of the polyline colour ,
color: randomColor()
})
poly.bindPopup(content)
polyline.push(poly);
map.addLayer(poly);
}
async function getDirections(points) {
try {
let res = await fetch(`https://mapmyindiaapi.nishithp2004.repl.co/api/?points=${points.map(pt => pt.lng + "," + pt.lat).join(";")}`, {
method: "GET"
})
.then(r => r.json())
.catch(err => {
if (err)
console.error(err)
})
let geometry = decode(res.routes[0].geometry)
createPolyline(geometry, "Actual Polyline")
} catch (err) {
if (err) console.error(err)
}
}
function resetMap() {
form.posA.value = "";
form.posB.value = "";
document.querySelector('#output > b').innerText = ""
polyline.forEach(poly => {
map.removeLayer(poly)
})
markers.forEach(marker => {
map.removeLayer(marker)
})
}
document.getElementById("reset").onclick = resetMap;
function decode(encoded) {
var points = [],
index = 0,
len = encoded.length,
lat = 0,
lng = 0;
while (index < len) {
var b, shift = 0,
result = 0;
do {
b = encoded.charAt(index++).charCodeAt(0) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
var dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = encoded.charAt(index++).charCodeAt(0) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
var dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
points.push([lat / 1E5, lng / 1E5])
}
return points
}