1.分不清的坐標(biāo)系
WG-S84: 地理坐標(biāo)系統(tǒng),GPS儀器記錄的經(jīng)緯度信息,Google Earth采用,Google Map中國范圍外使用,高德地圖中國范圍外使用。 GCJ-02: 投影坐標(biāo)系統(tǒng),火星坐標(biāo)系,中國國家測繪局制定的坐標(biāo)系統(tǒng),由WGS-84加密后的坐標(biāo)。Google中國和搜搜地圖,arcgis地圖,高德地圖 BD-09: 投影坐標(biāo)系統(tǒng),百度坐標(biāo),GCJ-02加密后的坐標(biāo)系,只適用于百度地圖
(在國內(nèi)是不允許直接用WGS84坐標(biāo)系標(biāo)注的,必須經(jīng)過加密后才能用。必須至少使用GCJ-02坐標(biāo)系,或者使用在GCJ-02加密后再進行加密的坐標(biāo)系,如百度坐標(biāo)系)
其他:搜狗地圖:搜狗坐標(biāo)系,圖吧:圖吧坐標(biāo)等,估計也是在GCJ02基礎(chǔ)上加密而成的,這里暫不涉及
2.直接上代碼文章來源:http://www.zghlxwxcb.cn/news/detail-769862.html
package ft.util.gps;
// 導(dǎo)入Proj4J庫
import org.locationtech.proj4j.*;
import org.locationtech.proj4j.io.Proj4FileReader;
import java.io.IOException;
/**
* 百度坐標(biāo)(BD09)、國測局坐標(biāo)(火星坐標(biāo),GCJ02)、和WGS84坐標(biāo)系之間的轉(zhuǎn)換的工具
* <p>
* 參考 https://github.com/wandergis/coordtransform 實現(xiàn)的Java版本
*
* @author geosmart
*/
public class CoordinateTransformUtil {
static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
// π
static double pi = 3.1415926535897932384626;
// 長半軸
static double a = 6378245.0;
// 扁率
static double ee = 0.00669342162296594323;
/**
* 百度坐標(biāo)系(BD-09)轉(zhuǎn)WGS坐標(biāo)
*
* @param lng 百度坐標(biāo)緯度
* @param lat 百度坐標(biāo)經(jīng)度
* @return WGS84坐標(biāo)數(shù)組
*/
public static PointXY bd09towgs84(double lng, double lat) {
PointXY gcj = bd09togcj02(lng, lat);
PointXY wgs84 = gcj02towgs84(gcj.getLon(), gcj.getLat());
return wgs84;
}
/**
* WGS坐標(biāo)轉(zhuǎn)百度坐標(biāo)系(BD-09)
*
* @param lng WGS84坐標(biāo)系的經(jīng)度
* @param lat WGS84坐標(biāo)系的緯度
* @return 百度坐標(biāo)數(shù)組
*/
public static PointXY wgs84tobd09(double lng, double lat) {
PointXY gcj = wgs84togcj02(lng, lat);
PointXY bd09 = gcj02tobd09(gcj.getLon(), gcj.getLat());
return bd09;
}
/**
* 火星坐標(biāo)系(GCJ-02)轉(zhuǎn)百度坐標(biāo)系(BD-09)
* <p>
* 谷歌、高德——>百度
*
* @param lng 火星坐標(biāo)經(jīng)度
* @param lat 火星坐標(biāo)緯度
* @return 百度坐標(biāo)數(shù)組
*/
public static PointXY gcj02tobd09(double lng, double lat) {
double z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * x_pi);
double theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * x_pi);
double bd_lng = z * Math.cos(theta) + 0.0065;
double bd_lat = z * Math.sin(theta) + 0.006;
return new PointXY(bd_lng, bd_lat);
}
/**
* 百度坐標(biāo)系(BD-09)轉(zhuǎn)火星坐標(biāo)系(GCJ-02)
* <p>
* 百度——>谷歌、高德
*
* @param bd_lon 百度坐標(biāo)緯度
* @param bd_lat 百度坐標(biāo)經(jīng)度
* @return 火星坐標(biāo)數(shù)組
*/
public static PointXY bd09togcj02(double bd_lon, double bd_lat) {
double x = bd_lon - 0.0065;
double 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);
double gg_lng = z * Math.cos(theta);
double gg_lat = z * Math.sin(theta);
return new PointXY(gg_lng, gg_lat);
}
/**
* WGS84轉(zhuǎn)GCJ02(火星坐標(biāo)系)
*
* @param lng WGS84坐標(biāo)系的經(jīng)度
* @param lat WGS84坐標(biāo)系的緯度
* @return 火星坐標(biāo)數(shù)組
*/
public static PointXY wgs84togcj02(double lng, double lat) {
if (out_of_china(lng, lat)) {
return new PointXY(lng, lat);
}
double dlat = transformlat(lng - 105.0, lat - 35.0);
double dlng = transformlng(lng - 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);
dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * pi);
double mglat = lat + dlat;
double mglng = lng + dlng;
return new PointXY(mglng, mglat);
}
/**
* GCJ02(火星坐標(biāo)系)轉(zhuǎn)GPS84
*
* @param lng 火星坐標(biāo)系的經(jīng)度
* @param lat 火星坐標(biāo)系緯度
* @return WGS84坐標(biāo)數(shù)組
*/
public static PointXY gcj02towgs84(double lng, double lat) {
if (out_of_china(lng, lat)) {
return new PointXY(lng, lat);
}
double dlat = transformlat(lng - 105.0, lat - 35.0);
double dlng = transformlng(lng - 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);
dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * pi);
double mglat = lat + dlat;
double mglng = lng + dlng;
return new PointXY(lng * 2 - mglng, lat * 2 - mglat);
}
/**
* 緯度轉(zhuǎn)換
*
* @param lng
* @param lat
* @return
*/
public static double transformlat(double lng, double lat) {
double ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(lng));
ret += (20.0 * Math.sin(6.0 * lng * pi) + 20.0 * Math.sin(2.0 * lng * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(lat * pi) + 40.0 * Math.sin(lat / 3.0 * pi)) * 2.0 / 3.0;
ret += (160.0 * Math.sin(lat / 12.0 * pi) + 320 * Math.sin(lat * pi / 30.0)) * 2.0 / 3.0;
return ret;
}
/**
* 經(jīng)度轉(zhuǎn)換
*
* @param lng
* @param lat
* @return
*/
public static double transformlng(double lng, double lat) {
double ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng));
ret += (20.0 * Math.sin(6.0 * lng * pi) + 20.0 * Math.sin(2.0 * lng * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(lng * pi) + 40.0 * Math.sin(lng / 3.0 * pi)) * 2.0 / 3.0;
ret += (150.0 * Math.sin(lng / 12.0 * pi) + 300.0 * Math.sin(lng / 30.0 * pi)) * 2.0 / 3.0;
return ret;
}
/**
* 判斷是否在國內(nèi),不在國內(nèi)不做偏移
*
* @param lng
* @param lat
* @return
*/
public static boolean out_of_china(double lng, double lat) {
if (lng < 72.004 || lng > 137.8347) {
return true;
} else if (lat < 0.8293 || lat > 55.8271) {
return true;
}
return false;
}
public static PointXY wgs2cgcg(double lon, double lat) {
Proj4FileReader proj4FileReader = new Proj4FileReader();
String[] paramStr = new String[0];
try {
paramStr = proj4FileReader.readParametersFromFile("epsg","4528");
} catch (IOException e) {
e.printStackTrace();
}
CRSFactory targetFactory = new CRSFactory();
//目標(biāo)坐標(biāo)系統(tǒng)
CoordinateReferenceSystem cgcs = targetFactory.createFromParameters("4528", paramStr);
//源坐標(biāo)系統(tǒng)
CRSFactory crsFactory = new CRSFactory();
String wgs84_param = "+title=long/lat:WGS84 +proj=longlat +ellps=WGS84 +datum=WGS84 +units=degress";
CoordinateReferenceSystem wgs84 = crsFactory.createFromParameters("WGS84", wgs84_param);
CoordinateTransformFactory ctf = new CoordinateTransformFactory();
CoordinateTransform transform = ctf.createTransform(wgs84, cgcs);
ProjCoordinate projCoordinate = new ProjCoordinate(lon,lat);
transform.transform(projCoordinate,projCoordinate);
return new PointXY(projCoordinate.x,projCoordinate.y);
}
public static PointXY cgcg2wgs(double lon, double lat) {
Proj4FileReader proj4FileReader = new Proj4FileReader();
String[] paramStr = new String[0];
try {
paramStr = proj4FileReader.readParametersFromFile("epsg","4528");
} catch (IOException e) {
e.printStackTrace();
}
CRSFactory targetFactory = new CRSFactory();
//目標(biāo)坐標(biāo)系統(tǒng)
CoordinateReferenceSystem cgcs = targetFactory.createFromParameters("4528", paramStr);
//源坐標(biāo)系統(tǒng)
CRSFactory crsFactory = new CRSFactory();
String wgs84_param = "+title=long/lat:WGS84 +proj=longlat +ellps=WGS84 +datum=WGS84 +units=degress";
CoordinateReferenceSystem wgs84 = crsFactory.createFromParameters("WGS84", wgs84_param);
CoordinateTransformFactory ctf = new CoordinateTransformFactory();
CoordinateTransform transform = ctf.createTransform(cgcs,wgs84);
ProjCoordinate projCoordinate = new ProjCoordinate(lon,lat);
transform.transform(projCoordinate,projCoordinate);
return new PointXY(projCoordinate.x,projCoordinate.y);
}
public static void main(String[] args) {
// 坐標(biāo)轉(zhuǎn)化api
// https://tool.lu/coordinate/?ivk_sa=1024320u
//
System.out.println(wgs84togcj02(118.840344,31.99335)); // ok
System.out.println(gcj02towgs84(107.80473041151717, 29.33190938671697)); // ok
System.out.println(gcj02tobd09(107.80473041151717, 29.33190938671697)); // ok
System.out.println(wgs84tobd09(107.8003647, 29.3347596)); // ok
System.out.println(bd09togcj02(107.81129990277971, 29.337686168994296));
System.out.println(bd09towgs84(107.81129990277971, 29.337686168994296));
System.out.println(wgs2cgcg(118.70331743312532,32.103750473727125));
// System.out.println(bd09towgs84(120.24794,29.72979));
//gcj02towgs84
// System.out.println(gcj02tobd09(120.236701, 29.726141));
//
PointXY p = wgs2cgcg(118.70331743312532,32.103750473727125);
System.out.println(p);
System.out.println(cgcg2wgs(p.getLon(), p.getLat()));
}
}`在這里插入代碼片`
測試結(jié)果
lon:118.84543348019479,lat:31.99121178083339
lon:107.80037405167121,lat:29.334768526991336
lon:107.81129968211874,lat:29.337686107444082
lon:107.81129968211874,lat:29.337686107444082
lon:107.80473019794286,lat:29.33191023473475
lon:107.80037383758244,lat:29.33476937412017
lon:4.0377605981347054E7,lat:3554093.2140468676
lon:4.0377605981347054E7,lat:3554093.2140468676
lon:118.70331743312477,lat:32.10375047372694文章來源地址http://www.zghlxwxcb.cn/news/detail-769862.html
到了這里,關(guān)于Java轉(zhuǎn)換坐標(biāo)系,GPS(WGS84)大地200(CGCS200)、百度(BD-09)、高德(GCJ-02)互轉(zhuǎn),一文搞懂坐標(biāo)系、坐標(biāo)轉(zhuǎn)換的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!