使用百度提供纠正接口对定位经度纬度纠正
通过GPS或者北斗定位坐标和中国国内地图坐标有偏移,纠正方法如下:11
/// <summary> /// 百度地图纠正偏移 /// </summary> /// <param name="requestModel"></param> /// <returns></returns> private RequestModel GetRealyLanANdLat(RequestModel requestModel) { string url = "http://api.map.baidu.com/ag/coord/convert?from=2&to=4&x="+requestModel.Location_X+"&y="+requestModel.Location_Y+""; HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); System.IO.Stream responseStream = response.GetResponseStream(); System.IO.StreamReader sr = new System.IO.StreamReader(responseStream, System.Text.Encoding.GetEncoding("utf-8")); string responseText = sr.ReadToEnd(); sr.Close(); sr.Dispose(); responseStream.Close(); string jsonData = responseText; JavaScriptSerializer jss = new JavaScriptSerializer(); ConvertLanAndLatWithBaiduModel convertLanAndLatWithBaidu = jss.Deserialize<ConvertLanAndLatWithBaiduModel>(jsonData); requestModel .Location_X= System.Text.Encoding.Default.GetString(Convert.FromBase64String(convertLanAndLatWithBaidu.X)); requestModel.Location_Y = System.Text.Encoding.Default.GetString(Convert.FromBase64String(convertLanAndLatWithBaidu.Y)); return requestModel; }
说明,上述方法使用百度提供的纠正方法,获取纠正坐标后做Base64解码获得真实经纬度。
以下是反系列化Model:
[Serializable] public class ConvertLanAndLatWithBaiduModel { public int Error { get; set; } public string X { get; set; } public string Y { get; set; } }
最近评论