Android开发之火星坐标转换工具

代码:

  1 import java.io.InputStream;
  2 import java.io.ObjectInputStream;
  3 
  4 /*
  5  * 把获取到的真实地址转换为火星坐标
  6  */
  7 public class LocationUtils {
  8 
  9     public static double[] standardToChina(Double x, Double y) {
 10 
 11         try {
 12             double[] haha = new double[2];
 13             PointDouble pointDouble = new PointDouble(x, y);
 14             PointDouble s2c = ModifyOffset.getInstance(
 15                     ModifyOffset.class.getResourceAsStream("axisoffset.dat"))
 16                     .s2c(pointDouble);
 17             haha[0] = s2c.x;
 18             haha[1] = s2c.y;
 19             return haha;
 20         } catch ( Exception e ) {
 21             e.printStackTrace();
 22         }
 23 
 24         return null;
 25 
 26     }
 27 
 28     static class ModifyOffset {
 29         private static ModifyOffset modifyOffset;
 30         static double[] X = new double[660 * 450];
 31         static double[] Y = new double[660 * 450];
 32 
 33         private ModifyOffset(InputStream inputStream) throws Exception {
 34             init(inputStream);
 35         }
 36 
 37         public synchronized static ModifyOffset getInstance(InputStream is)
 38                 throws Exception {
 39             if ( modifyOffset == null ) {
 40                 modifyOffset = new ModifyOffset(is);
 41             }
 42             return modifyOffset;
 43         }
 44 
 45         public void init(InputStream inputStream) throws Exception {
 46             ObjectInputStream in = new ObjectInputStream(inputStream);
 47             try {
 48                 int i = 0;
 49                 while ( in.available() > 0 ) {
 50                     if ( (i & 1) == 1 ) {
 51                         Y[(i - 1) >> 1] = in.readInt() / 100000.0d;
 52                         ;
 53                     } else {
 54                         X[i >> 1] = in.readInt() / 100000.0d;
 55                         ;
 56                     }
 57                     i++;
 58                 }
 59             } finally {
 60                 if ( in != null )
 61                     in.close();
 62             }
 63         }
 64 
 65         // standard -> china
 66         public PointDouble s2c(PointDouble pt) {
 67             int cnt = 10;
 68             double x = pt.x, y = pt.y;
 69             while ( cnt-- > 0 ) {
 70                 if ( x < 71.9989d || x > 137.8998d || y < 9.9997d || y > 54.8996d )
 71                     return pt;
 72                 int ix = (int) (10.0d * (x - 72.0d));
 73                 int iy = (int) (10.0d * (y - 10.0d));
 74                 double dx = (x - 72.0d - 0.1d * ix) * 10.0d;
 75                 double dy = (y - 10.0d - 0.1d * iy) * 10.0d;
 76                 x = (x + pt.x + (1.0d - dx) * (1.0d - dy) * X[ix + 660 * iy] + dx
 77                         * (1.0d - dy) * X[ix + 660 * iy + 1] + dx * dy
 78                         * X[ix + 660 * iy + 661] + (1.0d - dx) * dy
 79                         * X[ix + 660 * iy + 660] - x) / 2.0d;
 80                 y = (y + pt.y + (1.0d - dx) * (1.0d - dy) * Y[ix + 660 * iy] + dx
 81                         * (1.0d - dy) * Y[ix + 660 * iy + 1] + dx * dy
 82                         * Y[ix + 660 * iy + 661] + (1.0d - dx) * dy
 83                         * Y[ix + 660 * iy + 660] - y) / 2.0d;
 84             }
 85             return new PointDouble(x, y);
 86         }
 87 
 88         // china -> standard
 89         public PointDouble c2s(PointDouble pt) {
 90             int cnt = 10;
 91             double x = pt.x, y = pt.y;
 92             while ( cnt-- > 0 ) {
 93                 if ( x < 71.9989d || x > 137.8998d || y < 9.9997d || y > 54.8996d )
 94                     return pt;
 95                 int ix = (int) (10.0d * (x - 72.0d));
 96                 int iy = (int) (10.0d * (y - 10.0d));
 97                 double dx = (x - 72.0d - 0.1d * ix) * 10.0d;
 98                 double dy = (y - 10.0d - 0.1d * iy) * 10.0d;
 99                 x = (x + pt.x - (1.0d - dx) * (1.0d - dy) * X[ix + 660 * iy] - dx
100                         * (1.0d - dy) * X[ix + 660 * iy + 1] - dx * dy
101                         * X[ix + 660 * iy + 661] - (1.0d - dx) * dy
102                         * X[ix + 660 * iy + 660] + x) / 2.0d;
103                 y = (y + pt.y - (1.0d - dx) * (1.0d - dy) * Y[ix + 660 * iy] - dx
104                         * (1.0d - dy) * Y[ix + 660 * iy + 1] - dx * dy
105                         * Y[ix + 660 * iy + 661] - (1.0d - dx) * dy
106                         * Y[ix + 660 * iy + 660] + y) / 2.0d;
107             }
108             return new PointDouble(x, y);
109         }
110 
111 
112     }
113 
114     static class PointDouble {
115         double x, y;
116 
117         PointDouble(double x, double y) {
118             this.x = x;
119             this.y = y;
120         }
121 
122         public String toString() {
123             return "x=" + x + ", y=" + y;
124         }
125     }
126 
127 }

 

转载于:https://www.cnblogs.com/liyiran/p/5325283.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.map; public class GPSConverterUtils { 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; /** * 84 to 火星坐标系 (GCJ-02) World Geodetic System ==> Mars Geodetic System * 天地图 转 火星 * @param lat * @param lon */ 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 84 * * @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.getLon(); double latitude = lat * 2 - gps.getLat(); return new GPS(latitude, lontitude); } /** * 火星坐标系 (GCJ-02) 与百度坐标系 (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;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值