C#GPS坐标转百度地图坐标

国际经纬度坐标标准为WGS-84,国内要求必须至少使用国测局制定的GCJ- 02,对地理位置进行首次加密。百度坐标在此基础上,进行了BD-09二次加密措施。百度对外接口的坐标系并不是GPS采集的真实经 纬度,需要通过坐标转换接口进行转换。 

所以在进行百度地图开发时,如果我们手上的数据是GPS数据的话,需要进行坐标转换,转换方法如下:

 URL:     http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=longitude&y=latitude

    其中:
    from: 来源坐标系   (0表示原始GPS坐标,2表示Google坐标)
      to: 转换后的坐标   (4就是百度坐标)
        x: GPS经度
            y: GPS纬度
            返回的结果是一个json字符串:
           {"error":0,"x":"MTIwLjAxMTYxNjkwMTA0","y":"MzYuMDA2NDI2NDI0Mzc1"}

            其中:
            error:是结果是否出错,"0"表示OK
            x: 百度坐标系的经度(Base64加密)
            y: 百度坐标系的纬度(Base64加密)

下面上代码:

 public static double[] getBaiducoor(double[] coord)//坐标转换的方法
        {
            double longitude = coord[0];
            double latitude = coord[1];
            //需要转的gps经纬度
            string convertUrl = "http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=" + longitude + "&y=" + latitude + "";

            HttpWebRequest request = (HttpWebRequest)System.Net.HttpWebRequest.Create(convertUrl);//创建http请求
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream responseStream = response.GetResponseStream();
            StreamReader sr = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
            string responseTxt = sr.ReadToEnd();
            sr.Close();
            sr.Dispose();

            CoorConvert mapconvert = new CoorConvert();//创建存放结果的对象
            mapconvert = JsonConvert.DeserializeObject<CoorConvert>(responseTxt);//赋值

            string lon = mapconvert.x;
            string lat = mapconvert.y;

            byte[] xBuffer = Convert.FromBase64String(lon);//坐标base64解密
            string strX = Encoding.UTF8.GetString(xBuffer, 0, xBuffer.Length);

            byte[] yBuffer = Convert.FromBase64String(lat);
            string strY = Encoding.UTF8.GetString(yBuffer, 0, xBuffer.Length);

            double[] coor = new double[2];
            coor[0] = Convert.ToDouble(strX);
            coor[1] = Convert.ToDouble(strY);
            return coor;
        }

        //创建一个对象存储结果
        public class CoorConvert
        {
            public string error { get; set; }
            public string x { get; set; }
            public string y { get; set; }
        }

需要添加Newtonsoft.Json引用解析返回的json结果字符串,此方法输入的参数是存储经纬度的double数组,返回结果也是double数组。

 
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值