-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathGHUtil.js
94 lines (82 loc) · 2.49 KB
/
GHUtil.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
let GHUtil = function () {
};
GHUtil.prototype.clone = function (obj) {
let newObj = {};
for (let prop in obj) {
if (obj.hasOwnProperty(prop)) {
newObj[prop] = obj[prop];
}
}
return newObj;
};
GHUtil.prototype.decodePath = function (encoded, is3D) {
let len = encoded.length;
let index = 0;
let array = [];
let lat = 0;
let lng = 0;
let ele = 0;
while (index < len) {
let b;
let shift = 0;
let result = 0;
do {
b = encoded.charCodeAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
let deltaLat = ((result & 1) ? ~(result >> 1) : (result >> 1));
lat += deltaLat;
shift = 0;
result = 0;
do {
b = encoded.charCodeAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
let deltaLon = ((result & 1) ? ~(result >> 1) : (result >> 1));
lng += deltaLon;
if (is3D) {
// elevation
shift = 0;
result = 0;
do {
b = encoded.charCodeAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
let deltaEle = ((result & 1) ? ~(result >> 1) : (result >> 1));
ele += deltaEle;
array.push([lng * 1e-5, lat * 1e-5, ele / 100]);
} else
array.push([lng * 1e-5, lat * 1e-5]);
}
// let end = new Date().getTime();
// console.log("decoded " + len + " coordinates in " + ((end - start) / 1000) + "s");
return array;
};
GHUtil.prototype.extractError = function (res, url) {
let msg;
if (res && res.data) {
msg = res.data;
if (msg.hints && msg.hints[0] && msg.hints[0].message)
msg = msg.hints[0].message;
else if (msg.message)
msg = msg.message;
} else {
msg = res;
}
return new Error(msg + " - for url " + url);
};
GHUtil.prototype.isArray = function (value) {
let stringValue = Object.prototype.toString.call(value);
return (stringValue.toLowerCase() === "[object array]");
};
GHUtil.prototype.isObject = function (value) {
let stringValue = Object.prototype.toString.call(value);
return (stringValue.toLowerCase() === "[object object]");
};
GHUtil.prototype.isString = function (value) {
return (typeof value === 'string');
};
module.exports = GHUtil;