package com.lvtu.utils;
import com.lvmama.comm.utils.HttpsUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
/**
* 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法.
* @see http://blog.csdn.net/coolypf/article/details/8569813
* bd_encrypt 将 GCJ-02 坐标转换成 BD-09 坐标, bd_decrypt 反之.
*/
public class CoordinateUtil {
private final static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
private final static String baiduLbsAk = "tPhNm3k9msoHmBjGPq9DLZNF";
private final static String baiduApi = "http://api.map.baidu.com/geoconv/v1/";
public static void bd_encrypt(double gg_lat, double gg_lon, double[] pos) {
double x = gg_lon, y = gg_lat;
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);
pos[0] = z * Math.cos(theta) + 0.0065;
pos[1] = z * Math.sin(theta) + 0.006;
}
public static void bd_decrypt(double bd_lat, double bd_lon, double[] pos) {
double x = bd_lon - 0.0065, y = bd_lat - 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);
//经度
pos[0] = z * Math.cos(theta);
//伟度
pos[1] = z * Math.sin(theta);
}
/**
* 将GPS坐标转换成百度地图坐标
*/
public static void gps_baidu_decrypt(double gps_lat, double gps_lon, double[] pos) {
Map
params = new HashMap();
params.put("coords", gps_lon + "," + gps_lat);
params.put("from", "1"); //gps
params.put("to", "5"); //baidu
params.put("ak", baiduLbsAk);
String json = HttpsUtil.requestGet(baiduApi, params);
JSONObject data = JSONObject.fromObject(json);
int status = data.getInt("status");
// x:经度 y:纬度
if (0 == status) {
JSONArray result = data.getJSONArray("result");
if (result != null) {
pos[0] = result.getJSONObject(0).getDouble("x");
pos[1] = result.getJSONObject(0).getDouble("y");
}
}
// 如果接口无法使用,则使用原来的经纬度
else {
pos[0] = gps_lon;
pos[1] = gps_lat;
}
}
public static void main(String[] args) {
// double latitude = 31.539241;
// double longitude = 121.683632;
// double pos[] = new double[2];
// CoordinateUtil.bd_decrypt(latitude, longitude, pos);
// System.out.println("百度经度:" + longitude + ",纬度:" + latitude);
// System.out.println("高德经度:" + pos[0] + ",纬度:" + pos[1]);
// double pos[] = new double[2];
// double latitude = 31.23163273;
// double longitude = 121.37601939;
// CoordinateUtil.gps_baidu_decrypt(latitude, longitude, pos);
// System.out.println("GPS经度:" + longitude + ",纬度:" + latitude);
// System.out.println("百度经度:" + pos[0] + ",纬度:" + pos[1]);
}
}
//将baidu 经纬度 转换为google经纬度
//google经度
double googleLongitude=0;
//google纬度
double googleLatitude=0;
for(ComCoordinateVo comCoordinateVo : comCoordinateVoList) {
if(comCoordinateVo!=null){
ClientComCoordinateVo clientComCoordinateVo = new ClientComCoordinateVo();
if("BAIDU".equalsIgnoreCase(comCoordinateVo.getCoordType())){
double pos[] = new double[2];
if(comCoordinateVo.getLatitude()!=null && comCoordinateVo.getLongitude()!=null){
CoordinateUtil.bd_decrypt(comCoordinateVo.getLatitude(), comCoordinateVo.getLongitude(), pos);
//将baidu 经纬度 转换为google经纬度
googleLongitude =pos[0];
googleLatitude =pos[1];
}
}
BeanUtils.copyProperties(comCoordinateVo, clientComCoordinateVo);
clientComCoordinateVos.add(clientComCoordinateVo);
}
}