-
Notifications
You must be signed in to change notification settings - Fork 16
/
Geo3x3.mm
74 lines (73 loc) · 1.94 KB
/
Geo3x3.mm
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
#import <Foundation/Foundation.h>
#import "Geo3x3.objc.h"
@implementation Geo3x3
+ (NSString*)Encode:(double)lat lng:(double)lng level:(int)level {
if (level < 1) {
return @"";
}
NSString* res = nil;
if (lng >= 0) {
res = @"E";
} else {
res = @"W";
lng += 180.0;
}
lat += 90.0;
double unit = 180.0;
for (int i = 1; i < level; i++) {
unit /= 3.0;
const int x = (int)(lng / unit);
const int y = (int)(lat / unit);
const int n = x + y * 3 + 1;
NSString* c = [[NSNumber numberWithInt:n] stringValue];
res = [res stringByAppendingString:c];
lng -= x * unit;
lat -= y * unit;
}
return res;
}
+ (NSArray*)Decode:(NSString*)code {
const int clen = [code length];
if (code == nil || clen == 0) {
return nil;
}
NSString* c = [code substringWithRange:NSMakeRange(0, 1)];
bool flg = false;
int begin = 0;
if ([c isEqualToString:@"W"] || [c isEqualToString:@"-"]) {
flg = true;
begin = 1;
} else if ([c isEqualToString:@"E"] || [c isEqualToString:@"+"]) {
begin = 1;
}
double unit = 180.0;
double lat = 0.0;
double lng = 0.0;
int level = 1;
for (int i = begin; i < clen; i++) {
NSString* c = [code substringWithRange:NSMakeRange(i, 1)];
const NSRange range = [@"123456789" rangeOfString:c];
if (range.location == NSNotFound) {
break;
}
const int n = range.location;
unit /= 3;
lng += (double)(n % 3) * unit;
lat += (double)(n / 3) * unit;
level++;
}
lat += unit / 2;
lng += unit / 2;
lat -= 90.0;
if (flg) {
lng -= 180.0;
}
return [NSArray arrayWithObjects:
[NSNumber numberWithFloat:lat],
[NSNumber numberWithFloat:lng],
[NSNumber numberWithFloat:level],
[NSNumber numberWithFloat:unit],
nil
];
}
@end