Android-跳转高德地图并导航地址(所有地图均可以)

柳仲卿
2023-12-01

一、判断应用是否安装(高德或者百度等),注意存储分区

/**
     * 判断是否安装目标应用
     */
    private boolean isInstallByread(String packageName) {
        /**
         * 判断 API版本是否大于 29(Android 11)
         * 大于29会强制分区
         */
        if (Build.VERSION.SDK_INT < 24) {
            return new File("/data/data/" + packageName).exists();
        }else {
            return new File("storage/emulated/0/Android/data/" + packageName).exists();
        }

    }

二、获取定位端以及终点地址经纬度

//定位发起端
    private AMapLocationClient mLocationClient = null;
//导航
    public void openNavigation(double latitude, double longitude) {
        if (mLocationClient == null) {
            initLocation();
        }

        try {
            double mSlat = mLocationClient.getLastKnownLocation().getLatitude();
            double mSlon = mLocationClient.getLastKnownLocation().getLongitude();

            if (this.isInstallByread("com.autonavi.minimap")) {
                Intent intent = new Intent();
                intent.setData(Uri.parse("androidamap://route?sourceApplication=softname&slat="
                        + mSlat + "&slon=" + mSlon + "&sname=起点&dlat=" + latitude + "&dlon="
                        + longitude + "&dname=终点&dev=0&m=0&t=1&showType=1"));
                // 启动调用
                startActivity(intent);
            } else if (this.isInstallByread("com.baidu.BaiduMap")) {
                double[] wellLocation = GPSUtil.gcj02_To_Bd09(latitude, longitude);
                double[] location = GPSUtil.gcj02_To_Bd09(mSlat, mSlon);
                Intent intent = new Intent();
                intent.setData(Uri.parse("baidumap://map/direction?region="
                        + mLocationClient.getLastKnownLocation().getCity() + "&origin="
                        + location[0] + "," + location[1] + "&destination=" + wellLocation[0]
                        + "," + wellLocation[1] + "&mode=driving"));
                startActivity(intent);
            } else {
                ToastUtils.showShort("请安装高德或百度地图");
                Uri uri = Uri.parse("market://details?id=com.autonavi.minimap");
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
            }
        } catch (Exception e) {
            Log.e("openNavigation: ", e.toString());
            ToastUtils.showShort("请检查是否开启定位权限");
        }
    }

 

 类似资料: