-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathGeo3x3.dart
59 lines (58 loc) · 1.1 KB
/
Geo3x3.dart
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
class Geo3x3 {
static String encode(num lat, num lng, num level) {
if (level < 1) {
return "";
}
var res = "";
var lng2 = lng;
if (lng >= 0) {
res = "E";
} else {
res = "W";
lng2 += 180;
}
var lat2 = lat + 90.0;
var unit = 180.0;
for (int i = 1; i < level; i++) {
unit /= 3;
final x = (lng2 / unit).toInt();
final y = (lat2 / unit).toInt();
res += (x + y * 3 + 1).toString();
lng2 -= x * unit;
lat2 -= y * unit;
}
return res;
}
static List decode(String code) {
var unit = 180.0;
var lat = 0.0;
var lng = 0.0;
var level = 1;
var begin = 0;
var flg = false;
var c = code[0];
if (c == '-' || c == 'W') {
flg = true;
begin = 1;
} else if (c == '+' || c == 'E') {
begin = 1;
}
final clen = code.length;
for (int i = begin; i < clen; i++) {
var n = "0123456789".indexOf(code[i]);
if (n <= 0)
break;
unit /= 3;
n--;
lng += n % 3 * unit;
lat += (n / 3).toInt() * unit;
level++;
}
lat += unit / 2;
lng += unit / 2;
lat -= 90.0;
if (flg)
lng -= 180.0;
return [lat, lng, level.toDouble(), unit];
}
}