【四二学堂】标准GPS坐标,转换成百度坐标
生活随笔
收集整理的這篇文章主要介紹了
【四二学堂】标准GPS坐标,转换成百度坐标
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
百度坐標(biāo)反查地址:http://api.map.baidu.com/lbsapi/getpoint/
工具類如下:
?
import org.apache.commons.lang3.StringUtils;public class TransformPosUtils {private static double PI = Math.PI;private static double AXIS = 6378245.0; //private static double OFFSET = 0.00669342162296594323; //(a^2 - b^2) / a^2private static double X_PI = PI * 3000.0 / 180.0;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 double[] delta(double wgLat, double wgLon) {double[] latlng = new double[2];double dLat = transformLat(wgLon - 105.0, wgLat - 35.0);double dLon = transformLon(wgLon - 105.0, wgLat - 35.0);double radLat = wgLat / 180.0 * PI;double magic = Math.sin(radLat);magic = 1 - OFFSET * magic * magic;double sqrtMagic = Math.sqrt(magic);dLat = (dLat * 180.0) / ((AXIS * (1 - OFFSET)) / (magic * sqrtMagic) * PI);dLon = (dLon * 180.0) / (AXIS / sqrtMagic * Math.cos(radLat) * PI);latlng[0] = dLat;latlng[1] = dLon;return latlng;}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;}//GCJ-02=>BD09 火星坐標(biāo)系=>百度坐標(biāo)系public static double[] gcj2BD09(double glat, double glon) {double x = glon;double y = glat;double[] latlon = new double[2];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);latlon[0] = z * Math.sin(theta) + 0.006;latlon[1] = z * Math.cos(theta) + 0.0065;return latlon;}// WGS84=》GCJ02 地球坐標(biāo)系=>火星坐標(biāo)系public static double[] wgs2GCJ(double wgLat, double wgLon) {double[] latlon = new double[2];if (outOfChina(wgLat, wgLon)) {latlon[0] = wgLat;latlon[1] = wgLon;return latlon;}double[] deltaD = delta(wgLat, wgLon);latlon[0] = wgLat + deltaD[0];latlon[1] = wgLon + deltaD[1];return latlon;}// WGS84=》BD09 地球坐標(biāo)系=>百度坐標(biāo)系public static double[] wgs2BD09(double wgLat, double wgLon) {double[] latlon = wgs2GCJ(wgLat, wgLon);return gcj2BD09(latlon[0], latlon[1]);}public static String transformPos(String jwd) {if (StringUtils.isNotEmpty(jwd) && (jwd.contains("°"))) {//如果不為空并且存在度單位//計(jì)算前進(jìn)行數(shù)據(jù)處理jwd = jwd.replace("E", "").replace("N", "").replace(":", "").replace(":", "");double d = 0, m = 0, s = 0;d = Double.parseDouble(jwd.split("°")[0]);//不同單位的分,可擴(kuò)展if (jwd.contains("′")) {//正常的′m = Double.parseDouble(jwd.split("°")[1].split("′")[0]);} else if (jwd.contains("'")) {//特殊的'm = Double.parseDouble(jwd.split("°")[1].split("'")[0]);}//不同單位的秒,可擴(kuò)展if (jwd.contains("″")) {//正常的″//有時(shí)候沒(méi)有分 如:112°10.25″s = jwd.contains("′") ? Double.parseDouble(jwd.split("′")[1].split("″")[0]) : Double.parseDouble(jwd.split("°")[1].split("″")[0]);} else if (jwd.contains("''")) {//特殊的''//有時(shí)候沒(méi)有分 如:112°10.25''s = jwd.contains("'") ? Double.parseDouble(jwd.split("'")[1].split("''")[0]) : Double.parseDouble(jwd.split("°")[1].split("''")[0]);}jwd = String.valueOf(d + m / 60 + s / 60 / 60);//計(jì)算并轉(zhuǎn)換為string//使用BigDecimal進(jìn)行加減乘除/*BigDecimal bd = new BigDecimal("60");BigDecimal d = new BigDecimal(jwd.contains("°")?jwd.split("°")[0]:"0");BigDecimal m = new BigDecimal(jwd.contains("′")?jwd.split("°")[1].split("′")[0]:"0");BigDecimal s = new BigDecimal(jwd.contains("″")?jwd.split("′")[1].split("″")[0]:"0");//divide相除可能會(huì)報(bào)錯(cuò)(無(wú)限循環(huán)小數(shù)),要設(shè)置保留小數(shù)點(diǎn)jwd = String.valueOf(d.add(m.divide(bd,6,BigDecimal.ROUND_HALF_UP).add(s.divide(bd.multiply(bd),6,BigDecimal.ROUND_HALF_UP))));*/}return jwd;}public static double[] transformLatAndLng(String lat,String lng) {String sLat = transformPos(lat);String sLng = transformPos(lng);double[] doubles = wgs2BD09(Double.valueOf(sLat), Double.valueOf(sLng));//System.out.println(doubles[0] + "," + doubles[1]);return doubles;}public static void main(String[] args) {String latitude = "39564888"; //緯度String longitude = "116333641";double[] doubles = transformLatAndLng(StringUtils.substring(latitude, 0, 2) + "°" +StringUtils.substring(latitude, 2, 4) + "." + StringUtils.substring(latitude, 4, latitude.length()) + "′",StringUtils.substring(longitude, 0, 3) + "°" +StringUtils.substring(longitude, 3, 5) + "." + StringUtils.substring(longitude, 5, longitude.length()) + "′");System.out.println(doubles[0] + "," + doubles[1]);} }轉(zhuǎn)換結(jié)果是小數(shù)點(diǎn)后十四位的百度經(jīng)緯度,可以通過(guò)百度坐標(biāo)反差網(wǎng)址,查出位置信息
總結(jié)
以上是生活随笔為你收集整理的【四二学堂】标准GPS坐标,转换成百度坐标的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: B站首页仿写
- 下一篇: 美团(2) - 实战准备