コード例 #1
0
        private void AddTiles(Rect newvp)
        {
            //если зум новый то добавить тайлы
            double vpX             = newvp.X;
            var    firstTileXIndex = (int)Math.Floor(-1 * vpX / Constants.TileSize);
            var    firstTileYIndex = (int)Math.Floor(-1 * newvp.Y / Constants.TileSize);
            var    coordX          = vpX + firstTileXIndex * Constants.TileSize;
            var    coordY          = newvp.Y + firstTileYIndex * Constants.TileSize;

            while (firstTileYIndex < 0)
            {
                firstTileYIndex++;
                coordY += Constants.TileSize;
            }
            while (firstTileXIndex < 0)
            {
                firstTileXIndex++;
                coordX += Constants.TileSize;
            }

            int currentXIndex = 0;
            int currentYIndex = 0;
            var currentCoordX = coordX;
            var currentCoordY = coordY;

#if DEBUG
            int querypiccounter = 0;
#endif
            do
            {
                do
                {
                    var tp = new TilePosition
                    {
                        X = currentXIndex + firstTileXIndex,
                        Y = currentYIndex + firstTileYIndex
                    };
                    TileID tid = new TileID {
                        Pos = tp, Zoom = Zoom
                    };
                    Tile tile = new Tile(tid);
                    try
                    {
#if DEBUG
                        querypiccounter++;
#endif
                        tile.SetImage();
                    }
                    catch (TileIndexOutOfRangeException)
                    {
                    }
                    _map.Children.Add(tile);
                    Panel.SetZIndex(tile, 0);
                    Canvas.SetLeft(tile, currentCoordX);
                    Canvas.SetTop(tile, currentCoordY);
                    currentCoordX += Constants.TileSize;
                    currentXIndex++;
                    var contX = MyImageDownloaderAsync.IsIndexCorrect(currentXIndex, Zoom);
                    if (!contX)
                    {
                        break;
                    }
                } while (currentCoordX <= _map.ActualWidth);
                currentCoordY += Constants.TileSize;
                currentYIndex++;
                currentCoordX = coordX;
                currentXIndex = 0;
                var contY = MyImageDownloaderAsync.IsIndexCorrect(currentYIndex, Zoom);
                if (!contY)
                {
                    break;
                }
            } while (currentCoordY <= _map.ActualHeight);
#if DEBUG
            // Debug.Print("qpc {0}",querypiccounter);
#endif
        }
コード例 #2
0
 internal static async Task <ImageSource> GetImage(TileID tid)
 {
     return(await GetImage(tid.Zoom, tid.Pos.X, tid.Pos.Y));
 }
コード例 #3
0
 public Tile(TileID tid)
 {
     _tid   = tid;
     Width  = Constants.TileSize;
     Height = Constants.TileSize;
 }
コード例 #4
0
        internal static ImageSource GetImageS(TileID tid)
        {
            Debug.Print("query image {0}", tid);
            if (!(IsIndexCorrect(tid.Pos.X, tid.Zoom) & IsIndexCorrect(tid.Pos.Y, tid.Zoom)))
            {
                throw new TileIndexOutOfRangeException();
            }
            var zoom      = tid.Zoom;
            var x         = tid.Pos.X;
            var y         = tid.Pos.Y;
            var cachename = Path.Combine(CacheFolder, zoom.ToString(), x.ToString(), y + ".png");
            var url       = string.Format(urlTemplate, zoom, x, y);

            if (File.Exists(cachename))
            {
                FileStream file = null;
                try
                {
                    file = File.OpenRead(cachename);
                    return(GetImageFromStream(file));
                }
                catch (NotSupportedException) // Problem creating the bitmap (file corrupt?)
                {
                }
                catch (IOException) // Or a prolbem opening the file. We'll try to re-download the file.
                {
                }
                finally
                {
                    if (file != null)
                    {
                        file.Dispose();
                    }
                }
            }
            MemoryStream buffer = null;

            try
            {
                // First download the image to our memory.
                var request = (HttpWebRequest)WebRequest.Create(url);
                request.Proxy             = WebRequest.DefaultWebProxy;
                request.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
                buffer = new MemoryStream();
                using (var response = request.GetResponse())
                {
                    var stream = response.GetResponseStream();
                    stream.CopyTo(buffer);
                    stream.Close();
                }

                // Then save a copy for future reference, making sure to rewind
                // the stream to the start.
                buffer.Position = 0;
                SaveCacheImage(buffer, cachename);

                // Finally turn the memory into a beautiful picture.
                buffer.Position = 0;
                return(GetImageFromStream(buffer));
            }
            catch (WebException)
            {
                // RaiseDownloadError();
            }
            catch (NotSupportedException) // Problem creating the bitmap (messed up download?)
            {
                //RaiseDownloadError();
            }
            finally
            {
                // EndDownload();
                if (buffer != null)
                {
                    buffer.Dispose();
                }
            }
            return(null);
        }