APP跳转百度、高德、腾讯地图导航

    这个本来是一个很简单的需求,奈何我遇到了很奇葩的问题。最开始我用的是百度sdk中的跳转api,自己用小米和联想测试跳转没有问题,然而用户的魅蓝note5点击跳转没有任何响应,后来又陆续出现了华为meta9也有相应问题,但华为其他机型又可以正常跳转导航。由于测试机有限,问题又都出现在客户方,时间紧迫,排除了android 7.0系统的原因,百度地图论坛中试图找到有关问题,没有相应的解释,后来为了尽快解决问题,直接借鉴了Intent跳转,问题也就解决了。所以很大原因是因为百度SDK新老版本的兼容所导致的问题,兼容问题也是百度地图很坑的地方了。虽然跳转不需要集成百度sdk,但还是需要仔细看下官方开发文档 http://lbsyun.baidu.com/index.php?title=uri/api/android

百度sdk中api打开百度,不建议使用

public static void startBaiduMapNavi(final Context context, LatLng startPoint, String startName, LatLng endPoint, String endName) {
    NaviParaOption para = new NaviParaOption().startPoint(startPoint).startName(startName).endPoint(endPoint).endName(endName);
    try {
        BaiduMapNavigation.openBaiduMapNavi(para, context);
    } catch (BaiduMapAppNotSupportNaviException e) {
        e.printStackTrace();
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setMessage("您尚未安装百度地图app或app版本过低,点击确认安装?");
        builder.setTitle("提示");
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                OpenClientUtil.getLatestBaiduMapApp(context);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }
}

直接intent跳转,要靠谱一些。记得处理地址解析异常,当未安装百度地图的时候,提示框引导安装。可以将起点终点都传进去,也可以只传终点。注释中有各参数说明

    public static void startBaiduMapNavi(final Context context, LatLng startPoint, String startName, LatLng endPoint, String endName) {
        try {
            String startLag = startPoint.latitude + "," + startPoint.longitude;
            String endLag = endPoint.latitude + "," + endPoint.longitude;
            
            //   Intent intent = Intent.getIntent("intent://map/direction?origin=latlng:" + startLag + "|name:" + startName + "&destination=latlng:" + endLag + "|name:" + endName + "&mode=transit&src=某某公司#Intent;" + "scheme=bdapp;package=com.baidu.BaiduMap;end");
            //起点  此处不传值默认选择当前位置
            Intent intent = Intent.getIntent("intent://map/direction?destination=latlng:" + endLag + "|name:" + endName + "&mode=driving&src=某某公司#Intent;" + "scheme=bdapp;package=com.baidu.BaiduMap;end");
           
          
            //            URL接口:baidumap://map/direction
            //            destination : 终点名称或经纬度
            //            经纬度和名称:
            //            latlng:39.9761,116.3282|name:中关村 (注意:坐标先纬度,后经度)
            //            mode : 导航模式 //driving 驾车
            context.startActivity(intent);

        } catch (URISyntaxException e) {
            e.printStackTrace();
            Toast.makeText(context, "地址解析错误", Toast.LENGTH_SHORT).show();
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setMessage("您尚未安装百度地图app或app版本过低,点击确认安装?");
            builder.setTitle("提示");
            builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    OpenClientUtil.getLatestBaiduMapApp(context);
                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            builder.create().show();
        }
    }

跳转到高德地图导航,只需要传入终点经纬度和地址,默认起点为当前地址https://lbs.amap.com/api/amap-mobile/guide/android/route

方法一:跳转后默认驾车导航,不可自主选择出行方式

public static void startGaoDeNavi(final Context context, LatLng endPoint, String endName) {
    if (!isInstalled(context, "com.autonavi.minimap")) {
        Toast.makeText(context, "请先下载安装腾讯地图客户端", Toast.LENGTH_SHORT).show();
        return;
    }
    endPoint = fromBD09(endPoint);//坐标转换
    Intent intent = new Intent("android.intent.action.VIEW",
            android.net.Uri.parse(
                    "androidamap://navi?sourceApplication="
                            + context.getPackageName()
                            + "&poiname=" + endName
                            + "&lat=" + endPoint.latitude + "&lon=" + endPoint.longitude + "&dev=0"));
    intent.setPackage("com.autonavi.minimap");
    context.startActivity(intent);
}

