我想在 map 上画出与方向JSON相对应的路由,我正在通过Google directions API:https://developers.google.com/maps/documentation/directions/start获取这些路由

I have figured out how to extract the latitude and longitude from the steps field, however this doesn't follow curvy roads very well. I think what I need is to decode the polyline information, I found Googles instructions on how to encode polylines: https://developers.google.com/maps/documentation/utilities/polylinealgorithm

I did find some code here for Android and also Javascript on decoding the polylines, for example:

Map View draw directions using google Directions API - decoding polylines

android get and parse Google Directions

但我找不到同样的Objective-C iPhone代码,有人能帮我吗?如果有必要的话,我相信我可以自己做,但如果已经有地方可以使用的话,这肯定会节省我一些时间.

编辑:这里的关键是能够逐个字符地解码base64编码.更具体地说,我从谷歌的JSON中得到了类似的东西,它是使用base64编码进行编码的:

...   "overview_polyline" : {
        "points" : "ydelDz~vpN_@NO@QEKWIYIIO?YCS@WFGBEBICCAE?G@y@RKBEBEBAD?HTpB@LALALCNEJEFSP_@LyDv@aB\\GBMB"
       },
...

注意:我应该提一下这个问题是指Google Maps API v1,使用GMSPolyLine polyLineWithPath在v2中做这件事要容易得多,因为下面的许多答案都会告诉您(感谢@cdesCours).

推荐答案

I hope it's not against the rules to link to my own blog post if it's relevant to the question, but I've solved this problem in the past. Stand-alone answer from linked post:

@implementation MKPolyline (MKPolyline_EncodedString)

+ (MKPolyline *)polylineWithEncodedString:(NSString *)encodedString {
    const char *bytes = [encodedString UTF8String];
    NSUInteger length = [encodedString 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

Json相关问答推荐

在Reaction中从JSON文件中筛选数组

JQ-JSON将键转换为对象

错误:在 NX 工作区中找不到模块../swagger.json

错误解析错误:意外令牌:在我的 .eslintrc.json 文件中.为什么?

使用 JQ 获取 JSON 中的替代元素(输出:JSON 对象)

如何使用 jq 将字符串数组转换为对象?

打印与 JSON 和 PowerShell 中的模式匹配的子项的父项名称

将 JSON 文件放在哪里以在 Angular 8 应用程序中加载静态 JSON 数据?

如何在 Android Studio 中将 List 字段作为空列表[]返回?

坚持弄清楚如何使用 api 响应来调用以从不同的链接检索响应

自定义将 struct 解组为切片映射

在PowerShell中按时间戳过滤JSON

如何使用法语口音对数组进行 json_encode?

Rails:format.js 或 format.json,或两者兼而有之?

使用非美国日期格式时,JsonConvert.DeserializeObject 无法将字符串转换为 DateTime

Python Flask-Restful POST 不采用 JSON 参数

json和空数组

字符串的 Gson 数组到 JsonArray

为不同类型的项目数组正确的 JSON Schema

as_json 没有在关联上调用 as_json