Android和IOS启动第三方地图APP

最近客户新提了需求,地址字段要能通过第三方的地图进行定位,于是对Android和IOS端进行了调整。

以下是调用地图部分的代码。

android可按照包名来判断app是否存在:

方法:

 1    /*
 2      * check the app is installed
 3      */
 4     private boolean isAppInstalled(Context context, String packagename) {
 5         PackageInfo packageInfo;
 6         try {
 7             packageInfo = context.getPackageManager().getPackageInfo(packagename, 0);
 8         } catch (PackageManager.NameNotFoundException e) {
 9             packageInfo = null;
10             e.printStackTrace();
11         }
12         if (packageInfo == null) {
13             //System.out.println("没有安装");
14             return false;
15         } else {
16             //System.out.println("已经安装");
17             return true;
18         }
19     }

这是调用,我的是直接调用启动地图。你可以用原生实现一个操作表让用户选择后启动相应的APP。

 1  if (isAppInstalled(context, "com.autonavi.minimap")) {
 2         url = "amapuri://poi?sourceApplication=ewpower.com&keywords="+address;
 3         showToast("启动高德地图");
 4     }else if (isAppInstalled(context, "com.baidu.BaiduMap")) {
 5         url = "baidumap://map/geocoder?src=openApiDemo&address="+address;
 6         showToast("启动百度地图");
 7     } else {
 8         showToast("检测到您未安装地图APP,无法开始导航,建议您安装最新版的高德地图或百度地图");
 9         return;
10     }

IOS可用canOpenURL来判断Schema是否存在判断,代码如下:
记得添加 lsapplicationqueriesschemes

 1 if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]])
 2     {
 3         url = [[NSString stringWithFormat:@"iosamap://poi?sourceApplication=applicationName&name=%@",address]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
 4     }
 5     else if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]])
 6     {
 7         url = [[NSString stringWithFormat:@"baidumap://map/geocoder?address=%@&src=%@",address,@"ewpower.com"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
 8     }else
 9     {
10         UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"检测到您未安装地图APP,无法开始导航,建议您安装最新版的高德地图或百度地图" delegate:self cancelButtonTitle:@"知道啦"otherButtonTitles:nil, nil];
11         [alert show];
12         return;
13     }
14     
15     NSURL *schema = [NSURL URLWithString:url];
16     if ([[UIDevice currentDevice].systemVersion integerValue] >= 10) {
17         //iOS10以后,使用新API
18         
19         [[UIApplication sharedApplication] openURL:schema options:@{} completionHandler:^(BOOL success) { NSLog(@"scheme调用结束"); }];
20     } else {
21         //iOS10以前,使用旧API
22         [[UIApplication sharedApplication] openURL:schema];
23     }

 

posted @ 2017-08-30 17:05  季小鱼  阅读(750)  评论(0编辑  收藏  举报