栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在iPhone的Objective-C中将Google Directions API折线字段解码为经纬度的点?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在iPhone的Objective-C中将Google Directions API折线字段解码为经纬度的点?

我希望与该问题相关的链接到我自己的博客文章的规则不违反规则,但是我过去已经解决了这个问题。来自链接帖子的独立答案:

@implementation MKPolyline (MKPolyline_EnpredString)+ (MKPolyline *)polylineWithEnpredString:(NSString *)enpredString {    const char *bytes = [enpredString UTF8String];    NSUInteger length = [enpredString 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;        }    }    MKPolyline *polyline = [MKPolyline polylineWithCoordinates:coords count:coordIdx];    free(coords);    return polyline;}@end


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/449221.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号