-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFT2GMapsHelper.m
63 lines (58 loc) · 2.14 KB
/
FT2GMapsHelper.m
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
//
// FT2GMapsHelper.m
// Mercedes Map Test
//
// Created by Baldoph Pourprix on 10/04/2013.
// Copyright (c) 2013 Fuerte Int. All rights reserved.
//
#import "FT2GMapsHelper.h"
#import "NSData+Coordinates.h"
@implementation FT2GMapsHelper
CLLocationCoordinate2D coordinatesForDictionary(NSDictionary *dict)
{
CLLocationDegrees latitude = [dict[@"lat"] doubleValue];
CLLocationDegrees longitude = [dict[@"lng"] doubleValue];
return CLLocationCoordinate2DMake(latitude, longitude);
}
+ (NSMutableArray *)decodePolyline:(NSString *)encodedStr
{
NSMutableString *encoded = [[NSMutableString alloc] initWithCapacity:[encodedStr length]];
[encoded appendString:encodedStr];
[encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\"
options:NSLiteralSearch
range:NSMakeRange(0, [encoded length])];
NSInteger len = [encoded length];
NSInteger index = 0;
NSMutableArray *array = [[NSMutableArray alloc] init];
NSInteger lat=0;
NSInteger lng=0;
while (index < len) {
NSInteger b;
NSInteger shift = 0;
NSInteger result = 0;
do {
b = [encoded characterAtIndex:index++] - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
NSInteger dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = [encoded characterAtIndex:index++] - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
lng += dlng;
NSNumber *latitude = [[NSNumber alloc] initWithFloat:lat * 1e-5];
NSNumber *longitude = [[NSNumber alloc] initWithFloat:lng * 1e-5];
// printf("[%f,", [latitude doubleValue]);
// printf("%f]", [longitude doubleValue]);
CLLocationCoordinate2D coordinates = CLLocationCoordinate2DMake(latitude.doubleValue, longitude.doubleValue);
[array addObject:[NSData dataWithCoordinates:coordinates]];
}
return array;
}
@end