(文章代码参考网上 测试没什么问题, 汇总整理希望对大家有帮助-dou )
WGS84:国际坐标系,为一种大地坐标系,也是目前广泛使用的GPS全球卫星定位系统使用的坐标系。
GCJ02:火星坐标系,是由中国国家测绘局制订的地理信息系统的坐标系统。由WGS84坐标系经加密后的坐标系。
BD09:为百度坐标系,在GCJ02坐标系基础上再次加密。其中bd09ll表示百度经纬度坐标,bd09mc表示百度墨卡托米制坐标
一 : 百度坐标与百度墨卡坐标互转
import java.util.HashMap;
import java.util.Map;
/**
* @ClassName: Baidu
* @Description: 百度坐标与百度墨卡坐标互转
* @author: Max dou
* @date: 2017年4月11日 下午4:49:54
*/
public class Baidu {
public static void main(String[] args) {
//百度坐标转百度墨卡坐标
Map<String, Double> location = convertMC2LL(13745329.000000, 5104310.500000);
System.out.println(location.get("lng")+"==="+location.get("lat"));
//百度墨卡坐标转百度坐标
location = convertLL2MC(location.get("lng"),location.get("lat"));
System.out.println(location.get("x")+"==="+location.get("y"));
}
private static Double EARTHRADIUS = 6370996.81;
private static Double[] MCBAND = {12890594.86, 8362377.87, 5591021d, 3481989.83, 1678043.12, 0d};
private static Double[] LLBAND = {75d, 60d, 45d, 30d, 15d, 0d};
private static Double[][] MC2LL = {{1.410526172116255e-8, 0.00000898305509648872, -1.9939833816331, 200.9824383106796, -187.2403703815547, 91.6087516669843, -23.38765649603339, 2.57121317296198, -0.03801003308653, 17337981.2}, {-7.435856389565537e-9, 0.000008983055097726239, -0.78625201886289, 96.32687599759846, -1.85204757529826, -59.36935905485877, 47.40033549296737, -16.50741931063887, 2.28786674699375, 10260144.86}, {-3.030883460898826e-8, 0.00000898305509983578, 0.30071316287616, 59.74293618442277, 7.357984074871, -25.38371002664745, 13.45380521110908, -3.29883767235584, 0.32710905363475, 6856817.37}, {-1.981981304930552e-8, 0.000008983055099779535, 0.03278182852591, 40.31678527705744, 0.65659298677277, -4.44255534477492, 0.85341911805263, 0.12923347998204, -0.04625736007561, 4482777.06}, {3.09191371068437e-9, 0.000008983055096812155, 0.00006995724062, 23.10934304144901, -0.00023663490511, -0.6321817810242, -0.00663494467273, 0.03430082397953, -0.00466043876332, 2555164.4}, {2.890871144776878e-9, 0.000008983055095805407, -3.068298e-8, 7.47137025468032, -0.00000353937994, -0.02145144861037, -0.00001234426596, 0.00010322952773, -0.00000323890364, 826088.5}};
private static Double[][] LL2MC = {{-0.0015702102444, 111320.7020616939, 1704480524535203d, -10338987376042340d, 26112667856603880d, -35149669176653700d, 26595700718403920d, -10725012454188240d, 1800819912950474d, 82.5}, {0.0008277824516172526, 111320.7020463578, 647795574.6671607, -4082003173.641316, 10774905663.51142, -15171875531.51559, 12053065338.62167, -5124939663.577472, 913311935.9512032, 67.5}, {0.00337398766765, 111320.7020202162, 4481351.045890365, -23393751.19931662, 79682215.47186455, -115964993.2797253, 97236711.15602145, -43661946.33752821, 8477230.501135234, 52.5}, {0.00220636496208, 111320.7020209128, 51751.86112841131, 3796837.749470245, 992013.7397791013, -1221952.21711287, 1340652.697009075, -620943.6990984312, 144416.9293806241, 37.5}, {-0.0003441963504368392, 111320.7020576856, 278.2353980772752, 2485758.690035394, 6070.750963243378, 54821.18345352118, 9540.606633304236, -2710.55326746645, 1405.483844121726, 22.5}, {-0.0003218135878613132, 111320.7020701615, 0.00369383431289, 823725.6402795718, 0.46104986909093, 2351.343141331292, 1.58060784298199, 8.77738589078284, 0.37238884252424, 7.45}};
/**
* 墨卡托坐标转经纬度坐标
* @param x
* @param y
* @return
*/
public static Map<String, Double> convertMC2LL(Double x, Double y) {
Double[] cF = null;
x = Math.abs(x);
y = Math.abs(y);
for (int cE = 0; cE < MCBAND.length; cE++) {
if (y >= MCBAND[cE]) {
cF = MC2LL[cE];
break;
}
}
Map<String,Double> location = converter(x, y, cF);
location.put("lng",location.get("x"));
location.remove("x");
location.put("lat",location.get("y"));
location.remove("y");
return location;
}
/**
* 经纬度坐标转墨卡托坐标
* @param lng
* @param lat
* @return
*/
private static Map<String, Double> convertLL2MC(Double lng, Double lat) {
Double[] cE = null;
lng = getLoop(lng, -180, 180);
lat = getRange(lat, -74, 74);
for (int i = 0; i < LLBAND.length; i++) {
if (lat >= LLBAND[i]) {
cE = LL2MC[i];
break;
}
}
if (cE!=null) {
for (int i = LLBAND.length - 1; i >= 0; i--) {
if (lat <= -LLBAND[i]) {
cE = LL2MC[i];
break;
}
}
}
return converter(lng,lat, cE);
}
private static Map<String, Double> converter(Double x, Double y, Double[] cE) {
Double xTemp = cE[0] + cE[1] * Math.abs(x);
Double cC = Math.abs(y) / cE[9];
Double yTemp = cE[2] + cE[3] * cC + cE[4] * cC * cC + cE[5] * cC * cC * cC + cE[6] * cC * cC * cC * cC + cE[7] * cC * cC * cC * cC * cC + cE[8] * cC * cC * cC * cC * cC * cC;
xTemp *= (x < 0 ? -1 : 1);
yTemp *= (y < 0 ? -1 : 1);
Map<String, Double> location = new HashMap<String, Double>();
location.put("x", xTemp);
location.put("y", yTemp);
return location;
}
private static Double getLoop(Double lng, Integer min, Integer max) {
while (lng > max) {
lng -= max - min;
}
while (lng < min) {
lng += max - min;
}
return lng;
}
private static Double getRange(Double lat, Integer min, Integer max) {
if (min != null) {
lat = Math.max(lat, min);
}
if (max != null) {
lat = Math.min(lat, max);
}
return lat;
}
}
二 : 国际坐标即gps坐标、百度坐标、火星坐标转换
/**
* @ClassName: PositionUtil
* @Description:
* 各地图API坐标系统比较与转换;
* WGS84坐标系:即地球坐标系,国际上通用的坐标系。设备一般包含GPS芯片或者北斗芯片获取的经纬度为WGS84地理坐标系,
* 谷歌地图采用的是WGS84地理坐标系(中国范围除外);
* GCJ02坐标系:即火星坐标系,是由中国国家测绘局制订的地理信息系统的坐标系统。由WGS84坐标系经加密后的坐标系。
* 谷歌中国地图和搜搜中国地图采用的是GCJ02地理坐标系; BD09坐标系:即百度坐标系,GCJ02坐标系经加密后的坐标系;
* 搜狗坐标系、图吧坐标系等,估计也是在GCJ02基础上加密而成的。 chenhua
* @author: Max dou
* @date: 2017年4月10日 下午4:17:44
*/
public class PositionUtil {
public static final String BAIDU_LBS_TYPE = "bd09ll";
public static double pi = 3.1415926535897932384626;
public static double a = 6378245.0;
public static double ee = 0.00669342162296594323;
/**
* 国际坐标 to 火星坐标系 (GCJ-02) World Geodetic System ==> Mars Geodetic System
*
* @param lat
* @param lon
* @return
*/
public static Gps gps84_To_Gcj02(double lat, double lon) {
if (outOfChina(lat, lon)) {
return null;
}
double dLat = transformLat(lon - 105.0, lat - 35.0);
double dLon = transformLon(lon - 105.0, lat - 35.0);
double radLat = lat / 180.0 * pi;
double magic = Math.sin(radLat);
magic = 1 - ee * magic * magic;
double sqrtMagic = Math.sqrt(magic);
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
double mgLat = lat + dLat;
double mgLon = lon + dLon;
return new Gps(mgLat, mgLon);
}
/**
* * 火星坐标系 (GCJ-02) to 国际坐标 * * @param lon * @param lat * @return
* */
public static Gps gcj_To_Gps84(double lat, double lon) {
Gps gps = transform(lat, lon);
double lontitude = lon * 2 - gps.getWgLon();
double latitude = lat * 2 - gps.getWgLat();
return new Gps(latitude, lontitude);
}
/**
* 火星坐标系 (GCJ-02) to 百度坐标系 (BD-09) 的转换算法 将 GCJ-02 坐标转换成 BD-09 坐标
*
* @param gg_lat
* @param gg_lon
*/
public static Gps gcj02_To_Bd09(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.0065;
double bd_lat = z * Math.sin(theta) + 0.006;
return new Gps(bd_lat, bd_lon);
}
/**
* * 火星坐标系 (GCJ-02) to 百度坐标系 (BD-09) 的转换算法 * * 将 BD-09 坐标转换成GCJ-02 坐标 * * @param
* bd_lat * @param bd_lon * @return
*/
public static Gps bd09_To_Gcj02(double bd_lat, double bd_lon) {
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);
double gg_lon = z * Math.cos(theta);
double gg_lat = z * Math.sin(theta);
return new Gps(gg_lat, gg_lon);
}
/**
* 百度坐标系 (BD-09) to 国际坐标
* @param bd_lat
* @param bd_lon
* @return
*/
public static Gps bd09_To_Gps84(double bd_lat, double bd_lon) {
Gps gcj02 = PositionUtil.bd09_To_Gcj02(bd_lat, bd_lon);
Gps map84 = PositionUtil.gcj_To_Gps84(gcj02.getWgLat(),
gcj02.getWgLon());
return map84;
}
public static boolean outOfChina(double lat, double lon) {
if (lon < 72.004 || lon > 137.8347)
return true;
if (lat < 0.8293 || lat > 55.8271)
return true;
return false;
}
public static Gps transform(double lat, double lon) {
if (outOfChina(lat, lon)) {
return new Gps(lat, lon);
}
double dLat = transformLat(lon - 105.0, lat - 35.0);
double dLon = transformLon(lon - 105.0, lat - 35.0);
double radLat = lat / 180.0 * pi;
double magic = Math.sin(radLat);
magic = 1 - ee * magic * magic;
double sqrtMagic = Math.sqrt(magic);
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
double mgLat = lat + dLat;
double mgLon = lon + dLon;
return new Gps(mgLat, mgLon);
}
public static double transformLat(double x, double y) {
double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y
+ 0.2 * Math.sqrt(Math.abs(x));
ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
return ret;
}
public static double transformLon(double x, double y) {
double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1
* Math.sqrt(Math.abs(x));
ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0
* pi)) * 2.0 / 3.0;
return ret;
}
public static void main(String[] args) {
Gps gps = new Gps(40.084481,116.395412 );
System.out.println("gps :" + gps);
Gps bd = bd09_To_Gcj02(gps.getWgLat(), gps.getWgLon());
System.out.println("bd :" + bd);
}
}
/**
* @ClassName: Gps
* @Description: TODO
* @author: Max dou
* @date: 2017年4月10日 下午4:19:13
*/
public class Gps {
private double wgLat;
private double wgLon;
public Gps(double wgLat, double wgLon) {
setWgLat(wgLat);
setWgLon(wgLon);
}
public double getWgLat() {
return wgLat;
}
public void setWgLat(double wgLat) {
this.wgLat = wgLat;
}
public double getWgLon() {
return wgLon;
}
public void setWgLon(double wgLon) {
this.wgLon = wgLon;
}
@Override
public String toString() {
return wgLat + "," + wgLon;
}
}
三 : 利用百度地图api接口,坐标转换
参考链接:http://lbsyun.baidu.com/index.php?title=webapi/guide/changeposition
此接口为免费接口,不过每天有限制
四 : 利用百度地图api接口,根据坐标获取地理位置信息
参考链接:http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding
此接口为免费接口,不过每天有限制:
/**
* @ClassName: BaiDuUtil
* @Description: TODO
* @author: Max dou
* @date: 2017年4月10日 上午11:58:55
*/
public class BaiDuUtil {
public static String getCity(String lat, String lng) {
JSONObject objSrc = getLocationInfo(lat, lng);
System.out.println(objSrc.toString());
JSONObject obj = getLocationInfo(lat, lng).getJSONObject("result")
.getJSONObject("addressComponent");
return obj.getString("city");
}
public static JSONObject getLocationInfo(String lat, String lng) {
String url = "http://api.map.baidu.com/geocoder/v2/?location=" + lat
+ "," + lng + "&output=json&ak=" + "此处填写百度开放平台秘钥"
+ "&pois=0";
JSONObject obj = JSONObject.fromObject(HttpUtil.getRequest(url));
return obj;
}
public static void main(String[] args) {
System.out.println(BaiDuUtil.getCity("40.091637", "116.396764"));
}
}
五 : 阿里云接口 : 根据坐标获取地理位置信息
(此接口从网上找到的,已现在测试看没有限制条件)
需要json解析包:
net.sf.json-lib
json-lib
2.4
jdk15
import java.net.URL;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
* @ClassName: GetLocation
* @Description: TODO
* @author: Max dou
* @date: 2017年4月10日 下午1:53:42
*/
public class GetLocation {
public static void main(String[] args) {
String add = getAdd("110.303959", "23.548133");
JSONObject jsonObject = JSONObject.fromObject(add);
JSONArray jsonArray = JSONArray.fromObject(jsonObject.getString("addrList"));
JSONObject j_2 = JSONObject.fromObject(jsonArray.get(1));
JSONObject j_1 = JSONObject.fromObject(jsonArray.get(0));
String allAdd = j_2.getString("admName");
String arr[] = allAdd.split(",");
System.out.println("省:"+arr[0]+"\n市:"+arr[1]+"\n区:"+arr[2]+"\n路:"+j_1.getString("name")+"\n详细地址:"+j_2.getString("name"));
}
public static String getAdd(String log, String lat ){
//lat 小 log 大 ,经过测试,数据的经纬度为国家坐标即火星坐标
//参数解释: 纬度,经度 type 001 (100代表道路,010代表POI,001代表门址,111可以同时显示前三项)
String urlString = "http://gc.ditu.aliyun.com/regeocoding?l="+lat+","+log+"&type=111";
String res = "";
try {
URL url = new URL(urlString);
java.net.HttpURLConnection conn = (java.net.HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream(),"UTF-8"));
String line;
while ((line = in.readLine()) != null) {
res += line+"\n";
}
in.close();
} catch (Exception e) {
System.out.println("error in wapaction,and e is " + e.getMessage());
}
System.out.println(res);
return res;
}
}
如有什么问题,请大家指正,谢谢!