百度转高德==》
/**
* 百度坐标系 (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);
}