none
How to load tiles from local png files ? RRS feed

  • Question

  • Hello,

    I would like to load local png tiles as the tiles of my map in c# for a windows store app;

    Until now I used Portable Basemap Server with a mbtiles file to generate my map and get the tile Uri. Now I have all my tiles extracted in a "Map" folder in the appdata localstate folder of my app, but when I try to give the path to the tiles nothing happen and the map stay white. 

    My code:

    private void InitMap()
            {
                MyMap.PreloadArea = PreloadArea.Large;
                MapTileLayer layer2 = new MapTileLayer();
                layer2.Opacity = 1;
                    layer2.GetTileUri += (s, e) =>
                    {
                        //e.Uri = new Uri(string.Format("http://127.0.0.1:7080/PBS/rest/services/MyService/MapServer/WMTS/tile/1.0.0/MyService/default/default028mm/{0}/{1}/{2}.png", e.LevelOfDetail, e.Y, e.X));
                        int x = ((int)(Math.Pow(2.0, (double)e.LevelOfDetail) - 1.0)) - e.X;
                        string path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, 
                        "Map", 
                        e.LevelOfDetail.ToString(), 
                        x.ToString(), e.Y.ToString());
                        path += ".png";
                        e.Uri = new Uri(path);
                    };
                    MyMap.TileLayers.Add(layer2);
            }

    If I use PBS with the commented line it works well but I don't want to use PBS anymore.

    How can I use the local png files to load my map ?


    Wednesday, April 15, 2015 1:15 PM

Answers

All replies

  • The MapTileLayer in the Windows 8.1 map control isn't able to access the local tiles, however it is possible to work around this. I've created a sample app that does this here: https://code.msdn.microsoft.com/Local-Map-Tile-Layers-in-b1d6136b


    http://rbrundritt.wordpress.com

    Wednesday, April 15, 2015 5:54 PM
  • I have already tried that sample but when I use it it throws an Exception line 110 of the LocalTileLayer.cs file "Value does not fall within the expected range". It seems to fail to open the zip file.

    My Code:

            private LocalTileSource layerInfo = new LocalTileSource()
            {
                ZipTilePath = new Uri(Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "MapQuadKey.zip"))
            };
    
            private void InitMap()
            {
                MyMap.PreloadArea = PreloadArea.Large;
                MyMap.Loaded += (s, e) =>
                {
                    var layer = new LocalTileLayer(MyMap, layerInfo);
                    layer.Opacity = 1;
                    MyMap.Children.Add(layer);
                };
            }

    Thursday, April 16, 2015 9:06 AM
  • You have to update the path to point to where your tiles are.

    http://rbrundritt.wordpress.com

    Thursday, April 16, 2015 4:08 PM
  • Is there a sample for Windows 10 available?
    Friday, January 5, 2018 12:48 PM
  • Sample code for adding tile layers is available here:

    https://docs.microsoft.com/en-us/windows/uwp/maps-and-location/overlay-tiled-images

    Friday, January 5, 2018 7:08 PM
    Moderator