抽成工具类了,复制下来就能直接用了,直接看代码吧;

高德地图Url Api: http://lbs.amap.com/api/amap-mobile/guide/android/navigation

百度地图Url Api:http://lbsyun.baidu.com/index.php?title=uri/api/android

  1. /**
  2. * Created by ChenboCui on 2018/6/5.
  3. */
  4. public class MapNaviUtils {
  5. public static final String PN_GAODE_MAP = "com.autonavi.minimap"; // 高德地图包名
  6. public static final String PN_BAIDU_MAP = "com.baidu.BaiduMap"; // 百度地图包名
  7. public static final String DOWNLOAD_GAODE_MAP = "http://www.autonavi.com/"; // 高德地图下载地址
  8. public static final String DOWNLOAD_BAIDU_MAP = "http://map.baidu.com/zt/client/index/"; // 百度地图下载地址
  9. /**
  10. * 检查应用是否安装
  11. * @return
  12. */
  13. public static boolean isGdMapInstalled(){
  14. return isInstallPackage(PN_GAODE_MAP);
  15. }
  16. public static boolean isBaiduMapInstalled(){
  17. return isInstallPackage(PN_BAIDU_MAP);
  18. }
  19. private static boolean isInstallPackage(String packageName) {
  20. return new File("/data/data/" + packageName).exists();
  21. }
  22. /**
  23. * 百度坐标系 (BD-09) 与 火星坐标系 (GCJ-02)的转换
  24. * 即 百度 转 谷歌、高德
  25. *
  26. * @param latLng
  27. * @returns
  28. */
  29. public static LatLng BD09ToGCJ02(LatLng latLng) {
  30. double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
  31. double x = latLng.longitude - 0.0065;
  32. double y = latLng.latitude - 0.006;
  33. double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
  34. double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
  35. double gg_lat = z * Math.sin(theta);
  36. double gg_lng = z * Math.cos(theta);
  37. return new LatLng(gg_lat, gg_lng);
  38. }
  39. /**
  40. * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换
  41. * 即谷歌、高德 转 百度
  42. *
  43. * @param latLng
  44. * @returns
  45. */
  46. public static LatLng GCJ02ToBD09(LatLng latLng) {
  47. double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
  48. double z = Math.sqrt(latLng.longitude * latLng.longitude + latLng.latitude * latLng.latitude) + 0.00002 * Math.sin(latLng.latitude * x_pi);
  49. double theta = Math.atan2(latLng.latitude, latLng.longitude) + 0.000003 * Math.cos(latLng.longitude * x_pi);
  50. double bd_lat = z * Math.sin(theta) + 0.006;
  51. double bd_lng = z * Math.cos(theta) + 0.0065;
  52. return new LatLng(bd_lat, bd_lng);
  53. }
  54. /**
  55. * 打开高德地图导航功能
  56. * @param context
  57. * @param slat 起点纬度
  58. * @param slon 起点经度
  59. * @param sname 起点名称 可不填(0,0,null)
  60. * @param dlat 终点纬度
  61. * @param dlon 终点经度
  62. * @param dname 终点名称 必填
  63. */
  64. public static void openGaoDeNavi(Context context,double slat, double slon, String sname, double dlat, double dlon, String dname){
  65. String uriString = null;
  66. StringBuilder builder = new StringBuilder("amapuri://route/plan?sourceApplication=maxuslife");
  67. if (0 == slat){
  68. // //如果不传起点(注释下面这段),默认就是现在我的位置(手机当前定位)
  69. // AMapLocation location = LocationService.getInstance().getAMapLocation();
  70. // if (LocationService.isSuccess(location)) {
  71. // builder.append("&sname=我的位置")
  72. // .append("&slat=").append(location.getLatitude())
  73. // .append("&slon=").append(location.getLongitude());
  74. // }
  75. }else {
  76. builder.append("&sname=").append(sname)
  77. .append("&slat=").append(slat)
  78. .append("&slon=").append(slon);
  79. }
  80. builder.append("&dlat=").append(dlat)
  81. .append("&dlon=").append(dlon)
  82. .append("&dname=").append(dname)
  83. .append("&dev=0")
  84. .append("&t=0");
  85. uriString = builder.toString();
  86. Intent intent = new Intent(Intent.ACTION_VIEW);
  87. intent.setPackage(PN_GAODE_MAP);
  88. intent.setData(Uri.parse(uriString));
  89. context.startActivity(intent);
  90. }
  91. /**
  92. * 打开百度地图导航功能(默认坐标点是高德地图,需要转换)
  93. * @param context
  94. * @param slat 起点纬度
  95. * @param slon 起点经度
  96. * @param sname 起点名称 可不填(0,0,null)
  97. * @param dlat 终点纬度
  98. * @param dlon 终点经度
  99. * @param dname 终点名称 必填
  100. */
  101. public static void openBaiDuNavi(Context context,double slat, double slon, String sname, double dlat, double dlon, String dname){
  102. String uriString = null;
  103. //终点坐标转换
  104. LatLng destination = new LatLng(dlat,dlon);
  105. LatLng destinationLatLng = GCJ02ToBD09(destination);
  106. dlat = destinationLatLng.latitude;
  107. dlon = destinationLatLng.longitude;
  108. StringBuilder builder = new StringBuilder("baidumap://map/direction?mode=driving&");
  109. if (slat != 0){
  110. //起点坐标转换
  111. LatLng origin = new LatLng(slat,slon);
  112. LatLng originLatLng = GCJ02ToBD09(origin);
  113. slat = originLatLng.latitude;
  114. slon = originLatLng.longitude;
  115. builder.append("origin=latlng:")
  116. .append(slat)
  117. .append(",")
  118. .append(slon)
  119. .append("|name:")
  120. .append(sname);
  121. }
  122. builder.append("&destination=latlng:")
  123. .append(dlat)
  124. .append(",")
  125. .append(dlon)
  126. .append("|name:")
  127. .append(dname);
  128. uriString = builder.toString();
  129. Intent intent = new Intent(Intent.ACTION_VIEW);
  130. intent.setPackage(PN_BAIDU_MAP);
  131. intent.setData(Uri.parse(uriString));
  132. context.startActivity(intent);
  133. }
  134. }

 

