手机上报的经纬度,并不是所有的都是GPS格式的,有的是GCJ02、有的是Baidu等等,那么如何才能针对一个城市的数据进行全面的纠偏呢?首先需要建立一个纠偏库,之后使用纠偏库实现纠偏。

  以GCJ02纠偏库需要以下步骤:

一、建立纠GCJ02纠偏为GPS的偏库

1)选择一个城市的范围,找到最大最小经度、纬度的范围,设置需要纠偏的精确度,比如我选择A城市做为示例设置其纠偏范围

double leftUpLng = 121.0127;
double leftUpLat = 31.0850;
double rightDownLng = 121.392234;
double rightDownLat = 31.446334;

精确度:0.0001(以米为单位)

 long lngOffsetScope = (long)((rightDownLng - leftUpLng) / 0.0001);
 long latOffsetScope = (long)((rightDownLat - leftUpLat) / 0.0001);

3)选纠偏函数

  1         private void DoOffset(List<LatLngOffsetStruct> items)
  2         {
  3             string lats = string.Join(";", items.Select(m => m.GCJ02Lat).ToArray());
  4             string lngs = string.Join(";", items.Select(m => m.GCJ02Lng).ToArray());
  5 
  6             /**
  7 批量纠偏接口(POST)
  8 接口地址 http://api.zdoz.net/transmore.ashx
  9 接口说明 
 10 批量纠偏,一次最大可纠偏1000个坐标点
 11 参数
 12 lats:维度,多个维度用“;”隔开
 13 lngs:经度,多个经度用“;”隔开(要注意经纬度个数相等)
 14 type:转换类型 【1.WGS -> GCJ】 【2.GCJ -> WGS】 【3.GCJ -> BD】 【4.BD -> GCJ】 【5.WGS -> BD】 【6.BD -> WGS】
 15 返回值JSON
 16 根据次序返回一个json格式的数组
 17 演示
 18 参数:lats=34.123;34.332;55.231&lngs=113.123;112.213;115.321&type=1
 19 
 20 返回:[{"Lng":113.12942937312582,"Lat":34.121761850760855},{"Lng":112.21911710957568,"Lat":34.3306763095054}, {"Lng":115.33036232125529,"Lat":55.232930158541052}]
 21 */
 22             string requestUri = "http://api.zdoz.net/transmore.ashx";
 23             string parameter = string.Format("lats={0}&lngs={1}&type=2", lats, lngs);
 24             int cursor = 0;
 25 
 26             GOTO_AGAIN:
 27             try
 28             {
 29                 cursor++;
 30 
 31                 string httpContext = GetRequesetContext(requestUri, parameter);
 32                 // 返回:[{"Lng":113.12942937312582,"Lat":34.121761850760855},{"Lng":112.21911710957568,"Lat":34.3306763095054}, {"Lng":115.33036232125529,"Lat":55.232930158541052}]
 33                 string[] splitItems = httpContext.Split(new string[] { "[", "},{", "]" }, StringSplitOptions.RemoveEmptyEntries);
 34 
 35                 if (splitItems.Length < items.Count)
 36                 {
 37                     logger.Warn("出现拆分出的lat,lng的组合长度不够" + items.Count + "!!!");
 38                 }
 39 
 40                 int itemsNumber = splitItems.Length;
 41                 if (splitItems.Length > items.Count)
 42                 {
 43                     itemsNumber = items.Count;
 44                 }
 45 
 46                 for (var i = 0; i < itemsNumber; i++)
 47                 {
 48                     string[] lngLat = splitItems[i].Split(new string[] { "{", "}", "\"Lng\":", ",\"Lat\":" }, StringSplitOptions.RemoveEmptyEntries);
 49                     if (lngLat.Length != 2)
 50                     {
 51                         logger.Warn("出现" + splitItems[i] + "拆分出的lat,lng格式不正确!!!");
 52                     }
 53 
 54                     double lng = double.Parse(lngLat[0]);
 55                     double lat = double.Parse(lngLat[1]);
 56 
 57                     LatLngOffsetStruct item = items[i];
 58                     item.GpsLng = lng;
 59                     item.GpsLat = lat;
 60                     item.LatOffset = (item.GCJ02Lat - item.GpsLat).ToString();
 61                     item.LngOffset = (item.GCJ02Lng - item.GpsLng).ToString();
 62                 }
 63             }
 64             catch (Exception ex)
 65             {
 66                 if (cursor < 5)
 67                 {
 68                     goto GOTO_AGAIN;
 69                 }
 70 
 71                 logger.Error("DoOffset失败次数超过5次:\r\n{0}\r\n{1}", ex.Message, ex.StackTrace);
 72             }
 73         }
 74 
 75         private string GetRequesetContext(string requestUri, string parameter)
 76         {
 77             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
 78             request.Method = "post";
 79             request.ContentType = "application/x-www-form-urlencoded";
 80 
 81             byte[] payload = System.Text.Encoding.UTF8.GetBytes(parameter);
 82             request.ContentLength = payload.Length;
 83 
 84             Stream writer;
 85             try
 86             {
 87                 writer = request.GetRequestStream();
 88             }
 89             catch (Exception)
 90             {
 91                 writer = null;
 92                 Console.Write("连接服务器失败!");
 93             }
 94 
 95             writer.Write(payload, 0, payload.Length);
 96             writer.Close();
 97 
 98             HttpWebResponse response;
 99             try
100             {
101                 response = (HttpWebResponse)request.GetResponse();
102             }
103             catch (WebException ex)
104             {
105                 response = ex.Response as HttpWebResponse;
106             }
107 
108             string httpContext = string.Empty;
109 
110             using (Stream stream = response.GetResponseStream())
111             {
112                 using (StreamReader reader = new StreamReader(stream))
113                 {
114                     httpContext = reader.ReadToEnd();
115                 }
116             }
117 
118             response.Close();
119 
120             return httpContext;
121         }
View Code

相关文章: