zoukankan      html  css  js  c++  java
  • 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     }
  • 相关阅读:
    1.2 偏差与方差
    深度学习中Xavier初始化
    1.1 训练/开发/测试集
    Python笔记(5)类__方法与继承
    Python笔记(4)类__属性与描述符
    Python笔记(3)迭代器与生成器
    Python笔记(2)函数
    线性回归 Linear Regression
    Python笔记(1)变量与表达式
    跳转到某个Activity
  • 原文地址:https://www.cnblogs.com/efanfan/p/7454014.html
Copyright © 2011-2022 走看看