使用:

 

 

  1. public void test(View v){
  2. new SingleSelectDialog.Builder(mContext)
  3. .setTestSize(16)
  4. .setTextColor(R.color.colorPrimaryDark)
  5. .setItems(R.array.navi_arrays, (dialog, which) -> {
  6. if (which == 0) { // 导航应用1高德
  7. if (MapNaviUtils.isGdMapInstalled()) {
  8. MapNaviUtils.openGaoDeNavi(mContext,0,0,null,31.239666,121.499809,"上海外滩");
  9. // MapUtils.openMap(mContext,"com.autonavi.minimap",new LatLng(31.33260711060764,121.54777721524306,"CCB"));
  10. }else {
  11. UIHelper.showToast("您还未安装高德地图!");
  12. new AlertDialog.Builder(this)
  13. .setMessage("下载高德地图?")
  14. .setPositiveButton("下载", (dialog1, which1) -> startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(MapNaviUtils.DOWNLOAD_GAODE_MAP))))
  15. .setNegativeButton(R.string.cancel, null)
  16. .show();
  17. }
  18. } else if (which == 1) { // 导航应用2百度
  19. if (MapNaviUtils.isBaiduMapInstalled()){
  20. MapNaviUtils.openBaiDuNavi(mContext,0,0,null,31.239666,121.499809,"上海外滩");
  21. // MapUtils.openMap(mContext,"com.baidu.BaiduMap",new LatLng(31.33260715160764,121.54777723124306,"CCB"));
  22. }else {
  23. UIHelper.showToast("您还未安装百度地图!");
  24. new AlertDialog.Builder(this)
  25. .setMessage("下载百度地图?")
  26. .setNegativeButton("取消", null)
  27. .setPositiveButton("下载", (dialog12, which12) -> startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(MapNaviUtils.DOWNLOAD_BAIDU_MAP))))
  28. .show();
  29. }
  30. }
  31. })
  32. .setHead("地图应用")
  33. .show();
  34. }

 

版权声明:本文为cuichen16原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/cuichen16/p/10785977.html