美文网首页
android判断手机中有无安装地图应用来实现跳转到地图应用

android判断手机中有无安装地图应用来实现跳转到地图应用

作者: zhengLH | 来源:发表于2019-05-29 15:50 被阅读0次

【1】https://blog.csdn.net/ever69/article/details/82427085
【2】https://www.jianshu.com/p/1972b57e9a58

  //其他地图的对话框
public void createDialog() {

    appCompatDialog = new AppCompatDialog(getContext());

    View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_other_map, null);
    TextView goto_baidumap = view.findViewById(R.id.goto_baidumap);
    TextView goto_gaodemap = view.findViewById(R.id.goto_gaodemap);
    try {
        goto_baidumap.setOnClickListener(v -> {

            if (checkMapAppsIsExist(getActivity(), BAIDU_PKG)) {
                try {
                    String s = "baidumap://map/direction?destination=latlng:" + latitude + "," + longitude + "|name:" + addressthis + "&mode=driving";
                    Uri uri = Uri.parse(s);
                    getActivity().startActivity(new Intent(Intent.ACTION_VIEW, uri));
                    appCompatDialog.dismiss();
                } catch (Exception e) {
                    getUiHelper().showToast("百度地图未安装");
                }

            } else {
                getUiHelper().showToast("百度地图未安装");
            }

        });
        goto_gaodemap.setOnClickListener(view1 -> {
            if (checkMapAppsIsExist(getActivity(), GAODE_PKG)) {
                try {
                    togaode();
                    Uri uri = Uri.parse("amapuri://route/plan/?dlat=" + gaodeLatitude + "&dlon=" + gaodeLongitude + "&dname=" + addressthis + "&dev=0&t=0");
                    getActivity().startActivity(new Intent(Intent.ACTION_VIEW, uri));
                    appCompatDialog.dismiss();
                } catch (Exception e) {
                    getUiHelper().showToast("高德地图未安装");
                }

            } else {
                getUiHelper().showToast("高德地图未安装");
            }

        });
    } catch (Exception e) {
        e.printStackTrace();
    }

    appCompatDialog.setContentView(view);
    appCompatDialog.setCancelable(true);
    appCompatDialog.show();

}

//检测地图应用是否安装
public boolean checkMapAppsIsExist(Context context, String packagename) {
    PackageInfo packageInfo;
    try {
        packageInfo = context.getPackageManager().getPackageInfo(packagename, 0);
    } catch (Exception e) {
        packageInfo = null;
        e.printStackTrace();
    }
    if (packageInfo == null) {
        return false;
    } else {
        return true;
    }
}

 void togaode() {
    LatLng sourceLatLng = new LatLng(latitude, longitude);

    CoordinateConverter converter = new CoordinateConverter();
     // 待转换坐标类型
    converter.from(CoordinateConverter.CoordType.BD09LL);
     // sourceLatLng待转换坐标点 LatLng类型
    converter.coord(sourceLatLng);
     // 执行转换操作
    LatLng desLatLng = converter.convert();
    gaodeLatitude = desLatLng.latitude;
    gaodeLongitude = desLatLng.longitude;

}

相关文章

网友评论

      本文标题:android判断手机中有无安装地图应用来实现跳转到地图应用

      本文链接:https://www.haomeiwen.com/subject/mbuytctx.html