百度轉高德==》
/**
* 百度坐標系 (BD-09) 與 火星坐標系 (GCJ-02)的轉換
* 即 百度 轉 谷歌、高德
*
* @param latLng
* @returns
*/
public static LatLng BD09ToGCJ02(LatLng latLng) {
double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
double x = latLng.longitude - 0.0065;
double y = latLng.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);
double gg_lat = z * Math.sin(theta);
double gg_lng = z * Math.cos(theta);
return new LatLng(gg_lat, gg_lng);
}
高德轉百度==》
/**
* 火星坐標系 (GCJ-02) 與百度坐標系 (BD-09) 的轉換
* 即谷歌、高德 轉 百度
*
* @param latLng
* @returns
*/
public static LatLng GCJ02ToBD09(LatLng latLng) {
double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
double z = Math.sqrt(latLng.longitude * latLng.longitude + latLng.latitude * latLng.latitude) + 0.00002 * Math.sin(latLng.latitude * x_pi);
double theta = Math.atan2(latLng.latitude, latLng.longitude) + 0.000003 * Math.cos(latLng.longitude * x_pi);
double bd_lat = z * Math.sin(theta) + 0.006;
double bd_lng = z * Math.cos(theta) + 0.0065;
return new LatLng(bd_lat, bd_lng);
}
獲取轉換后的經緯度打開高德地圖
StringBuilder builder = new StringBuilder("amapuri://route/plan?sourceApplication=maxuslife");
String uriString = null;
if (0 == slat) {
//默認我的位置
} else {
builder.append("&sname=").append("null")
.append("&slat=").append("0")
.append("&slon=").append("0");
}
builder.append("&dlat=").append(latLng.latitude)
.append("&dlon=").append(latLng.longitude)
.append("&dname=").append(dname)//要去的地址
.append("&dev=0")
.append("&t=0");
uriString = builder.toString();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setPackage(PN_GAODE_MAP);
intent.setData(Uri.parse(uriString));
context.startActivity(intent);
打開后高德地圖實際定位的位置并不準確,后又參考了高德開發檔案提供的api轉換
public static LatLng getLatLng(Activity context, LatLng destination, double slat, double slon, String sname, String dname) {
String url = "https://restapi.amap.com/v3/assistant/coordinate/convert?"
+ "locations=" + destination.longitude + "," + destination.latitude
+ "&coordsys=baidu&output==json&key=25d18d3aff6af7a0b315428d9c450d1c";
HttpClientWx.getInstance(context).get(url, new HttpClientWx.MyCallback() {
@Override
public void success(String res) throws IOException {
MyLog.e(res);
TransformMapBean bean = GsonUtils.fromJson(res, TransformMapBean.class);
String latlng = bean.locations;
String lng = latlng.substring(0, latlng.indexOf(","));
String lat = latlng.substring(latlng.indexOf(",") + 1, latlng.length());
MyLog.e(destination.toString());
MyLog.e(lng + " " + lat);
StringBuilder builder = new StringBuilder("amapuri://route/plan?sourceApplication=maxuslife");
String uriString = null;
if (0 == slat) {
//默認我的位置
} else {
builder.append("&sname=").append(sname)
.append("&slat=").append(slat)
.append("&slon=").append(slon);
}
builder.append("&dlat=").append(lat)
.append("&dlon=").append(lng)
.append("&dname=").append(dname)
.append("&dev=0")
.append("&t=0");
uriString = builder.toString();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setPackage(PN_GAODE_MAP);
intent.setData(Uri.parse(uriString));
context.startActivity(intent);
}
@Override
public void failed(IOException e) {
MyLog.e(e.getMessage());
}
});
return null;
}

傳入的經緯度為百度的經緯度,收到高德官方回傳的轉換后經緯度并打開高德,還是與實際有所差異
最后通過**關鍵詞路線規劃** 才完成
public static void openGaoDeNavi(Activity context, String dname) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setPackage(PN_GAODE_MAP);
intent.setData(Uri.parse("androidamap://keywordNavi?sourceApplication=softname&keyword="+dname+"&style=2"));
context.startActivity(intent);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/281666.html
標籤:其他
