public void UpdateDrawTiles() { IsTilesUpdating = true; TilesUpdateLock.AcquireWriterLock(1000); try { if (ScreenArea != null) { var width = TilesArea.Width; var height = TilesArea.Height; var pointLeftTopPix = TileCenterPix.GetOffSet(-width / 2, -height / 2); //左上角像素坐标 var tileXYLeftTop = MercatorHelper.PixelToTileXY(pointLeftTopPix); //左上角的瓦片索引 var tilePixLeftTop = MercatorHelper.TileXYToPixel(tileXYLeftTop); //左上角瓦片像素坐标 //var tileDrawPositionLeftTop = new PointInt((int)(tilePixLeftTop.X - pointLeftTopPix.X + 0.5), (int)(tilePixLeftTop.Y - pointLeftTopPix.Y + 0.5));//屏幕坐标系下左上角瓦片的屏幕坐标 var tileDrawPositionLeftTop = TilesArea.LeftTop.GetOffSet((int)(tilePixLeftTop.X - pointLeftTopPix.X), (int)(tilePixLeftTop.Y - pointLeftTopPix.Y)); var tileXYCenter = MercatorHelper.PixelToTileXY(TileCenterPix);//中心瓦片索引 //var tilePixCenter = MercatorHelper.TileXYToPixel(tileXYCenter);//中心瓦片左上角所在像素坐标 var xCount = (int)Math.Ceiling((double)width / TileSize.Width) + 1; //X方向的瓦片数量 var yCount = (int)Math.Ceiling((double)height / TileSize.Height) + 1; //Y方向的瓦片数量 RectTilesBitmap = new RectInt(tileDrawPositionLeftTop, xCount * TileSize.Width, yCount * TileSize.Height); tileDrawPositionLeftTop = new PointInt(0, 0); GridInfo = new DrawGrid { GridSize = TileSize, LeftTop = RectTilesBitmap.LeftTop, RightBottom = RectTilesBitmap.RightBottom, XCount = xCount, YCount = yCount, }; //计算要显示的绘图瓦片 if (DicDrawTile == null) { DicDrawTile = new Dictionary <Tile, DrawTile>(); } DicDrawTile.Clear(); var minTileIndex = MapHelper.GetTileMatrixMinXY(Zoom); var maxTileIndex = MapHelper.GetTileMatrixMaxXY(Zoom); List <Tile> lstWantToLoad = new List <Tile>(); List <DrawTile> lstDrawTile = new List <DrawTile>(); //DateTime rt = DateTime.Now; for (int x = 0; x < xCount; x++) { for (int y = 0; y < yCount; y++) { var drawPostiton = tileDrawPositionLeftTop.GetOffSet(x * TileSize.Width, y * TileSize.Height); //Tile t = new Tile(tileXYLeftTop.X + x, tileXYLeftTop.Y + y, Zoom); Tile t = TileManager.Singleton.GetTile(Zoom, new PointInt64(tileXYLeftTop.X + x, tileXYLeftTop.Y + y)); if (t == null) { t = new Tile(tileXYLeftTop.X + x, tileXYLeftTop.Y + y, Zoom); } if (t.TileIndex.X >= minTileIndex.Width && t.TileIndex.Y >= minTileIndex.Height && t.TileIndex.X <= maxTileIndex.Width && t.TileIndex.Y <= maxTileIndex.Height) //范围判断 { var rt = new DrawTile() { DrawPosition = drawPostiton, Tile = t }; lstDrawTile.Add(rt); ////if (RotationAngle == 0 || MapHelper.CheckTileCrossRect(new RectInt(dt.DrawPosition.GetOffSet(TilesArea.LeftTop.X, TilesArea.LeftTop.Y), TileSize.Width, TileSize.Height), ScreenArea, RotationAngle)) //{ // //DicDrawTile.Add(t, rt); // lstDrawTile.Add(rt); // //if (t.TileImg == null) // //{ // // lstWantToLoad.Add(t); // //} //} } } } lstDrawTile = lstDrawTile.OrderBy(d => Math.Abs(d.Tile.TileIndex.X - tileXYCenter.X) + Math.Abs(d.Tile.TileIndex.Y - tileXYCenter.Y)).ToList(); foreach (var item in lstDrawTile) { if (item.Tile.State == TileState.New) { TileManager.Singleton.AddTile(item.Tile); } if (item.Tile.TileImg == null) { lstWantToLoad.Add(item.Tile); } item.Tile.ReadTime = DateTime.Now; DicDrawTile.Add(item.Tile, item); } lstWantToLoad.ForEach(a => { TileManager.Singleton.LoadTile(a); }); //_lastTilesRect = tilesRect; //TileDrawOffset = new PointInt(0,0); if (TilesUpdateComplete != null) { TilesUpdateComplete(DicDrawTile); } } } finally { IsTilesUpdating = false; TilesUpdateLock.ReleaseWriterLock(); } }
/// <summary> /// 根据瓦片索引和瓦片尺寸获取像素坐标 /// </summary> /// <param name="p"></param> /// <param name="tileSize"></param> /// <returns></returns> public static PointInt64 TileXYToPixel(PointInt64 p) { return(MapHelper.TileXYToPixel(p, TileSize)); }
/// <summary> /// 获取像素分辨率(每像素代表多少米) /// </summary> /// <param name="zoom">层级</param> /// <param name="latitude">纬度</param> /// <returns></returns> public static double GetResolution(int zoom, double latitude) { return((Math.Cos(latitude * (Math.PI / 180)) * 2 * Math.PI * Axis) / MapHelper.GetTileMatrixSizePixel(zoom, TileSize).Width); }
/// <summary> /// 根据像素坐标和瓦片尺寸获取瓦片索引 /// </summary> /// <param name="p"></param> /// <param name="tileSize"></param> /// <returns></returns> public static PointInt64 PixelToTileXY(PointDouble p) { return(MapHelper.PixelToTileXY(p, TileSize)); }