iOS 字符串转Polyline数组

//一段行程通过字符串保存下来,通常是一堆乱码的字符串.
例如 :
NSString *path = @"woo}DuixdV??A@???A??A???A?@???AA?@@??AFE????????????????????????A?@?l@DA??A?@A??A?@????A?AA?@A????AdAB?A?@???@AAA??BADADADAFAJCHAJeBCJALCJCN?JALAN?L?N?N?HAPAN?RQL?R?T?TAV?X?V?X?Z?Z?TAZ?X?Z?XCV?V?T?V?R@X?X?X?X@X?T?X?T?T@TIR?VAZ?X?TAX@TAX?XAV?T?R?T?VATDX@R?R@T?T?R?R?P?R?R?L@NAR@NAPCR?R?R?N?RARAN?P?J?NAL?L?PAN?TBZ?T?R@N?R?V?P?R?R?T?V?XAV?V?TCP?P?NAN?H?J?H?L?J?L?N@J@H?F@B@F?@?B@D@F@H@D@F??@DB@@D@FBHDFJVFNDPFPDNFPFRFPDTFPFNFTBTDVDTHVBRDTBT@VBRBRBP@N@P@F?F@B@F?BIJ@F?D?B?@?@???A?@?A?@???A????A???????????A???@?A?????????A?AJ@@??AA??@B?F?H@F?H@L?J@J@L?N@b@@HBNBHHHHDNDR@VDXDXDZJZHZJZJf@TVHRLPJLFNHNJPJPJPLPJLHLHLHLHFRJHHHJFFFFDBBBDBB@D?FAJAJCLCLWNCD?B?C@B@B@B?@???????A?@???AZZ?@@A??A??????@?A@A?@??A?@AA@???A@@?A???????????@?A?@?A@@?A???A?@?A@@???????????A???@?A@@???@@?A???AA?@????AA?@????A?@?";

我们在拿到这一长串字符串之后需要解析成一个数组,这里需要一个复杂的算法去解析
- (NSArray *)decodePolyline:(NSString *)path{
    NSMutableArray *array = [[NSMutableArray alloc] init];
    const char *bytes = [path UTF8String];
    NSUInteger length = [path lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
    NSUInteger idx = 0;   
    NSUInteger count = length / 4;
    CLLocationCoordinate2D *coords = calloc(count, sizeof(CLLocationCoordinate2D));
    NSUInteger coordIdx = 0;
    float latitude = 0;
    float longitude = 0;
    while (idx < length) {
        char byte = 0;
        int res = 0;
        char shift = 0;
        
        do {
            byte = bytes[idx++] - 63;
            res |= (byte & 0x1F) << shift;
            shift += 5;
        } while (byte >= 0x20);
        
        float deltaLat = ((res & 1) ? ~(res >> 1) : (res >> 1));
        latitude += deltaLat;
        
        shift = 0;
        res = 0;
        
        do {
            byte = bytes[idx++] - 0x3F;
            res |= (byte & 0x1F) << shift;
            shift += 5;
        } while (byte >= 0x20);
        
        float deltaLon = ((res & 1) ? ~(res >> 1) : (res >> 1));
        longitude += deltaLon;
        
        float finalLat = latitude * 1E-5;
        float finalLon = longitude * 1E-5;
        
        CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(finalLat, finalLon);
        coords[coordIdx++] = coord;
        
        if (coordIdx == count) {
            NSUInteger newCount = count + 10;
            coords = realloc(coords, newCount * sizeof(CLLocationCoordinate2D));
            count = newCount;
        }
        
        CLLocation *loc = [[CLLocation alloc] initWithLatitude:finalLat
                                                     longitude:finalLon];
        [array addObject:loc];
    }
    
    return array;
}

//解析出来的结果如下
**2016-02-05 16:21:27.928 ****字符串转Polyline数组****[3707:51509] 31.214199,121.535149**
**2016-02-05 16:21:27.929 ****字符串转Polyline数组****[3707:51509] 31.214199,121.535149**
**2016-02-05 16:21:27.929 ****字符串转Polyline数组****[3707:51509] 31.214211,121.535141**
**2016-02-05 16:21:27.929 ****字符串转Polyline数组****[3707:51509] 31.214211,121.535141**
**2016-02-05 16:21:27.929 ****字符串转Polyline数组****[3707:51509] 31.214211,121.535149**
**2016-02-05 16:21:27.929 ****字符串转Polyline数组****[3707:51509] 31.214211,121.535149**
.
.
.
这段字符串解析出来的数组总共有389个元素,都是经纬度.
这些坐标唯一的用途是绘制一段行程

你可能感兴趣的:(iOS 字符串转Polyline数组)