百度墨卡托坐标转百度经纬度坐标方法实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace DataProcess
{
    class CoordinatesConver
    {
        private static double[] Sp = { 1.289059486E7, 8362377.87, 5591021, 3481989.83, 1678043.12, 0 };
        private static String BAIDU_LBS_TYPE = "bd09ll";
        private static double pi = 3.1415926535897932384626;
        private static double a = 6378245.0;
        private static double ee = 0.00669342162296594323;

        /// <summary>
        /// Bd09mc To Bd09(精度高)
        /// </summary>
        /// <param name="lng"></param>
        /// <param name="lat"></param>
        /// <returns></returns>
        public static double[] Mercator2BD09(double lng, double lat)
        {
            double[] lnglat = new double[2];
            ArrayList c = null;
            //List<Double> d0 = new ArrayList<Double>();
            ArrayList d0 = new ArrayList();
            double[] d0str = { 1.410526172116255E-8, 8.98305509648872E-6, -1.9939833816331, 200.9824383106796, -187.2403703815547, 91.6087516669843, -23.38765649603339, 2.57121317296198, -0.03801003308653, 1.73379812E7 };
            for (int i = 0; i < d0str.Length; i++)
            {
                d0.Add(d0str[i]);
            }

            ArrayList d1 = new ArrayList();
            double[] d1str = { -7.435856389565537E-9, 8.983055097726239E-6, -0.78625201886289, 96.32687599759846, -1.85204757529826, -59.36935905485877, 47.40033549296737, -16.50741931063887, 2.28786674699375, 1.026014486E7 };
            for (int i = 0; i < d1str.Length; i++)
            {
                d1.Add(d1str[i]);
            }

            ArrayList d2 = new ArrayList();
            double[] d2str = { -3.030883460898826E-8, 8.98305509983578E-6, 0.30071316287616, 59.74293618442277, 7.357984074871, -25.38371002664745, 13.45380521110908, -3.29883767235584, 0.32710905363475, 6856817.37 };
            for (int i = 0; i < d2str.Length; i++)
            {
                d2.Add(d2str[i]);
            }

            ArrayList d3 = new ArrayList();
            double[] d3str = { -1.981981304930552E-8, 8.983055099779535E-6, 0.03278182852591, 40.31678527705744, 0.65659298677277, -4.44255534477492, 0.85341911805263, 0.12923347998204, -0.04625736007561, 4482777.06 };
            for (int i = 0; i < d3str.Length; i++)
            {
                d3.Add(d3str[i]);
            }

            ArrayList d4 = new ArrayList();
            double[] d4str = { 3.09191371068437E-9, 8.983055096812155E-6, 6.995724062E-5, 23.10934304144901, -2.3663490511E-4, -0.6321817810242, -0.00663494467273, 0.03430082397953, -0.00466043876332, 2555164.4 };
            for (int i = 0; i < d4str.Length; i++)
            {
                d4.Add(d4str[i]);
            }

            ArrayList d5 = new ArrayList();
            double[] d5str = { 2.890871144776878E-9, 8.983055095805407E-6, -3.068298E-8, 7.47137025468032, -3.53937994E-6, -0.02145144861037, -1.234426596E-5, 1.0322952773E-4, -3.23890364E-6, 826088.5 };
            for (int i = 0; i < d5str.Length; i++)
            {
                d5.Add(d5str[i]);
            }

            lnglat[0] = Math.Abs(lng);
            lnglat[1] = Math.Abs(lat);

            for (int d = 0; d < 6; d++)
            {
                if (lnglat[1] >= Sp[d])
                {
                    if (d == 0)
                    {
                        c = d0;
                    }

                    if (d == 1)
                    {
                        c = d1;
                    }

                    if (d == 2)
                    {
                        c = d2;
                    }

                    if (d == 3)
                    {
                        c = d3;
                    }

                    if (d == 4)
                    {
                        c = d4;
                    }

                    if (d == 5)
                    {
                        c = d5;
                    }

                    break;
                }
            }
            lnglat = Yr(lnglat, c);
            return lnglat;
        }

        private static double[] Yr(double[] lnglat, ArrayList b)
        {
            if (b != null)
            {
                double c = double.Parse(b[0].ToString()) + double.Parse(b[1].ToString()) * Math.Abs(lnglat[0]);
                double d = Math.Abs(lnglat[1]) / double.Parse(b[9].ToString());
                d = double.Parse(b[2].ToString()) + double.Parse(b[3].ToString()) * d + double.Parse(b[4].ToString()) * d * d + double.Parse(b[5].ToString()) * d * d * d + double.Parse(b[6].ToString()) * d * d * d * d + double.Parse(b[7].ToString()) * d * d * d * d * d + double.Parse(b[8].ToString()) * d * d * d * d * d * d;
                double bd;
                if (0 > lnglat[0])
                {
                    bd = -1 * c;
                }
                else
                {
                    bd = c;
                }
                lnglat[0] = bd;

                double bd2;
                if (0 > lnglat[1])
                {
                    bd2 = -1 * d;
                }
                else
                {
                    bd2 = d;
                }
                lnglat[1] = bd2;
                return lnglat;
            }
            return null;
        }
    }
}

该方法参考了 https://blog.csdn.net/qq_16664325/article/details/67639684

这篇文章中主要是Java版本的,我把它改成了C#版本。该方法主要实现了百度地图api中pointToLngLat ()这个方法,用于将百度墨卡托投影坐标转换为百度经纬度坐标,经过测试,与官方完全一致。

posted @ 2018-04-03 10:40  陈荷西  阅读(5570)  评论(1编辑  收藏  举报