火星坐标(gcj02)、国测局坐标(GPS)和百度坐标(bd0911)互转

火星坐标转百度坐标

/**
 * 火星转百度
 * 
 * @param bd_lat 百度坐标纬度
 * @param bd_lon 百度坐标经度
 */
public void bd_encrypt(double gg_lat, double gg_lon) {
    double x = gg_lon, y = gg_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);
    double bd_lon = z * Math.cos(theta) + 0.0128;//百度坐标的经度
    double bd_lat = z * Math.sin(theta) + 0.0075;//百度坐标的纬度
    LatLng lalng = new LatLng(bd_lat, bd_lon);
    latlonList.add(lalng);
}

 

百度坐标转火星坐标

/**
 * 百度转火星
 * 
 * @param bd_lat 百度坐标纬度
 * @param bd_lon 百度坐标经度
 */
public void bd_decrypt(double bd_lat, double bd_lon) {
	double x = bd_lon - 0.0126, y = bd_lat - 0.0080;
	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);
	System.out.println("经度x=" + x + "--纬度y=" + y + "--转换后的z=" + z
	+ "--得出的theta=" + theta);
	double gcj02_lat = z * Math.sin(theta);//火星坐标纬度
	double gcj02_lon = z * Math.cos(theta);//火星坐标经度
	LatLng lalng = new LatLng(gcj02_lat, gcj02_lon);
	gcj02List.add(lalng);
	Log.d("百度坐标-->火星坐标", "经纬度:" + lalng);
}

 

GPS转百度坐标

/**
 * GPS转百度
 * 
 * @param sourceLatLng
 * @return
 */
public LatLng convertGPSToBaidu(LatLng sourceLatLng) {
	// 将GPS设备采集的原始GPS坐标转换成百度坐标
	CoordinateConverter converter = new CoordinateConverter();
	converter.from(CoordType.GPS);
	// sourceLatLng待转换坐标
	converter.coord(sourceLatLng);
	LatLng desLatLng = converter.convert();
	return desLatLng;
}

 

百度坐标转GPS

/**
 * Baidu to GPS  百度转GPS
 * 
 * @param sourceLatLng
 * @return
 */
public LatLng convertBaiduToGPS(LatLng sourceLatLng) {
	// 将GPS设备采集的原始GPS坐标转换成百度坐标
	CoordinateConverter converter = new CoordinateConverter();
	converter.from(CoordType.GPS);
	// sourceLatLng待转换坐标
	converter.coord(sourceLatLng);
	LatLng desLatLng = converter.convert();
	double latitude = 2 * sourceLatLng.latitude - desLatLng.latitude;
	double longitude = 2 * sourceLatLng.longitude - desLatLng.longitude;
	BigDecimal bdLatitude = new BigDecimal(latitude);
	bdLatitude = bdLatitude.setScale(6, BigDecimal.ROUND_HALF_UP);
	BigDecimal bdLongitude = new BigDecimal(longitude);
	bdLongitude = bdLongitude.setScale(6, BigDecimal.ROUND_HALF_UP);
	return new LatLng(bdLatitude.doubleValue(), bdLongitude.doubleValue());
}

 

 

 

转载自:https://blog.csdn.net/Johnson8702/article/details/82184218

You may also like...