ios url地图调用 调用三方地址看我就行

花了半天时间研究oc中用url调用地图的方法,api文档找的真心累,费话不多说了,直接上代码:
demo:https://pan.baidu.com/s/1B2c8eCUdEw-DdMZUzT03-w

实现的效果

你以为看个demo就行,还是按心看下去吧
在oc中url调用oc地图,主要分为二步,1,白名单的添加 2,代码调用

先贴下各三方地图的api文档和Scheme

– 地图 – scheme – 文档地址 –

  1. 苹果地图 无 https://developer.apple.com/library/archive/featuredarticles/iPhoneURLScheme_Reference/MapLinks/MapLinks.html

  2. 百度地图 baidumap:// http://lbsyun.baidu.com/index.php?title=uri/api/ios

  3. 高德地图 iosamap:// https://lbs.amap.com/api/amap-mobile/guide/ios/navi/

4.谷歌地图 comgooglemaps:// https://developers.google.com/maps/documentation/ios-sdk/urlscheme

5.腾讯地图: qqmap://
http://lbs.qq.com/uri_v1/guide-route.html

首先去info.plist文件中添加白名单,如果 不知道怎么添加,去打开高德地图文档 看下,很详细,

添加好白名单后,接着就是上代码,

    __block NSString *urlScheme = self.urlScheme;
    __block NSString *appName = self.appName;
    __block CLLocationCoordinate2D coordinate = self.coordinate;

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"选择地图" message:nil preferredStyle:UIAlertControllerStyleActionSheet];

    //这个判断其实是不需要的
    if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"http://maps.apple.com/"]])
    {
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"苹果地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

            MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
            MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil]];

            [MKMapItem openMapsWithItems:@[currentLocation, toLocation]
                           launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];
        }];

        [alert addAction:action];
    }

    if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]])
    {
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"百度地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

            NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=目的地&mode=driving&coord_type=gcj02",coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

            NSLog(@"%@",urlString);

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

        }];

        [alert addAction:action];
    }


    if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]])
    {
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"高德地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

            NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%f&lon=%f&dev=0&style=2",appName,urlScheme,coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

            NSLog(@"%@",urlString);

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

        }];

        [alert addAction:action];
    }

    if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]])
    {
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"谷歌地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

            NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving",appName,urlScheme,coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

            NSLog(@"%@",urlString);

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

        }];

        [alert addAction:action];
    }

    UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:action];

    [self presentViewController:alert animated:YES completion:nil];

到这,你觉的结束了吧,还有彩蛋,你中镖了吗,哈哈

首页想想模拟器是绝对不行的,但真机时发现也不行,有没有草蛋的感觉,哈哈,看一下控制台,有报错:
-canOpenURL: failed for URL: “comgooglemaps://” - error: “This app is not allowed to query for scheme XXX
一看原来是白名单添加的不够,好吧,那就把XXX再添加到白名单中去,直到没有XXX报错为止,

在添加apple 地图里有点要注意,记的导入

#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

不要说我没提醒你哦,记得 linked frameworks and libraries中把两个framework添加进去

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值