方法二:体验会更好一些,可由用户选择出行方式,下面也给出了详细注释,两个方法参数名不一样,但参数对应含义一样

    public static void startGaoDeNavi(final Context context, LatLng endPoint, String endName) {
        if (!isInstalled(context, "com.autonavi.minimap")) {
            Toast.makeText(context, "请先下载安装高德地图客户端", Toast.LENGTH_SHORT).show();
            return;
        }
        endPoint = fromBD09(endPoint);//坐标转换

        StringBuffer stringBuffer = new StringBuffer("androidamap://route?sourceApplication=").append("amap");

        stringBuffer.append("&dlat=").append(endPoint.latitude) //终点纬度
                .append("&dlon=").append(endPoint.longitude) //终点经度
                .append("&dname=").append(endName) //终点地址
                .append("&dev=").append(0)  //起终点是否偏移(0:lat 和 lon 是已经加密后的,不需要国测加密; 1:需要国测加密)
                .append("&t=").append(0);  //t = 0(驾车)= 1(公交)= 2(步行)= 3(骑行)= 4(火车)= 5(长途客车)

        Intent intent = new Intent("android.intent.action.VIEW", android.net.Uri.parse(stringBuffer.toString()));
        intent.setPackage("com.autonavi.minimap");
        context.startActivity(intent);
    }

跳转到腾讯地图导航,参数可参考http://lbs.qq.com/uri_v1/guide-route.html

/**
 * 腾讯地图跳转URI
 */
public static void startTencentNavi(Context context, LatLng startPoint, String startName, LatLng endPoint, String endName) {
    if (!isInstalled(context, "com.tencent.map")) {
        Toast.makeText(context, "请先下载安装腾讯地图客户端", Toast.LENGTH_SHORT).show();
        return;
    }
    startPoint = fromBD09(startPoint);//坐标转换
    endPoint = fromBD09(endPoint);//坐标转换
    StringBuffer stringBuffer = new StringBuffer("qqmap://map/routeplan?type=drive&from=")
            .append(startName)
            .append("&fromcoord=").append(startPoint.latitude).append(",").append(startPoint.longitude)
            .append("&to=").append(endName)
            .append("&tocoord=").append(endPoint.latitude).append(",").append(endPoint.longitude)
            .append("&policy=2")//0:较快捷1:无高速 2:距离
            .append("&referer=trydriver");
    Intent intent = new Intent("android.intent.action.VIEW", android.net.Uri.parse(stringBuffer.toString()));
    context.startActivity(intent);
}

通过包名检测应用是否安装

public static boolean isInstalled(Context context, String packageName) {
    final PackageManager packageManager = context.getPackageManager();//获取packagemanager
    final List pinfo = packageManager.getInstalledPackages(0);//获取所有已安装程序的包信息
    if (pinfo != null) {
        for (PackageInfo info : pinfo) {
            if (info.packageName.equals(packageName)) {
                return true;
            }
            //pName.add(pn);
        }
    }
    return false;
}

坐标转换

    /**
     * BD-09 坐标转换成 GCJ-02 坐标
     */
    public static LatLng fromBD09(LatLng bd) {
        double x = bd.longitude - 0.0065, y = bd.latitude - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);

        double[] ret = new double[2];
        ret[1] = z * Math.cos(theta);
        ret[0] = z * Math.sin(theta);
        return new LatLng(ret[0], ret[1]);
    }
   /**
     * GCJ-02 坐标转换成 BD-09 坐标
     */
    public static LatLng toBD09(LatLng gg) {
        double x = gg.longitude, y = gg.latitude;
        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
        double[] ret = new double[2];
        ret[1] = z * Math.cos(theta) + 0.0065;
        ret[0] = z * Math.sin(theta) + 0.006;
        return new LatLng(ret[0], ret[1]);
    }


你可能感兴趣的:(APP跳转百度、高德、腾讯地图导航)