高德地圖百度地圖導航座標轉換
阿新 • • 發佈:2019-01-07
專案中的定位商家服務搜尋用的是高德地圖,畢竟高德在地圖這塊做的比百度好(個人認為)。這裡吐槽下百度的開發平臺,上面開發文件及案例寫的真不好,想找某個問題點很難找到和定位,希望百度改善下。
高德轉百度(火星座標gcj02ll–>百度座標bd09ll)
private double[] gaoDeToBaidu(double gd_lon, double gd_lat) {
double[] bd_lat_lon = new double[2];
double PI = 3.14159265358979324 * 3000.0 / 180.0;
double x = gd_lon, y = gd_lat;
double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * PI);
double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * PI);
bd_lat_lon[0] = z * Math.cos(theta) + 0.0065;
bd_lat_lon[1] = z * Math.sin(theta) + 0.006;
return bd_lat_lon;
}
百度轉高德(百度座標bd09ll–>火星座標gcj02ll)
private double[] bdToGaoDe(double bd_lat, double bd_lon) {
double[] gd_lat_lon = new double[2];
double PI = 3.14159265358979324 * 3000.0 / 180.0;
double x = bd_lon - 0.0065, y = bd_lat - 0.006;
double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * PI);
double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * PI);
gd_lat_lon[0 ] = z * Math.cos(theta);
gd_lat_lon[1] = z * Math.sin(theta);
return gd_lat_lon;
}
呼叫高德導航:
1.AndroidAPI呼叫方式
/***
*
* @author James
* @Description 呼叫高德去導航
* @param mLatLng
* @param type
*/
public static void startNavByAmap(LatLng mLatLng, int type, Context mContext) {
try {
// 構造導航引數
NaviPara naviPara = new NaviPara();
// 設定終點位置
naviPara.setTargetPoint(mLatLng);
// 設定導航策略,這裡是避免擁堵
naviPara.setNaviStyle(type);
// 調起高德地圖導航
AMapUtils.openAMapNavi(naviPara, mContext);
} catch (AMapException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
2,URI呼叫方式(不想下載SDK包使用URI呼叫也很方便)
Intent gddtIntent = new Intent("android.intent.action.VIEW",
Uri.parse("androidamap://navi?sourceApplication=“name”&poiname="
+ poi + "&lat=" + lat + "&lon=" + lon + "&dev=" + 0
+ "&style=" + 4));
gddtIntent.addCategory("android.intent.category.DEFAULT");
gddtIntent.setPackage("com.autonavi.minimap");
gddtIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(gddtIntent);
引數
style導航策略
呼叫百度導航
1.AndroidAPI呼叫方式
double mLat1 = 39.915291;
double mLon1 = 116.403857;
// 百度大廈座標
double mLat2 = 40.056858;
double mLon2 = 116.308194;
LatLng pt1 = new LatLng(mLat1, mLon1);
LatLng pt2 = new LatLng(mLat2, mLon2);
NaviParaOption para = new NaviParaOption()
.startPoint(pt1).endPoint(pt2)
.startName("天安門").endName("百度大廈");
try {
// 調起百度地圖導航
BaiduMapNavigation.openBaiduMapNavi(para, this);
} catch (BaiduMapAppNotSupportNaviException e) {
e.printStackTrace();
}
2,URI呼叫方式
intent = Intent
.getIntent("intent://map/navi?location="
+ lonlat[1]
+ ","
+ lonlat[0]
+ "&type="
+ strategyBD
+
"&coordType=bd09ll&src=thirdapp.navi.公司名字#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
mContext.startActivity(intent); // 啟動呼叫
引數
strategyBD 導航策略