Exemple #1
0
        private readonly string[] _serverEndpoints = { "a", "b", "c" }; // OSM server addreses

        void Awake()
        {
            // Create a TileRange based on the corner coordinates of the GPX
            // track.
            _gpxInput.Initialize(_zoom); // initialize _gpxInput obj
            var bounds = _gpxInput.ComputeMapCoordinateBounds();
            var tiles  = new TileRange((int)bounds.minLon, (int)bounds.minLat,
                                       (int)bounds.maxLon, (int)bounds.maxLat,
                                       _zoom);

            // init list of tex2D that forms the tiles in the tilemap
            var tileTexList = new List <(int X, int Y, Texture2D texture)>();

            // system path to save the png OSM tile images and later load them
            // from dir
            string tilesDataPath = Path.Combine(Application.persistentDataPath,
                                                "Assets/Resources");

            // for each tile in the tilerange, download from server and save to
            // file or load from file if it already exists/
            foreach (var tile in tiles)
            {
                var random   = new System.Random();
                var savePath = Path.Combine(tilesDataPath, "Tiles",
                                            $"{_zoom}/{tile.X}/{tile.Y}.png");
                if (!File.Exists(savePath))
                {
                    // Download new map tile
                    var url =
                        $"http://{_serverEndpoints[random.Next(0, 2)]}.tile.openstreetmap.org/{_zoom}/{tile.X}/{tile.Y}.png";
                    WebRequests.GetTexture(url,
                                           (string error) =>
                    {
                        // Net error.
                        // Debug.Log("Error: " + error);
                    }, (Texture2D texture2D) =>
                    {
                        // Save Image
                        ImageLoader.SaveImage(savePath, texture2D.EncodeToPNG());
                        tileTexList.Add((tile.X, -tile.Y, texture2D));
                    });