IOS 在自己的app内唤起地图APP

选择背景:由于地图的SDK一般较大,在app内部实现线路规划、导航等功能比较麻烦,而且耗时,所以就选择在app内部唤起地图app(如高德地图、百度地图、腾讯地图、苹果地图等)以实现同样的功能。

效果见下图:

这里写图片描述
IMG_0330.PNG

代码如下:(具体会根据手机上安装的地图app来显示)

#pragma mark - 选择导航app
- (void)actionBtnClick {
    //当前的位置
    CLLocationCoordinate2D currentLoc = self.mapView.userLocation.location.coordinate;
    MKPlacemark *curPlacemark = [[MKPlacemark alloc] initWithCoordinate:currentLoc addressDictionary:nil];
    MKMapItem *curLocationItem = [[MKMapItem alloc] initWithPlacemark:curPlacemark];
    curLocationItem.name = @"当前位置";
    //目的地的位置
    CLLocationCoordinate2D toLocation;
    toLocation.longitude = _longi.floatValue;
    toLocation.latitude = _lati.floatValue;
    MKPlacemark *toPlacemark = [[MKPlacemark alloc] initWithCoordinate:toLocation addressDictionary:nil];
    MKMapItem *toLocationItem = [[MKMapItem alloc] initWithPlacemark:toPlacemark];
    toLocationItem.name = _shopName;
    //选择要唤起的app
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
        UIAlertAction *daode = [UIAlertAction actionWithTitle:@"高德地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSString *urlString = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=ApplicationName&sid=BGVIS1&slat=%f&slon=%f&sname=当前位置&did=BGVIS2&dlat=%f&dlon=%f&dname=%@&dev=0&m=0&t=0", currentLoc.latitude, currentLoc.longitude, _lati.floatValue, _longi.floatValue, _shopName] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            [CGOpenUrlTool openUrl:[NSURL URLWithString:urlString]];
        }];
        [alert addAction:daode];
    }
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
        UIAlertAction *baidu = [UIAlertAction actionWithTitle:@"百度地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin=%f,%f&destination=latlng:%f,%f|name=%@&mode=driving&coord_type=gcj02",currentLoc.latitude, currentLoc.longitude, _lati.floatValue, _longi.floatValue, _shopName] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            [CGOpenUrlTool openUrl:[NSURL URLWithString:urlString]];
        }];
        [alert addAction:baidu];
    }
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]) {
        UIAlertAction *tencent = [UIAlertAction actionWithTitle:@"腾讯地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSString *urlString = [[NSString stringWithFormat:@"qqmap://map/routeplan?from=当前位置&fromcoord=%f,%f&type=drive&tocoord=%f,%f&to=%@&coord_type=1&policy=0", currentLoc.latitude, currentLoc.longitude, _lati.floatValue, _longi.floatValue, _shopName] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            [CGOpenUrlTool openUrl:[NSURL URLWithString:urlString]];
        }];
        [alert addAction:tencent];
    }
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"http://maps.apple.com/"]]) {
        UIAlertAction *apple = [UIAlertAction actionWithTitle:@"苹果地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            NSArray *items = @[curLocationItem,toLocationItem];
            NSDictionary *options = @{
                                  MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDefault,
                                  MKLaunchOptionsMapTypeKey : @(MKMapTypeStandard),
                                  MKLaunchOptionsShowsTrafficKey : @(YES)
                                  };

            //打开苹果自身地图应用,并呈现特定的item
            [MKMapItem openMapsWithItems:items launchOptions:options];
        }];
        [alert addAction:apple];
    }
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

    }];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
}

详细内容请到我的简书查看

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值