Android implements third-party map navigation (Baidu, Gaode)

Many project requirements require you to implement the address navigation function. If there is no hard requirement, it is undoubtedly the most convenient to jump directly to a third party. Next, let's talk about how to achieve the third-party map navigation jump

1. First determine whether the device is installed with Baidu and Gaode maps. The judgment method is as follows

/**
     * 判断手机中是否安装指定包名的软件
     * @param context
     * @param pkgname 包名
     */
    public static boolean isInstallApk(Context context, String pkgname) {
        List<PackageInfo> packages = context.getPackageManager().getInstalledPackages(0);
        for (int i = 0; i < packages.size(); i++) {
            PackageInfo packageInfo = packages.get(i);
            if (packageInfo.packageName.equals(pkgname)) {
                return true;
            } else {
                continue;
            }
        }
        return false;
    }

2. Start Baidu map navigation

/**
     * 跳转到百度地图
     * @param context
     * @param latitude 纬度
     * @param longtitude 经度
     * @param address 终点
     * */
    private void goBaiduMap(Context context,double latitude, double longtitude, String address) {
        if (isInstallApk(context, "com.baidu.BaiduMap")) {
            try {
                Intent intent = Intent.getIntent("intent://map/direction?destination=latlng:"
                        + latitude + ","
                        + longtitude + "|name:" + address + //终点:该地址会在导航页面的终点输入框显示
                        "&mode=driving&" + //选择导航方式 此处为驾驶
                        "region=" + //
                        "&src=#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
                context.startActivity(intent);
            } catch (URISyntaxException e) {
                Log.e("goError", e.getMessage());
            }
        } else {
            Toast.makeText(context, "您尚未安装百度地图", Toast.LENGTH_SHORT).show();
        }
    }

3. Start the high moral map navigation

/**
     * 跳转到高德地图
     * @param context
     * @param latitude 纬度
     * @param longtitude 经度
     * @param address 终点
     * */
    private void goGaodeMap(Context context,double latitude, double longtitude, String address) {
        if (isInstallApk(context, "com.autonavi.minimap")) {
            try {
                Intent intent = Intent.getIntent("androidamap://navi?sourceApplication=&poiname=" + address + "&lat=" + latitude
                        + "&lon=" + longtitude + "&dev=0");
                context.startActivity(intent);
            } catch (URISyntaxException e) {
                Log.e("goError", e.getMessage());
            }
        } else {
            Toast.makeText(context, "您尚未安装高德地图", Toast.LENGTH_SHORT).show();
        }
    }

 It's that simple, of course, there are more complicated usages, add other parameters

http://lbsyun.baidu.com/index.php?title=uri/api/android

Incidentally give a test address

goBaiduMap(MainActivity.this,22.618458,114.033342,"深圳北站");

Then give another way:

According to the official documentation

Intent i1 = new Intent();
double lat = 22.568287;
double lnt = 114.110793;
double lat2 = 22.562236;
double lnt2 = 114.110895;
String origin = String.format("origin=%s,%s", 0.0, 0.0);
String destination = String.format("&destination=%s,%s", lat2, lnt2);
// 骑行导航
i1.setData(Uri.parse("baidumap://map/bikenavi?" + origin + destination + "&coord_type=bd09ll&src=andr.baidu.openAPIdemo"));
startActivity(i1);

The origin value 0.0, 0.0 represents the default starting position is the current position, destination is the end position

Published 65 original articles · 80 praised · 70,000 + views

Guess you like

Origin blog.csdn.net/lovelixue/article/details/105537638