关键代码:
class="csharpcode">using System; using System.Collections; using System.Collections.Generic; using System.IO; using YanZhiwei.DotNet2.Utilities.Models; namespace YanZhiwei.DotNet2.Utilities.Common { /// <summary> /// 地图纠偏数据帮助类 /// </summary> public class MapOffsetDataHelper { #region 构造函数以及变量 private string offsetFullPath = string.Empty; /// <summary> /// 构造函数 /// </summary> /// <param name="path">纠偏数据文件路径</param> public MapOffsetDataHelper(string path) { offsetFullPath = path; } #endregion #region 私有方法 private void GetOffsetData(Action<MapCoord> mapCoordHanlder) { using (FileStream stream = new FileStream(offsetFullPath, FileMode.OpenOrCreate, FileAccess.Read)) { using (BinaryReader reader = new BinaryReader(stream)) { int _size = (int)stream.Length / 8; for (int i = 0; i < _size; i++) { byte[] _source = reader.ReadBytes(8); MapCoord _coord = ToCoord(_source); mapCoordHanlder(_coord); } } } } /// <summary> /// 将字节转化为具体的数据对象 /// </summary> /// <param name="bytes">bytes</param> /// <returns>MapCoord</returns> private MapCoord ToCoord(byte[] bytes) { //经度,纬度,x偏移量,y偏移量 【均两个字节】 MapCoord _coord = new MapCoord(); byte[] _b1 = new byte[2], _b2 = new byte[2], _b3 = new byte[2], _b4 = new byte[2]; Array.Copy(bytes, 0, _b1, 0, 2); Array.Copy(bytes, 2, _b2, 0, 2); Array.Copy(bytes, 4, _b3, 0, 2); Array.Copy(bytes, 6, _b4, 0, 2); _coord.Lon = BitConverter.ToInt16(_b1, 0); _coord.Lat = BitConverter.ToInt16(_b2, 0); _coord.X_off = BitConverter.ToInt16(_b3, 0); _coord.Y_off = BitConverter.ToInt16(_b4, 0); return _coord; } #endregion #region 获取纠偏数据集合 /// <summary> /// 获取纠偏数据集合 /// </summary> /// <returns>纠偏数据集合</returns> public List<MapCoord> GetMapCoordList() { List<MapCoord> _mapCoordList = new List<MapCoord>(); GetOffsetData(c => _mapCoordList.Add(c)); return _mapCoordList; } /// <summary> /// 获取纠偏数据集合 /// </summary> /// <returns>纠偏数据集合</returns> public ArrayList GetMapCoordArrayList() { ArrayList _mapCoordArrayList = new ArrayList(); GetOffsetData(c => _mapCoordArrayList.Add(c)); return _mapCoordArrayList; } #endregion } }
-----------------------------------------------------------------------------------------------
using System.Collections; using YanZhiwei.DotNet2.Utilities.Models; using YanZhiwei.DotNet2.Utilities.Tool; namespace YanZhiwei.DotNet2.Utilities.Common { /// <summary> /// 地图纠偏 帮助类 /// </summary> public class MapOffsetHelper { /* *参考: *1.http://www.apkbus.com/forum.php?mod=viewthread&tid=137621&extra=page%3D1&page=1 *2.http://yanue.net/post-122.html *3.http://go2log.com/2011/08/30/%E4%B8%AD%E5%9B%BD%E5%9C%B0%E5%9B%BE%E5%81%8F%E7%A7%BB%E6%A0%A1%E6%AD%A3php%E7%AE%97%E6%B3%95/ *4.http://www.devdiv.com/ios_gps_google_-blog-60266-10835.html */ #region 构造函数以及变量 private ArrayList mapCoordArrayList; /// <summary> /// 构造函数 /// </summary> /// <param name="offsetData">纠偏数据</param> public MapOffsetHelper(ArrayList offsetData) { mapCoordArrayList = offsetData; } #endregion #region 私有方法 private MapCoord QueryOffSetData(LatLngPoint point) { MapCoord _search = new MapCoord(); _search.Lat = (int)(point.LatY * 100); _search.Lon = (int)(point.LonX * 100); MapOffsetComparer rc = new MapOffsetComparer(); int _findedIndex = mapCoordArrayList.BinarySearch(0, mapCoordArrayList.Count, _search, rc); MapCoord _findedCoord = (MapCoord)mapCoordArrayList[_findedIndex]; return _findedCoord; } #endregion #region 地球坐标(WGS-84)转火星坐标 (GCJ-02) /// <summary> /// 地球坐标(WGS-84)转火星坐标 (GCJ-02) /// </summary> /// <param name="wgsPoint">地球坐标(WGS-84)</param> /// <returns>火星坐标 (GCJ-02)</returns> public LatLngPoint WGS84ToGCJ02(LatLngPoint wgsPoint) { MapCoord _findedCoord = QueryOffSetData(wgsPoint); double _pixY = MapHelper.LatToPixel(wgsPoint.LatY, 18); double _pixX = MapHelper.LonToPixel(wgsPoint.LonX, 18); _pixY += _findedCoord.Y_off; _pixX += _findedCoord.X_off; double _lat = MapHelper.PixelToLat(_pixY, 18); double _lng = MapHelper.PixelToLon(_pixX, 18); return new LatLngPoint(_lat, _lng); } #endregion #region 火星坐标转 (GCJ-02)地球坐标(WGS-84) /// <summary> /// 火星坐标转 (GCJ-02)地球坐标(WGS-84) /// </summary> /// <param name="gcjPoint">火星坐标转 (GCJ-02)</param> /// <returns>地球坐标(WGS-84)</returns> public LatLngPoint GCJ02ToWGS84(LatLngPoint gcjPoint) { MapCoord _findedCoord = QueryOffSetData(gcjPoint); double _pixY = MapHelper.LatToPixel(gcjPoint.LatY, 18); double _pixX = MapHelper.LonToPixel(gcjPoint.LonX, 18); _pixY -= _findedCoord.Y_off; _pixX -= _findedCoord.X_off; double _lat = MapHelper.PixelToLat(_pixY, 18); double _lng = MapHelper.PixelToLon(_pixX, 18); return new LatLngPoint(_lat, _lng); } #endregion } }