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