iOS 在 程序内调用手机上安装的地图软件进行导航

// 需求是需要用户 能从 所在位置 到 附近的健身房的 路线, 然而,就一个需求,不值当的添加一个地图, 就用调用手机上第三方地图软件,  什么高德, 百度, 腾讯, iOS 原生地图都可以, 如果手机上什么地图都没有, 还有原生的  地图, 如果连原生的地图都删除了,  呵呵,  那我就不管了,  开玩笑, 如果连原生地图都删除了, 会跳转到  appStore 下载, 苹果自带的软件, 不会删除, 只是把图标隐藏而已, 下载非常快, 几秒即可. 不影响什么

// 进入正题

// 首先 添加白名单, 这是 必须的, 否则不能跳转, 添加在这里

LSApplicationQueriesSchemes

baidumap//百度

iosamap// 高德

comgooglemaps//谷歌

 

// 谷歌经我亲测, 在中国已废,  打开没有内容, 不用也罢

 

重点是打开的代码

iOS 原生地图

//当前的位置

MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];

//目的地的位置

MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(latitude, longitude) addressDictionary:nil]];

toLocation.name = name;

NSArray *items = [NSArray arrayWithObjects:currentLocation, toLocation, nil];

NSDictionary *options = @{ MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsMapTypeKey: [NSNumber numberWithInteger:MKMapTypeStandard], MKLaunchOptionsShowsTrafficKey:@YES };

//打开苹果自身地图应用,并呈现特定的item

[MKMapItem openMapsWithItems:items launchOptions:options];

 

// 百度地图

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

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

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

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

}];

}

 

// 高德地图

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

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

NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%f&lon=%f&dev=0&style=2",@"一七健康",@"yiqihealth",latitude, longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

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

}];

}

 

运行之后, 完美

posted @ 2017-08-22 15:49  Dingzhijie  阅读(1171)  评论(0编辑  收藏  举报