三个地图跳转文档的官方链接
高德(https://lbs.amap.com/api/amap-mobile/guide/android/route)
百度(http://lbsyun.baidu.com/index.php?title=uri/api/ios&qq-pf-to=pcqq.c2c)
腾讯(https://lbs.qq.com/uri_v1/guide-mobile-navAndRoute.html)
————————————————
/**
* 导航工具类:参考https://www.jianshu.com/p/fda64caa2875
*/
public class GpsUtils {
/**
* 启动高德App进行导航
*/
public static void gotoGaodeMap(Context context, double lat, double lng, String end) {
try {
LatLng latLng = baidu_to_gaode(new LatLng(lat, lng));
Uri uri = Uri.parse("amapuri://route/plan/?dlat=" + latLng.latitude + "&dlon=" + latLng.longitude + "&dname=" + end + "&dev=0&t=0");
Intent intent = new Intent("android.intent.action.VIEW", uri);
intent.addCategory("android.intent.category.DEFAULT");
context.startActivity(intent);
} catch (Exception e) {
ToastUtil.showToast("请安装高德地图");
}
}
/**
* 打开百度地图导航客户端
*/
public static void gotoBaiduMap(Context context, double lat, double lng, String end) {
try {
Uri uri = Uri.parse("baidumap://map/direction?destination=latlng:" + lat + "," + lng + "|name:" + end + "&mode=driving");
context.startActivity(new Intent(Intent.ACTION_VIEW, uri));
} catch (Exception e) {
ToastUtil.showToast("请安装百度地图");
}
}
/**
* 打开腾讯地图导航客户端
*/
public static void gotoTengxunMap(Context context, double lat, double lng, String end) {
try {
LatLng latLng = baidu_to_gaode(new LatLng(lat, lng));
Uri uri = Uri.parse("qqmap://map/routeplan?"
+ "type=drive"
+ "&to=" + end//终点的显示名称 必要参数
+ "&tocoord=" + latLng.latitude + "," + latLng.longitude//终点的经纬度
+ "&referer=test");
Intent intent = new Intent();
intent.setData(uri);
context.startActivity(intent);
} catch (Exception e) {
ToastUtil.showToast("请安装腾讯地图");
}
}
/**
* 将百度坐标转变成火星坐标
*
* @param lngLat_bd 百度坐标(百度地图坐标)
* @return 火星坐标(高德 、 腾讯地图等)
*/
public static LatLng baidu_to_gaode(LatLng lngLat_bd) {
double x_pi = Math.PI * 3000.0 / 180.0;
double x = lngLat_bd.longitude - 0.0065, y = lngLat_bd.latitude - 0.006;
double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
return new LatLng(dataDigit(6, z * Math.sin(theta)), dataDigit(6, z * Math.cos(theta)));
}
/**
* 对double类型数据保留小数点后多少位
* <p>
* 高德地图转码返回的就是 小数点后6位,为了统一封装一下
*
* @param digit 位数
* @param in 输入
* @return 保留小数位后的数
*/
static double dataDigit(int digit, double in) {
return new BigDecimal(in).setScale(6, BigDecimal.ROUND_HALF_UP).doubleValue();
}
}