diff --git a/modules/mogo-module-common/src/main/java/com/mogo/module/common/utils/CoordinateSystemTransformationUtil.java b/modules/mogo-module-common/src/main/java/com/mogo/module/common/utils/CoordinateSystemTransformationUtil.java new file mode 100644 index 0000000000..ce46444fbe --- /dev/null +++ b/modules/mogo-module-common/src/main/java/com/mogo/module/common/utils/CoordinateSystemTransformationUtil.java @@ -0,0 +1,88 @@ +package com.mogo.module.common.utils; + +import java.math.BigDecimal; + +/** + * 坐标系转化 + * + * @author linyang + * @since 2020.10.21 + */ +public class CoordinateSystemTransformationUtil { + + private static final double x_PI = 52.35987755982988D; + private static final double PI = 3.141592653589793D; + private static final double a = 6378245.0D; + private static final double ee = 0.006693421622965943D; + + public static final boolean outOfChina( double lat, double lng ) { + return lng <= 73.66D || lng >= 135.05D || lat <= 3.86D || lat >= 53.55D; + } + + private static final double transformLat( double lng, double lat ) { + double ret = -100.0D + 2.0D * lng + 3.0D * lat + 0.2D * lat * lat + 0.1D * lng * lat + 0.2D * Math.sqrt( Math.abs( lng ) ); + ret += ( 20.0D * Math.sin( 6.0D * lng * 3.141592653589793D ) + 20.0D * Math.sin( 2.0D * lng * 3.141592653589793D ) ) * 2.0D / 3.0D; + ret += ( 20.0D * Math.sin( lat * 3.141592653589793D ) + 40.0D * Math.sin( lat / 3.0D * 3.141592653589793D ) ) * 2.0D / 3.0D; + ret += ( 160.0D * Math.sin( lat / 12.0D * 3.141592653589793D ) + ( double ) 320 * Math.sin( lat * 3.141592653589793D / 30.0D ) ) * 2.0D / 3.0D; + return ret; + } + + private static final double transformLon( double lng, double lat ) { + double ret = 300.0D + lng + 2.0D * lat + 0.1D * lng * lng + 0.1D * lng * lat + 0.1D * Math.sqrt( Math.abs( lng ) ); + ret += ( 20.0D * Math.sin( 6.0D * lng * 3.141592653589793D ) + 20.0D * Math.sin( 2.0D * lng * 3.141592653589793D ) ) * 2.0D / 3.0D; + ret += ( 20.0D * Math.sin( lng * 3.141592653589793D ) + 40.0D * Math.sin( lng / 3.0D * 3.141592653589793D ) ) * 2.0D / 3.0D; + ret += ( 150.0D * Math.sin( lng / 12.0D * 3.141592653589793D ) + 300.0D * Math.sin( lng / 30.0D * 3.141592653589793D ) ) * 2.0D / 3.0D; + return ret; + } + + public static final double[] transformWgsToGcj( double wgLat, double wgLon ) { + double[] point = new double[2]; + if ( outOfChina( wgLat, wgLon ) ) { + point[0] = wgLon; + point[1] = wgLat; + return point; + } else { + double dLat = transformLat( wgLon - 105.0D, wgLat - 35.0D ); + double dLon = transformLon( wgLon - 105.0D, wgLat - 35.0D ); + double radLat = wgLat / 180.0D * 3.141592653589793D; + double magic = Math.sin( radLat ); + magic = ( double ) 1 - 0.006693421622965943D * magic * magic; + double sqrtMagic = Math.sqrt( magic ); + dLat = dLat * 180.0D / ( 6335552.717000426D / ( magic * sqrtMagic ) * 3.141592653589793D ); + dLon = dLon * 180.0D / ( 6378245.0D / sqrtMagic * Math.cos( radLat ) * 3.141592653589793D ); + double mgLat = wgLat + dLat; + double mgLon = wgLon + dLon; + point[0] = dealRound( mgLon ); + point[1] = dealRound( mgLat ); + return point; + } + } + + public static final double[] transformGcj02toWgs84( double lat, double lng ) { + double[] var10000; + if ( outOfChina( lat, lng ) ) { + var10000 = new double[]{lng, lat}; + } else { + double dlat = transformLat( lng - 105.0D, lat - 35.0D ); + double dlng = transformLon( lng - 105.0D, lat - 35.0D ); + double radlat = lat / 180.0D * 3.141592653589793D; + double magic = Math.sin( radlat ); + magic = ( double ) 1 - 0.006693421622965943D * magic * magic; + double sqrtmagic = Math.sqrt( magic ); + dlat = dlat * 180.0D / ( 6335552.717000426D / ( magic * sqrtmagic ) * 3.141592653589793D ); + dlng = dlng * 180.0D / ( 6378245.0D / sqrtmagic * Math.cos( radlat ) * 3.141592653589793D ); + double mglat = lat + dlat; + double mglng = lng + dlng; + var10000 = new double[]{dealRound( lng * ( double ) 2 - mglng ), dealRound( lat * ( double ) 2 - mglat )}; + } + + return var10000; + } + + private static final double dealRound( double value ) { + BigDecimal bg = new BigDecimal( value ); + double result = bg.setScale( 6, 4 ).doubleValue(); + return result; + } + +} diff --git a/modules/mogo-module-common/src/main/java/com/mogo/module/common/utils/CoordinateUtils.java b/modules/mogo-module-common/src/main/java/com/mogo/module/common/utils/CoordinateUtils.java new file mode 100644 index 0000000000..a12b86d42e --- /dev/null +++ b/modules/mogo-module-common/src/main/java/com/mogo/module/common/utils/CoordinateUtils.java @@ -0,0 +1,97 @@ +package com.mogo.module.common.utils; + +import com.amap.api.maps.model.LatLng; + +/** + * @author donghongyu + */ +public class CoordinateUtils { + + private static double a = 6378245.0; + private static double ee = 0.00669342162296594323; + + /** + * 手机GPS坐标转火星坐标 + * + * @param wgLoc + * @return + */ + public static double[] transformFromWGSToGCJ( double lat, double lon ) { + + //如果在国外,则默认不进行转换 + if ( outOfChina( lat, lon ) ) { + return new double[]{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 * Math.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 ) * Math.PI ); + dLon = ( dLon * 180.0 ) / ( a / sqrtMagic * Math.cos( radLat ) * Math.PI ); + + return new double[]{lat + dLat, lon + dLon}; + } + + /** + * 手机GPS坐标转火星坐标 + * + * @param wgLoc + * @return + */ + public static LatLng transformFromWGSToGCJ( LatLng wgLoc ) { + + //如果在国外,则默认不进行转换 + if ( outOfChina( wgLoc.latitude, wgLoc.longitude ) ) { + return new LatLng( wgLoc.latitude, wgLoc.longitude ); + } + double dLat = transformLat( wgLoc.longitude - 105.0, + wgLoc.latitude - 35.0 ); + double dLon = transformLon( wgLoc.longitude - 105.0, + wgLoc.latitude - 35.0 ); + double radLat = wgLoc.latitude / 180.0 * Math.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 ) * Math.PI ); + dLon = ( dLon * 180.0 ) / ( a / sqrtMagic * Math.cos( radLat ) * Math.PI ); + + return new LatLng( wgLoc.latitude + dLat, wgLoc.longitude + dLon ); + } + + 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( x > 0 ? x : -x ); + ret += ( 20.0 * Math.sin( 6.0 * x * Math.PI ) + 20.0 * Math.sin( 2.0 * x + * Math.PI ) ) * 2.0 / 3.0; + ret += ( 20.0 * Math.sin( y * Math.PI ) + 40.0 * Math.sin( y / 3.0 + * Math.PI ) ) * 2.0 / 3.0; + ret += ( 160.0 * Math.sin( y / 12.0 * Math.PI ) + 320 * Math.sin( y + * Math.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( x > 0 ? x : -x ); + ret += ( 20.0 * Math.sin( 6.0 * x * Math.PI ) + 20.0 * Math.sin( 2.0 * x + * Math.PI ) ) * 2.0 / 3.0; + ret += ( 20.0 * Math.sin( x * Math.PI ) + 40.0 * Math.sin( x / 3.0 + * Math.PI ) ) * 2.0 / 3.0; + ret += ( 150.0 * Math.sin( x / 12.0 * Math.PI ) + 300.0 * Math.sin( x + / 30.0 * Math.PI ) ) * 2.0 / 3.0; + return ret; + } + + 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; + } + +}