【问题标题】:MapKit - Make route line follow streets when map zoomed inMapKit - 地图放大时使路线跟随街道
【发布时间】:2012-10-12 09:46:00
【问题描述】:

我编写了一个简单的应用程序,它在 MapKit 上的两个位置之间绘制路线。我正在使用谷歌地图 API。我使用了在网上找到的资源,这是我用来向 Google 发出请求的代码:

_httpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://maps.googleapis.com/"]];
    [_httpClient registerHTTPOperationClass: [AFJSONRequestOperation class]];
    [_httpClient setDefaultHeader:@"Accept" value:@"application/json"];

    NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
    [parameters setObject:[NSString stringWithFormat:@"%f,%f", coordinate.latitude, coordinate.longitude] forKey:@"origin"];
    [parameters setObject:[NSString stringWithFormat:@"%f,%f", endCoordinate.latitude, endCoordinate.longitude] forKey:@"destination"];
    [parameters setObject:@"true" forKey:@"sensor"];

    NSMutableURLRequest *request = [_httpClient requestWithMethod:@"GET" path: @"maps/api/directions/json" parameters:parameters];
    request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];    
    [operation  setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){
        NSInteger statusCode = operation.response.statusCode;

        if (statusCode == 200)
        {
            NSLog(@"Success: %@", operation.responseString);
        }
        else
        {
            NSLog(@"Status code = %d", statusCode);
        }
    }
                                      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                          NSLog(@"Error: %@",  operation.responseString);

                                      }
     ];

    [_httpClient enqueueHTTPRequestOperation:operation];

这完美无缺。当我运行它并尝试显示洛杉矶和芝加哥之间的路线时,它是这样的:

但是。当我将地图缩放到街道级别时,路线如下所示:

有谁知道我如何在地图缩放时实现我正在绘制的沿着街道的路线?我想路线显示穿过街道的确切路径。我不知道是否需要在我对 Google 的请求中添加一些额外的参数。

任何帮助或建议都会很棒。非常感谢!


[编辑 #1:添加来自 Google 的请求 URL 和响应]

从上面的代码创建操作对象后我的请求 URL 如下所示:

http://maps.googleapis.com/maps/api/directions/json?sensor=true&destination=34%2E052360,-118%2E243560&origin=41%2E903630,-87%2E629790

只需将该 URL 粘贴到您的浏览器,您就会看到 Google 发送的 JSON 数据作为我在代码中得到的响应。


[编辑 #2:解析来自 Google 的答案并构建路径]

- (void)parseResponse:(NSData *)response
{
    NSDictionary *dictResponse = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableContainers error:nil];
    NSArray *routes = [dictResponse objectForKey:@"routes"];
    NSDictionary *route = [routes lastObject];

    if (route)
    {
        NSString *overviewPolyline = [[route objectForKey: @"overview_polyline"] objectForKey:@"points"];
        _path = [self decodePolyLine:overviewPolyline];
    }
}

- (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];

        CLLocation *location = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]];
        [array addObject:location];
    }

    return array;
}

【问题讨论】:

标签: iphone objective-c ios google-maps-api-3 mapkit


【解决方案1】:

Google 似乎没有给你所有的分数,或者你没有查看所有的分数。实际上,我希望地标之间有折线,而不仅仅是您似乎有的地标(直线)。

  • 检查响应中的 DirectionsStatus 以查看您是否受限
  • 提供 Google 发回的 json 数据。

我不太确定他们使用的墨卡托投影与 Google 使用的投影完全不同。

【讨论】:

  • 谢谢你的回答,西里尔。检查我的问题中的编辑#1,我提供了您在那里询问的数据。
  • 在编辑 #2 中,我添加了解析来自 Google 的答案并构建路径的源代码。我现在尝试将模式从 DRIVING 更改为 WALKING,有趣的是,尽管来自 Google 的 JSON 响应显然比在 DRIVING 模式下大得多,但我最终仍然得到了 205 个坐标用于在路径中行走,而对于 DRIVING,我得到了 203 个坐标小路。奇怪。
  • 很遗憾,我对路线 API 的了解不足,无法进一步帮助您。我认为一个关键元素是文档中的这句话:“overview_polyline 包含一个对象,该对象包含一个编码点数组,这些编码点表示结果方向的近似(平滑)路径。”尝试使用较短的路线进行相同的请求,看看它是否更精确,或者更好,尝试像纽约或洛杉矶这样的真正方形路线,看看您的路线是否会随着您请求更近的路线而变得更好。
  • 是的,西里尔,我试过这个,但在我的编辑之一中忘记提及它。当我想显示洛杉矶和芝加哥之间的路线时,它看起来就像我的问题中提到的那样。但是当我将 LA 坐标作为原点并将 LA 坐标(经纬度)+ 0.3f 作为目标点时,路线沿着 LA 的街道非常好。因此,当我尝试在两个非常远的点之间绘制路线时,看起来这些街道点在 Google 响应中被简单地忽略了。
  • 嗨@uerceg,您找到问题的解决方案了吗?我目前遇到同样的问题。任何帮助将不胜感激。提前致谢!
【解决方案2】:

我相信 MapKit 使用的投影与 Google Maps 使用的投影不同。 MapKit uses Cylindrical Mercator,而Google uses a variant of the Mercator Projection。

坐标系之间的转换 虽然您通常指定 使用纬度和经度值在地图上的点,可能有 需要在其他坐标系之间进行转换的时间。 例如,您通常在指定形状时使用地图点 覆盖。

引用自Apple:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-12
    • 2019-11-06
    • 1970-01-01
    相关资源
    最近更新 更多