//需在 info.plist 配置白名单 LSApplicationQueriesSchemes baidumap / iosamap / qqmap
#pragma mark++++ 打开地图导航 ++++
-(void)goMapWithLatitude:(NSString*)latitudeLongitude:(NSString*)longitudeAddressName:(NSString*)addressName
{
NSMutableArray *mapArr = [NSMutableArray arrayWithCapacity:0];
if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]){
[mapArraddObject:LocalizedString(@"百度地图")];
}
if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]){
[mapArraddObject:LocalizedString(@"高德地图")];
}
if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]){
[mapArraddObject:LocalizedString(@"腾讯地图")];
}
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"http://maps.apple.com/"]])
{
[mapArraddObject:LocalizedString(@"苹果地图")];
}
if(mapArr.count>0)
{
UIAlertController *mapAlert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
for(NSString*mapNameinmapArr)
{
UIAlertAction*Action = [UIAlertActionactionWithTitle:mapNamestyle:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction)
{
[selfJumpToMap:action.titleWithLatitude:latitudeLongitude:longitudeAddressName:addressName];
}];
[mapAlertaddAction:Action];
}
//取消
UIAlertAction*cancelAction = [UIAlertActionactionWithTitle:LocalizedString(@"取消")style:UIAlertActionStyleCancelhandler:^(UIAlertAction*_Nonnullaction) {
}];
[mapAlertaddAction:cancelAction];
[self presentViewController:mapAlert animated:YES completion:^{
}];
}
else
{
// 弹出提示框
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:LocalizedString(@"提示") message:LocalizedString(@"请先安装地图软件,再使用此功能") preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* cancelAc = [UIAlertActionactionWithTitle:LocalizedString(@"取消")style:UIAlertActionStyleCancelhandler:^(UIAlertAction*_Nonnullaction) {
//点击取消要执行的代码
}];
UIAlertAction*comfirmAc = [UIAlertActionactionWithTitle:LocalizedString(@"确定")style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {
//点击确定要执行的代码
}];
[alertVCaddAction:cancelAc];
[alertVCaddAction:comfirmAc];
[self presentViewController:alertVC animated:YES completion:nil];
}
}
//选择地图
- (void)JumpToMap:(NSString*)mapNameWithLatitude:(NSString*)latitudeLongitude:(NSString*)longitudeAddressName:(NSString*)addressName
{
NSLog(@"\n---CURRENT_Latitude:%@----\n",[UserDefaults objectForKey:@"CURRENT_Latitude"]);
NSLog(@"\n---CURRENT_Longitude:%@----\n",[UserDefaults objectForKey:@"CURRENT_Longitude"]);
NSLog(@"\n---latitude:%@----\n",latitude);
NSLog(@"\n---longitude:%@----\n",longitude);
if([mapNameisEqualToString:LocalizedString(@"苹果地图")])
{
[selfgoAppleMapWithLatitude:latitudeLongitude:longitudeAddressName:addressName];
}
elseif([mapNameisEqualToString:LocalizedString(@"百度地图")])
{
[selfgoBaiduMapWithLatitude:latitudeLongitude:longitudeAddressName:addressName];
}
elseif([mapNameisEqualToString:LocalizedString(@"高德地图")])
{
[selfgoGaodeiMapWithLatitude:latitudeLongitude:longitudeAddressName:addressName];
}
elseif([mapNameisEqualToString:LocalizedString(@"腾讯地图")])
{
[selfgoQQMapWithLatitude:latitudeLongitude:longitudeAddressName:addressName];
}
}
#pragma mark+++++ 苹果地图 ++++++
-(void)goAppleMapWithLatitude:(NSString*)latitudeLongitude:(NSString*)longitudeAddressName:(NSString*)addressName
{
CLLocationCoordinate2D loc = CLLocationCoordinate2DMake([latitude doubleValue], [longitude doubleValue]);
//当前的位置
MKMapItem*currentLocation = [MKMapItemmapItemForCurrentLocation];
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:loc addressDictionary:nil]];
toLocation.name= addressName;//目的地
NSDictionary *options =@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsMapTypeKey: [NSNumber numberWithInteger:MKMapTypeStandard],
MKLaunchOptionsShowsTrafficKey:@YES};
[MKMapItemopenMapsWithItems:@[currentLocation, toLocation]
launchOptions:options];
}
#pragma mark+++++ 打开腾讯地图 +++
-(void)goQQMapWithLatitude:(NSString*)latitudeLongitude:(NSString*)longitudeAddressName:(NSString*)addressName
{
//传入经纬度页面来进行跳转 // 判断是否安装了百度地图如果未安装提示
if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]])
{
//https://lbs.qq.com/webApi/uriV1/uriGuide/uriMobileGuide
//请填写开发者key, [点此申请] DVKBZ-BGS6R-KNRW5-WJ3KY-5OADE-KZF4J
NSString*referer = @"DVKBZ-BGS6R-KNRW5-WJ3KY-5OADE-KZF4J";
NSString *urlString = [[NSString stringWithFormat:@"qqmap://map/routeplan?from=我的位置&to=%@&type=drive&tocoord=%@,%@&coord_type=1&referer=%@",addressName,latitude, longitude,referer] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}else{
// 弹出提示框
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:LocalizedString(@"提示") message:LocalizedString(@"请先安装地图软件,再使用此功能") preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* cancelAc = [UIAlertActionactionWithTitle:LocalizedString(@"取消")style:UIAlertActionStyleCancelhandler:^(UIAlertAction*_Nonnullaction) {
//点击取消要执行的代码
}];
UIAlertAction*comfirmAc = [UIAlertActionactionWithTitle:LocalizedString(@"确定")style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {
//点击确定要执行的代码
}];
[alertVCaddAction:cancelAc];
[alertVCaddAction:comfirmAc];
[self presentViewController:alertVC animated:YES completion:nil];
}
}
#pragma mark+++++ 打开高德地图 +++
-(void)goGaodeiMapWithLatitude:(NSString*)latitudeLongitude:(NSString*)longitudeAddressName:(NSString*)addressName
{
//https://lbs.amap.com/api/amap-mobile/guide/ios/navi
//传入经纬度页面来进行跳转 // 判断是否安装了百度地图如果未安装提示
if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]])
{
NSDictionary*infoDictionary=[[NSBundlemainBundle]infoDictionary];
//// app名称
NSString*app_Name=[infoDictionaryobjectForKey:@"CFBundleDisplayName"];
// //// app版本
// NSString*app_Version=[infoDictionary objectForKey:@"CFBundleShortVersionString"];
// //// app build版本
// NSString*app_build=[infoDictionary objectForKey:@"CFBundleVersion"];
// ////手机别名: 用户定义的名称
// NSString*userPhoneName=[[UIDevice currentDevice] name];
// //// 设备名称
// NSString*device_Name=[[UIDevice currentDevice] systemName];
// //// 手机系统版本
// NSString*phone_Version=[[UIDevice currentDevice] systemVersion];
// //// 地方型号 (国际化区域名称)
// NSString*localPhoneModel=[[UIDevice currentDevice] localizedModel];
// 要注意几点:
// sourceApplication=%@&backScheme=%@
// sourceApplication代表你自己APP的名称,会在之后跳回的时候显示出来,所以必须填写。backScheme是你APP的URL Scheme,不填是跳不回来的
// dev=0
// 这里填0就行了,跟上面的gcj02一个意思 ,1代表wgs84, 也用不上。
NSString *urlString = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=%@&backScheme=%@&dlat=%@&dlon=%@&sname=%@&dname=%@&dev=0&t=0",app_Name,@"iosamap",latitude, longitude,@"我的位置",addressName] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}else{
// 弹出提示框
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:LocalizedString(@"提示") message:LocalizedString(@"请先安装地图软件,再使用此功能") preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* cancelAc = [UIAlertActionactionWithTitle:LocalizedString(@"取消")style:UIAlertActionStyleCancelhandler:^(UIAlertAction*_Nonnullaction) {
//点击取消要执行的代码
}];
UIAlertAction*comfirmAc = [UIAlertActionactionWithTitle:LocalizedString(@"确定")style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {
//点击确定要执行的代码
}];
[alertVCaddAction:cancelAc];
[alertVCaddAction:comfirmAc];
[self presentViewController:alertVC animated:YES completion:nil];
}
}
#pragma mark+++++ 打开百度地图 +++
-(void)goBaiduMapWithLatitude:(NSString*)latitudeLongitude:(NSString*)longitudeAddressName:(NSString*)addressName
{
//http://lbsyun.baidu.com/index.php?title=uri/api/ios
//传入经纬度页面来进行跳转 // 判断是否安装了百度地图如果未安装提示
if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]])
{
// 这里面要注意几点:
// 1,origin={{我的位置}}, 这个是不能被修改的,不然无法把出发位置设置为当前位置
// 2,destination = latlng:%f,%f|name = 目的地
// 这里面的 name 的字段不能省略,否则导航会失败,而后面的文字则可以随意
// 3,coord_type = gcj02
// coord_type 允许的值为 bd09ll、gcj02、wgs84,如果你 APP 的地图 SDK 用的是百度地图 SDK,请填 bd09ll,否则就填gcj02,wgs84的话基本是用不上了(需要涉及到地图加密)
NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=%@&latlng:%@,%@|name=目的地&mode=driving&coord_type=bd09ll",addressName,latitude, longitude] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}else{
// 弹出提示框
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:LocalizedString(@"提示") message:LocalizedString(@"请先安装地图软件,再使用此功能") preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* cancelAc = [UIAlertActionactionWithTitle:LocalizedString(@"取消")style:UIAlertActionStyleCancelhandler:^(UIAlertAction*_Nonnullaction) {
//点击取消要执行的代码
}];
UIAlertAction*comfirmAc = [UIAlertActionactionWithTitle:LocalizedString(@"确定")style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {
//点击确定要执行的代码
}];
[alertVCaddAction:cancelAc];
[alertVCaddAction:comfirmAc];
[self presentViewController:alertVC animated:YES completion:nil];
}
}
网友评论