項目中遇到的一個需求,跳轉外部App進行步行導航偶房。分別是百度趁曼、高德、騰訊棕洋。
東西不難但是每次都要去官方文檔找(有時候會更新一些字段)挡闰。
所以在此做下記錄和分享。
騰訊地圖
try {
LatLng latLng=TransBaiduGaodePoint.baidu_to_gaode(new LatLng(Double.parseDouble(lat),Double.parseDouble(lng)));
? ? Uri uri = Uri.parse("qqmap://map/routeplan?type=walk" +
"&to=" +add//終點的顯示名稱 必要參數
? ? ? ? ? ? +"&tocoord=" + latLng.latitude +"," + latLng.longitude//終點的經緯度
? ? ? ? ? ? +"&referer=呼喚");
? ? Intent intent =new Intent();
? ? intent.setData(uri);
? ? baseContext.startActivity(intent);
}catch (Exception e) {
Toast.makeText(baseContext,"請安裝騰訊地圖",Toast.LENGTH_SHORT).show();
}
百度地圖
LatLng ptStart =new LatLng(Params.latitude, Params.longitude);
LatLng ptEnd =new LatLng(Double.parseDouble(lat), Double.parseDouble(lng));
// 構建 route搜索參數
RouteParaOption para =new RouteParaOption()
.startPoint(ptStart)
.endName(add)
.endPoint(ptEnd);
try {
BaiduMapRoutePlan.openBaiduMapWalkingRoute(para, baseContext);
}catch (Exception e) {
e.printStackTrace();
? ? Toast.makeText(baseContext,"請安裝百度地圖",Toast.LENGTH_SHORT).show();
}
高德地圖
try {
LatLng latLng=TransBaiduGaodePoint.baidu_to_gaode(new LatLng(Double.parseDouble(lat),Double.parseDouble(lng)));
? ? Uri uri = Uri.parse("amapuri://route/plan/?dlat="+latLng.latitude+"&dlon="+latLng.longitude+"&dname="+add+"&dev=0&t=2");
? ? Intent intent =new Intent("android.intent.action.VIEW", uri);
? ? intent.addCategory("android.intent.category.DEFAULT");
? ? baseContext.startActivity(intent);
}catch (Exception e) {
Toast.makeText(baseContext,"請安裝高德地圖",Toast.LENGTH_SHORT).show();
}
坐標轉換
注:我的項目中用的是百度地圖掰盘,所以到高德和騰訊里面會有很多偏差摄悯。所以有個坐標轉換。
/**
* 將百度坐標轉變成火星坐標
*
* @param lngLat_bd 百度坐標(百度地圖坐標)
* @return 火星坐標(高德愧捕、騰訊地圖等)
*/
public static LatLngbaidu_to_gaode(LatLng lngLat_bd) {
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)));
}
最后附上三個地圖跳轉文檔的官方鏈接
高德(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)
喜歡的朋友可以關注一下