安卓通过经纬度,实现intent调用百度,高德,Google地图客户端实现驾车导航功能

大家在开发中实现多套地图实现导航功能,导入sdk会增大APK体积,且繁琐,但是在代码可以通过Intent就可以直接调用百度地图客户端、高德地图客户端、Google 地图客户端实现导航功能!话不多说直接上代码吧,浅显易懂!
地图官方公开URI API如下:
百度地图:http://lbsyun.baidu.com/index.php?title=uri/api/android
高德地图:http://lbs.amap.com/api/uri-api/guide/android-uri-explain/navi/
谷歌地图:https://developers.google.com/maps/documentation/android-api/intents
1.判断是否安装该地图

 private boolean isInstallByread(String packageName) {
        return new File("/data/data/" + packageName).exists();
    }

2.启用高德地图进行导航

 /**
     * 启动高德App进行导航
     * sourceApplication 必填 第三方调用应用名称。如 amap
     * poiname           非必填 POI 名称
     * dev               必填 是否偏移(0:lat 和 lon 是已经加密后的,不需要国测加密; 1:需要国测加密)
     * style             必填 导航方式(0 速度快; 1 费用少; 2 路程短; 3 不走高速;4 躲避拥堵;5 不走高速且避免收费;6 不走高速且躲避拥堵;7 躲避收费和拥堵;8 不走高速躲避收费和拥堵))
     */
    private void openGaoDeNavi() {
        StringBuffer stringBuffer = new StringBuffer("androidamap://navi?sourceApplication=")
                .append("yitu8_driver").append("&lat=").append(lat)
                .append("&lon=").append(lng)
                .append("&dev=").append(1)
                .append("&style=").append(0);
        ;
//        if (!TextUtils.isEmpty(poiname)) {
//            stringBuffer.append("&poiname=").append(poiname);
//        }
        Intent intent = new Intent(Intent.ACTION_VIEW, android.net.Uri.parse(stringBuffer.toString()));
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.setPackage("com.autonavi.minimap");
        startActivity(intent);
    }

3.打开百度地图客户端进行导航

 /**
     * 打开百度地图导航客户端
     * intent = Intent.getIntent("baidumap://map/navi?location=34.264642646862,108.95108518068&type=BLK&src=thirdapp.navi.you
     * location 坐标点 location与query二者必须有一个,当有location时,忽略query
     * query    搜索key   同上
     * type 路线规划类型  BLK:躲避拥堵(自驾);TIME:最短时间(自驾);DIS:最短路程(自驾);FEE:少走高速(自驾);默认DIS
     */
    private void openBaiduNavi() {
        StringBuffer stringBuffer = new StringBuffer("baidumap://map/navi?location=")
                .append(lat).append(",").append(lng).append("&type=TIME");
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(stringBuffer.toString()));
        intent.setPackage("com.baidu.BaiduMap");
        startActivity(intent);
    }

4.打开手机浏览器即Google Web进行导航

     /**
     * 打开google Web地图导航
     */
    private void openWebGoogleNavi() {
        StringBuffer stringBuffer = new StringBuffer("http://ditu.google.cn/maps?hl=zh&mrt=loc&q=").append(lat).append(",").append(lng);
        Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(stringBuffer.toString()));
        startActivity(i);
    }

5.打开Google 地图客户端进行导航

    /**
     * 打开google地图客户端开始导航
     * q:目的地
     * mode:d驾车 默认
     */
    private void openGoogleNavi() {
        StringBuffer stringBuffer = new StringBuffer("google.navigation:q=").append(lat).append(",").append(lng).append("&mode=d");
        Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(stringBuffer.toString()));
        i.setPackage("com.google.android.apps.maps");
        startActivity(i);
    }

6.未安装地图,跳转商店

(1)百度地图
//market为路径,id为包名  
//显示手机上所有的market商店  
Toast.makeText(context, "您尚未安装百度地图", Toast.LENGTH_LONG).show();
Uri uri = Uri.parse("market://details?id=com.baidu.BaiduMap");  
Intent intent = new Intent(Intent.ACTION_VIEW, uri);   
startActivity(intent);   
(2)高德地图
Toast.makeText(context, "您尚未安装高德地图", Toast.LENGTH_LONG).show();
Uri uri = Uri.parse("market://details?id=com.autonavi.minimap");  
Intent intent = new Intent(Intent.ACTION_VIEW, uri);   
startActivity(intent);
(3)Google地图
Toast.makeText(context, "您尚未安装谷歌地图", Toast.LENGTH_LONG).show();
Uri uri = Uri.parse("market://details?id=com.google.android.apps.maps");  
Intent intent = new Intent(Intent.ACTION_VIEW, uri);   
startActivity(intent);

你可能感兴趣的:(android)