美文网首页iOS技术
iOS实现跳转到其他地图导航

iOS实现跳转到其他地图导航

作者: Leeson1989 | 来源:发表于2017-11-27 14:48 被阅读19次

    这段时间在做导航,为了减少app内存开支,于是选择了以URI跳转的方式直接跳转到对应的地图进行导航,让最专业的人做最专业的事,也能满足需求,话不多说,进入正题

        我目前集成了几个国内常用的地图软件,“百度地图”,“高德地图”,“苹果自带地图”,“腾讯地图”,“谷歌地图”,如图,只需要点击对应的action就可以跳转到对应的地图了,当然前提是已经安装了对应的地图app。

    第一步,在info.plist文件中设置

    (加入:LSApplicationQueriesSchemes)如下图

    设置好之后,接下来就是代码了,没有做太多封装,直接简单明了的使用

    第二步,为了能够跳转回你的APP,需要在 URL Types里填写你的app的 URL Schemes

    第三步,创建一个类,集成自NSObject,在.h文件里导入头文件

    #import <Foundation/Foundation.h>

    #import <CoreLocation/CoreLocation.h>

    #import <MapKit/MKMapItem.h>//用于苹果自带地图

    #import <MapKit/MKTypes.h>//用于苹果自带地图

    接下来声明一个类方法方便外界调用

    /**

    *跳转到已经安装的地图

    *@param coord        目标位置

    *@param currentCoord 当前位置

    *@return sheetAction

    */

    +(UIAlertController *)getInstalledMapAppWithEndLocation:(CLLocationCoordinate2D)coord currentLocation:(CLLocationCoordinate2D)currentCoord;

    第四步,在.m文件里实现

    +(UIAlertController *)getInstalledMapAppWithEndLocation:(CLLocationCoordinate2D)coord currentLocation:(CLLocationCoordinate2D)currentCoord

    {

    //    调用地图

    UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:@"前往导航" preferredStyle:UIAlertControllerStyleActionSheet];

    //百度地图

    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://map/"]]) {

    UIAlertAction *baiduMapAction = [UIAlertAction actionWithTitle:@"百度地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

    NSString *baiduParameterFormat = @"baidumap://map/direction?origin=latlng:%f,%f|name:我的位置&destination=latlng:%f,%f|name:终点&mode=driving";

    NSString *urlString = [[NSString stringWithFormat:

    baiduParameterFormat,

    currentCoord.latitude,//用户当前的位置

    currentCoord.longitude,//用户当前的位置

    coord.latitude,

    coord.longitude]

    stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];

    }];

    [actionSheet addAction:baiduMapAction];

    }

    //高德地图

    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://map/"]]) {

    UIAlertAction *gaodeMapAction = [UIAlertAction actionWithTitle:@"高德地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

    NSString *gaodeParameterFormat = @"iosamap://navi?sourceApplication=%@&backScheme=%@&poiname=%@&lat=%f&lon=%f&dev=1&style=2";

    NSString *urlString = [[NSString stringWithFormat:

    gaodeParameterFormat,

    @“yourAppName”,

    @"yourUrlScheme",

    @"终点",

    coord.latitude,

    coord.longitude]

    stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];

    }];

    [actionSheet addAction:gaodeMapAction];

    }

    //苹果地图

    [actionSheet addAction:[UIAlertAction actionWithTitle:@"苹果地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

    //起点

    MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];

    CLLocationCoordinate2D desCorrdinate = CLLocationCoordinate2DMake(coord.latitude, coord.longitude);

    //终点

    MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:desCorrdinate addressDictionary:nil]];

    //默认驾车

    [MKMapItem openMapsWithItems:@[currentLocation, toLocation]

    launchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,

    MKLaunchOptionsMapTypeKey:[NSNumber numberWithInteger:MKMapTypeStandard],

    MKLaunchOptionsShowsTrafficKey:[NSNumber numberWithBool:YES]}];

    }]];

    //腾讯地图

    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://map/"]]) {

    [actionSheet addAction:[UIAlertAction actionWithTitle:@"腾讯地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

    NSString *QQParameterFormat = @"qqmap://map/routeplan?type=drive&fromcoord=%f, %f&tocoord=%f,%f&coord_type=1&policy=0&refer=%@";

    NSString *urlString = [[NSString stringWithFormat:

    QQParameterFormat,

    currentCoord.latitude,//用户当前的位置

    currentCoord.longitude,//用户当前的位置

    coord.latitude,

    coord.longitude,

    @“yourAppName”]

    stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];

    }]];

    }

    //谷歌地图

    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://map/"]]) {

    [actionSheet addAction:[UIAlertAction actionWithTitle:@"谷歌地图"style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

    NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving",

    @“yourAppName”,

    @"yourUrlScheme",

    coord.latitude,

    coord.longitude]

    stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];

    }]];

    }

    //取消按钮

    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

    [actionSheet dismissViewControllerAnimated:YES completion:nil];

    }];

    [actionSheet addAction:action3];

    return actionSheet;

    }

    至此,方法已经实现完成,你只需要在需要的地方调用就可以了,使用时要注意各种经纬度之间的转换,确保定位位置准确,关于经纬度转换的详细方法,请查看我的另一个帖子《经纬度转换封装

    有遗漏的地方还请在评论里帮忙提醒补充,谢谢

    相关文章

      网友评论

        本文标题:iOS实现跳转到其他地图导航

        本文链接:https://www.haomeiwen.com/subject/qzfdbxtx.html