GMap.Net添加百度和高德地图瓦片源

来源:互联网 发布:mysql给root所有权限 编辑:程序博客网 时间:2024/05/16 18:21

   GMap.NET is great and Powerful, Free, cross platform, open source .NET control. Enable use routing, geocoding, directions and maps from Coogle, Yahoo!, Bing, OpenStreetMap, ArcGIS, Pergo, SigPac, Yandex, Mapy.cz, Maps.lt, iKarte.lv, NearMap, OviMap, CloudMade, WikiMapia, MapQuest in Windows Forms & Presentation, supports caching and runs on windows mobile!

它与很多地图厂商API不同的是,它里面有很多底层的实现,缓存,地图Tile的管理,路线,坐标转换等等,因此很有研究价值。并且,GMAP.NET是可以在Windows Form, WPF和Windows Mobile上运行的。

   GMAP.NET目前是支持openstreetmap, google, bing,yahoo,ovi等地图的,但对于国内的地图支持寥寥,这篇博客http://www.cnblogs.com/enjoyeclipse/archive/2013/01/13/2858392.html只是从是通过编码转换实现了地图编码转换,但是此举只可实现浏览功能,但是无法实现地图的正反编码功能,在参考http://www.cnblogs.com/kekec/p/3159970.html这篇博客中对百度地图投影分析的基础上,我实现了百度地图投影接口,百度地图瓦片投影公式介绍:


图片(x,y,z)像素(m,n)[注:像素坐标以左上角为原点,x轴向右,y轴向下]的经纬度[单位:度]分别为:

-----------------------------------------------------------------------------

已知经纬度(单位:度),求瓦片编号x,y:

实现代码:

1、经纬度到像素坐标系的转换

 public override GPoint FromLatLngToPixel(double lat, double lng, int zoom)      {         GPoint ret = GPoint.Empty;         lat = Clip(lat, MinLatitude, MaxLatitude);         lng = Clip(lng, MinLongitude, MaxLongitude);         GSize s = GetTileMatrixSizePixel(zoom);         long mapSizeX = s.Width;         long mapSizeY = s.Height;         double xMercatorTileNum = (Math.Pow(2.0, zoom - 26) *                              (Math.PI * lng * Axis / 180.0));         double xAxisPixelLength = xMercatorTileNum * 256;         ret.X = (long)(mapSizeX * 0.5 + xAxisPixelLength);         double yMercatorTileNum = (Math.Pow(2.0, zoom - 26) * Axis *                                    Math.Log(Math.Tan(Math.PI * lat / 180.0) +                                    1.0 / Math.Cos(Math.PI * lat / 180.0)));         double yAxisPixelLength = yMercatorTileNum * 256;         ret.Y = (long)(mapSizeY * 0.5 - yAxisPixelLength);         return ret;      }

2、像素坐标系到经纬度坐标的转换

public override PointLatLng FromPixelToLatLng(long x, long y, int zoom)      {          PointLatLng ret = PointLatLng.Empty;          GSize s = GetTileMatrixSizePixel(zoom);          double mapSizeX = s.Width;          double mapSizeY = s.Height;          double xMercatorBaidu = x - 0.5 * mapSizeX;          double yMercatorBaidu = -y + 0.5 * mapSizeY;          double xTileNum = xMercatorBaidu / 256;          double yTileNum = yMercatorBaidu / 256;          int tileXNum = (int)Math.Floor(xTileNum);          int tileYNum = (int)Math.Floor(yTileNum);          double xRelativeTileCoord = xMercatorBaidu % 256 / 256;          xRelativeTileCoord = xRelativeTileCoord > 0 ? xRelativeTileCoord : 1 + xRelativeTileCoord;          double yRelativeTileCoord = yMercatorBaidu % 256 / 256;          yRelativeTileCoord = yRelativeTileCoord > 0 ? 1 - yRelativeTileCoord : -yRelativeTileCoord;          long xPixelCoord = (long)(xRelativeTileCoord * 256);          long yPixelCoord = (long)(yRelativeTileCoord * 256);          double tileNumX = (tileXNum + (double)xPixelCoord / 256);          ret.Lng = (Math.Pow(2.0, 26 - zoom) * (tileNumX)) /                    (Math.PI * Axis) * 180;          double eIndex = Math.Pow(2.0, 26 - zoom) * (tileYNum + 1 - (double)yPixelCoord / 256) / Axis;          ret.Lat = 360 * Math.Atan(Math.Pow(Math.E, eIndex)) / Math.PI - 90;         return ret;      }

3、显示效果




0 1
原创粉丝点击