-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
98 lines (77 loc) · 1.79 KB
/
index.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
const SAFE_CHARACTERS =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";
const MULTIPLIER = 100000;
const DIVIDER = 32;
function encodePoints(points) {
var result = "";
var latitude = 0;
var longitude = 0;
for (var i in points) {
var newLat = Math.round(points[i][0] * MULTIPLIER);
var newLon = Math.round(points[i][1] * MULTIPLIER);
var dy = newLat - latitude;
var dx = newLon - longitude;
latitude = newLat;
longitude = newLon;
dy = (dy << 1) ^ (dy >> 31);
dx = (dx << 1) ^ (dx >> 31);
var index = ((dy + dx) * (dy + dx + 1)) / 2 + dy;
while (index > 0) {
var rem = index & 31;
index = (index - rem) / DIVIDER;
if (index > 0) {
rem += DIVIDER;
}
result += SAFE_CHARACTERS[rem];
}
}
return result;
}
function decodePoints(compressedValue) {
var latLon = [];
var pointsArray = [];
var point = [];
var lastLat = 0,
lastLon = 0;
for (var i = 0; i < compressedValue.length; i++) {
var num = SAFE_CHARACTERS.indexOf(compressedValue[i]);
if (num < DIVIDER) {
point.push(num);
pointsArray.push(point);
point = [];
} else {
num -= DIVIDER;
point.push(num);
}
}
for (var y in pointsArray) {
var result = 0;
var list = pointsArray[y].reverse();
for (var x in list) {
if (result == 0) {
result = list[x];
} else {
result = result * DIVIDER + list[x];
}
}
var dIag = parseInt((Math.sqrt(8 * result + 5) - 1) / 2);
var latY = result - (dIag * (dIag + 1)) / 2;
var lonX = dIag - latY;
if (latY % 2 == 1) {
latY = (latY + 1) * -1;
}
if (lonX % 2 == 1) {
lonX = (lonX + 1) * -1;
}
latY /= 2;
lonX /= 2;
var lat = latY + lastLat;
var lon = lonX + lastLon;
lastLat = lat;
lastLon = lon;
lat /= MULTIPLIER;
lon /= MULTIPLIER;
latLon.push(lat, lon);
}
return latLon;
}