【问题标题】:Map app not working地图应用程序不工作
【发布时间】:2013-11-09 20:55:36
【问题描述】:

看看下面的代码,它工作正常

NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps?daddr=%f,%f&saddr=%f,%f", 23.0300, 72.5800, 22.3000, 70.7833];    
NSString *escapedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];    
NSURL *url=[NSURL URLWithString:escapedString];
[[UIApplication sharedApplication]openURL:url];

但它会在 Safari 浏览器中打开地图。

之后我尝试了下面的代码

NSString *urlString = [NSString stringWithFormat:@"http://maps.apple.com/maps?daddr=%f,%f&saddr=%f,%f", 23.0300, 72.5800, 22.3000, 70.7833];    
NSString *escapedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];    
NSURL *url=[NSURL URLWithString:escapedString];
[[UIApplication sharedApplication]openURL:url];

它根据我的需要打开地图应用程序,但没有给我路线的方向结果或上面谷歌地图给出的相同结果我如何才能在地图应用程序中实现第一个代码的结果?

请帮忙!!

【问题讨论】:

    标签: ios iphone google-maps map mapkit


    【解决方案1】:

    Apple 的地图没有与 google maps 相同的 API,因此您的 URL 将不起作用。苹果在 iOS 6 中引入了MKMapItem,它允许开发人员与 maps.app 进行交互。

    如果您想继续通过 http 使用地图,那么您应该更改您的网址:

    NSString *urlString = [NSString stringWithFormat:@"http://maps.apple.com/?daddr=%f,%f&saddr=%f,%f", 23.0300, 72.5800, 22.3000, 70.7833];    
    NSString *escapedString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];    
    NSURL *url=[NSURL URLWithString:escapedString];
    [[UIApplication sharedApplication]openURL:url];
    

    Apple URL Scheme Reference 中所述,您不应添加/maps/ 路径。

    不包含路径参数或包含特定地图的网址 路径在 Safari 中打开并显示在那里。例如,网址 基于路径http://maps.apple.com/http://maps.apple.com/mapshttp://maps.apple.com/localhttp://maps.apple.com/m 都是 在 Safari 中打开。要在地图应用程序中打开 URL,路径必须是 表格http://maps.apple.com/?q

    创建有效地图链接的规则如下:

    • 域必须是 maps.apple.com。
    • 路径不能是 /maps/*。
    • 如果值为 URL,则参数不能为 q=*(因此不会提取 KML)。
    • 参数不能包含 view=text 或 dirflg=r

    【讨论】:

    • 仍然无法像谷歌地图使用您的代码那样定位路线。
    • 那是因为苹果地图的工作方式与谷歌地图不同。您将无法进行直接转换。我建议您使用MKMapItem,但它需要地址和坐标。此外,正如您在 Apple 文档中看到的那样,daddrsaddr 需要地址而不是位置。
    • 好的,所以我想最好在 safari 上打开谷歌地图,而不是在地图应用上打开苹果地图...哈哈:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-05
    • 2021-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多