iOS 应用内调用第三方地图导航功能

最近公司的应用要添加这个功能,在百度上、GitHub上、以及其他的技术网站上找了好多资料,都没有达到自己想要的效果,先是苹果地图导航,找到的类似Demo可以调取成功,但是把Demo中的文件拖到应用中就不行了,每次执行到

[MKMapItem openMapsWithItems:items launchOptions:dic];

这句代码都会崩溃,找了好多资料都没解决。
最后功夫不负有心人,直接上代码:

  //苹果原生地图  strLatitude、strLongitude:开始位置的经纬度 direLatitude、direLongitude 目的地位置经纬度  address目的地地址
  NSURL * apple_App = [NSURL URLWithString:@"http://maps.apple.com/"];
  
 if ([[UIApplication sharedApplication] canOpenURL:apple_App]) {
        
        NSString *urlString=[NSString stringWithFormat:@"http://maps.apple.com/?saddr=%f,%f&daddr=%f,%f",strLatitude,strLongitude,direLatitude,direLongitude ];
        CLLocationCoordinate2D from = CLLocationCoordinate2DMakestrLatitude,strLongitude);
        MKMapItem *currentLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:from addressDictionary:nil]];
        currentLocation.name = @"我的位置";
        
        CLLocationCoordinate2D to = CLLocationCoordinate2DMakedireLatitude,direLongitude);
        MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:to addressDictionary:nil]];
        toLocation.name = address;
       
    }

当然百度和高德也不是一下就完美的,虽然能成功调取地图导航,高德的问题是没有路径规划,直接开始导航,百度的则是地图首页终点的位置没有显示传过来的地址,当然这最后都解决了,主要还是自己坚持的结果,不断的查找相关资料,再结合一番总结学习。
直接上代码:

//百度地图
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
    
        NSString *urlString =[[NSString stringWithFormat:@"baidumap://map/direction?origin=latlng:%f,%f|name:%@&destination=latlng:%f,%f|name:%@&mode=driving&coord_type=gcj02&output=html",strLatitude,strLongitude,@"我的位置",direLatitude,direLongitude,address] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
    }

    //高德地图
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
    
        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",strLatitude,strLongitude,@"我的位置",direLatitude,direLongitude,address] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值