火星坐标与地球坐标的转换,以及地球上两点之间的距离计算

火星坐标与地球坐标的转换,以及地球上两点之间的距离计算

  1. 地球坐标就是我们通过GPS获得的GPS坐标,而我们所谓的火星坐标,就是经过国家测绘局进行加密后的坐标(WGS-84 ),而这个火星坐标只在国内有效。

下面这个函数用来判断坐标是否属于中国境内,但是这个坐标判断不是太准确,只是大概判断。

        bool 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;
        }
  1. 地球坐标转火星坐标,
        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 * System.Math.Sqrt(System.Math.Abs(x));
            ret += (20.0 * System.Math.Sin(6.0 * x * pi) + 20.0 * System.Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
            ret += (20.0 * System.Math.Sin(y * pi) + 40.0 * System.Math.Sin(y /3.0 * pi)) * 2.0 / 3.0;
            ret += (160.0 * System.Math.Sin(y / 12.0 * pi) + 320 * System.Math.Sin(y * pi / 30.0)) * 2.0 / 3.0;
            return ret;
        }

        double transformLon(double x, double y)
        {
            double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * System.Math.Sqrt(System.Math.Abs(x));
            ret += (20.0 * System.Math.Sin(6.0 * x * pi) + 20.0 * System.Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
            ret += (20.0 * System.Math.Sin(x * pi) + 40.0 * System.Math.Sin(x / 3.0 * pi)) * 2.0 / 3.0;
            ret += (150.0 * System.Math.Sin(x / 12.0 * pi) + 300.0 * System.Math.Sin(x / 30.0 * pi)) * 2.0 / 3.0;
            return ret;
        }  


 /** 
     * 地球坐标转换为火星坐标 
     * World Geodetic System ==> Mars Geodetic System 
     * 
     * @param wgLat  地球坐标 
     * @param wgLon 
     * 
     * mglat,mglon 火星坐标 
     */
    void transform2Mars(double wgLat, double wgLon, out double mgLat, out double mgLon)  
    {
        if (outOfChina(wgLat, wgLon) )  
        {  
            mgLat  = wgLat;  
            mgLon = wgLon;  
            return ;  
        }  
        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 = System.Math.Sin(radLat);  
        magic = 1 - ee * magic * magic;  
        double sqrtMagic = System.Math.Sqrt(magic);  
        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);  
        dLon = (dLon * 180.0) / (a / sqrtMagic * System.Math.Cos(radLat) * pi);  
        mgLat = wgLat + dLat;  
        mgLon = wgLon + dLon;  
    }
  1. 火星坐标转地球坐标
public PointLatLng Mar2Earth(PointLatLng Mar) {
         PointLatLng Earth = new PointLatLng();
         if (outOfChina(Mar.Lat, Mar.Lng))
         {
            return Mar;
         }
        PointLatLng tmp;
        double initDelta = 0.1;
        double threshold = 0.000001;
        double dLat = initDelta, dLon = initDelta;
        double mLat = Mar.Lat - dLat, mLon = Mar.Lng - dLon;
        double pLat = Mar.Lat + dLat, pLon = Mar.Lng + dLon;
        double wgsLat, wgsLon, i = 0;
        double lastDlat = initDelta, lastDlng = initDelta;
        PointLatLng lasttmp = new PointLatLng((mLat + pLat) / 2, (mLon + pLon) / 2);
        PointLatLng minitemp=new PointLatLng();

        while (true) {
            wgsLat = (mLat + pLat) / 2;
            wgsLon = (mLon + pLon) / 2;
transform2Mars(wgsLat,wgsLon,tmp.Lat.tmp.lng);

            dLat = tmp.Lat - Mar.Lat;
            dLon = tmp.Lng - Mar.Lng;

            if ((System.Math.Abs(dLat) < threshold) && (System.Math.Abs(dLon) < threshold))
                break;

            if (dLat + dLon >= lastDlat + lastDlng)
            {
            }
            else {
                lastDlat = dLat;
                lastDlng = dLon;

                minitemp.Lat = tmp.Lat;
                minitemp.Lng = tmp.Lng;
            }

            if (lasttmp.Lat == tmp.Lat && lasttmp.Lng == tmp.Lng) {
                wgsLat = minitemp.Lat;
                wgsLon = minitemp.Lng;

                break;
            }
            lasttmp = tmp;

            if (dLat > 0) pLat = wgsLat; else mLat = wgsLat;
            if (dLon > 0) pLon = wgsLon; else mLon = wgsLon;


           if (++i > 10000) break;
        }

        if(i>=10000){
            return new PointLatLng();
        }
        Earth.Lat = wgsLat;
        Earth.Lng = wgsLon;
        return Earth;
     }

火星坐标转地球坐标这个函数其实是利用地球坐标转火星坐标方法使用二分法求得,精度为:threshold = 0.000001

  1. 求地球上两点之间的距离
      public  double Distance(double lat1, double lon1, double lat2, double lon2)
        {
            double latitude1, longitude1, latitude2, longitude2;
            double dlat, dlon;
            latitude1 = lat1;
            longitude1 = lon1;
            latitude2 = lat2;
            longitude2 = lon2;
            //computing procedure  
            double a, c, distance;
            dlon = System.Math.Abs((longitude2 - longitude1)) * System.Math.PI / 180;
            dlat = System.Math.Abs((latitude2 - latitude1)) * System.Math.PI / 180;
            a = (System.Math.Sin(dlat / 2) * System.Math.Sin(dlat / 2)) + System.Math.Cos(latitude1 * System.Math.PI / 180) * System.Math.Cos(latitude2 * System.Math.PI / 180) * (System.Math.Sin(dlon / 2) * System.Math.Sin(dlon / 2));
            if (a == 1.0)
                c = System.Math.PI;
            else
                c = 2 * System.Math.Atan(System.Math.Sqrt(a) / System.Math.Sqrt(1 - a));
            distance = 6378137.0 * c;

            return distance;
        }

你可能感兴趣的:(火星坐标与地球坐标的转换,以及地球上两点之间的距离计算)