Android之调用高德地图或百度地图原生app

今天做一个路径规划的功能,需要使用到高德地图,或是百度地图,然后想着怎样的实现比较好。刚好也在看前程无忧,它里面就是点击导航的话会先检查本机是否安装了这些地图app,如果有的话,就直接打开使用,没有的话就通过浏览器去打开百度的web地图进行路径规划。

主要的流程是这样,先判断是否安装了高德地图,有的话就打开高德地图app;否则就判断是否有百度地图;否的话就直接用浏览器跳转到高德地图的网页做路径规划:

if(PackageManagerUtil.haveGaodeMap()){
                        openGaodeMapToGuide();
                    } else if(PackageManagerUtil.haveBaiduMap()){
                       openBaiduMapToGuide();
                    }else {
                        openBrowserToGuide();
                    }


相应地图的调用都有它们自己的写法,可以去官网上查阅。我这里有些是少了个起点,因为我的起点是定位获得的,如果我不写的话,它们会默认起点是定位的位置。

 private void openBaiduMapToGuide() {
        Intent intent = new Intent();
        double[] location = GPSUtil.gcj02_To_Bd09(locationX , locationY);
        String url = "baidumap://map/direction?" +
                "destination=name:"+storeName+"|latlng:"+location[0] + "," + location[1]+
				"&mode=transit&sy=3&index=0&target=1";
        Uri uri = Uri.parse(url);
        //将功能Scheme以URI的方式传入data
        intent.setData(uri);
        //启动该页面即可
        startActivity(intent);
    }

    private void openGaodeMapToGuide() {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        String url = "androidamap://route?sourceApplication=amap&slat="+currLocationX+"&slon="+currLocationY
                +"&dlat="+locationX+"&dlon="+locationY+"&dname="+storeName+"&dev=0&t=1";
        Uri uri = Uri.parse(url);
        //将功能Scheme以URI的方式传入data
        intent.setData(uri);
        //启动该页面即可
        startActivity(intent);
    }



    private void openBrowserToGuide() {
        String url = "http://uri.amap.com/navigation?to=" + locationY + "," + locationX + "," + 
		storeName + "&mode=car&policy=1&src=mypage&coordinate=gaode&callnative=0";
        Uri uri = Uri.parse(url);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);
    }

public class PackageManagerUtil {

    private static PackageManager mPackageManager = TheApp.instance.getPackageManager();
    private static List mPackageNames = new ArrayList<>();
    private static final String GAODE_PACKAGE_NAME = "com.autonavi.minimap";
    private static final String BAIDU_PACKAGE_NAME = "com.baidu.BaiduMap";


    private static void initPackageManager(){

        List packageInfos = mPackageManager.getInstalledPackages(0);

        if (packageInfos != null) {
            for (int i = 0; i < packageInfos.size(); i++) {
                mPackageNames.add(packageInfos.get(i).packageName);
            }
        }
    }

    public static boolean haveGaodeMap(){
        initPackageManager();
        return mPackageNames.contains(GAODE_PACKAGE_NAME);
    }

    public static boolean haveBaiduMap(){
        initPackageManager();
        return mPackageNames.contains(BAIDU_PACKAGE_NAME);
    }
}


注:因为我这里一开始的坐标是高德地图的坐标,而它是不能用在百度地图上的,因为两家采用的标准不一样,所以需要我们将高德地图的坐标转换成百度地图使用的坐标。具体的转换工具方法参考下方的引用。

参考:

百度地图和高德地图坐标系的互相转换

高德地图URI路径规划

高德地图app路径规划调用

百度地图URI API


你可能感兴趣的:(Android学习笔记)