locked
Offline mapping ideas RRS feed

  • Question

  • User245536 posted

    I am working on a concept app that will be offline and would like to use Esri and ArcGIS for the map since native offline mapping is not supported for android and ios. My first thought was to create a .mpk file using their software and then load that onto the device, but I am relatively new to Dev work and haven't been able to find a way to load the mpk file using xamarin. Does anyone know how to load this file, and care to explain/ leave a lifeline, or see a better way to work around the issue? Some project specifications in case they are needed: working in VS 2015 needs to be offline, return an address based on user inputs, and preferably not work in WebMercator

    Tuesday, August 16, 2016 4:33 PM

Answers

  • User98308 posted

    here is how to download offline maps through your app https://gist.github.com/anstrevor/90a202d8c1e587ad0803e053b33a73f9

    here is how to load the downloaded files:

    ` // Get tile cache files in designated folder InitTileCacheList("/storage/emulated/0/SmartMaps/", "**");

            // Check internet connection.  If it exists, then load maps from online source,
            // if not, the attempt to lead map from offline file.
            var isConnected = getNetworkConnectionPing();
            if (isConnected)
            {
                Map = new Map(getDefaultMapType());  // Load online map
            }
            else
            {
                // Create basemap
                Basemap basemap = new Basemap();
    
                var layerlist = await GetTileLayers(_tileCacheFiles);
    
                // Get layers from file
                foreach (var layer in layerlist ?? new List<ArcGISTiledLayer>())
                {
                    basemap.BaseLayers.Add(layer);
                }
    
                // If the tile cache files could not be found, load default world map
                if (_tileCacheFiles.Count <= 0 || basemap.BaseLayers.Count <= 0)
                    Map = new Map(getDefaultMapType());  // Load default map
                else
                {
                    Map = new Map(basemap);  // Load basemap with offline tile cache layers
                }
            }`
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, August 16, 2016 9:11 PM

All replies

  • User98308 posted

    here is how to download offline maps through your app https://gist.github.com/anstrevor/90a202d8c1e587ad0803e053b33a73f9

    here is how to load the downloaded files:

    ` // Get tile cache files in designated folder InitTileCacheList("/storage/emulated/0/SmartMaps/", "**");

            // Check internet connection.  If it exists, then load maps from online source,
            // if not, the attempt to lead map from offline file.
            var isConnected = getNetworkConnectionPing();
            if (isConnected)
            {
                Map = new Map(getDefaultMapType());  // Load online map
            }
            else
            {
                // Create basemap
                Basemap basemap = new Basemap();
    
                var layerlist = await GetTileLayers(_tileCacheFiles);
    
                // Get layers from file
                foreach (var layer in layerlist ?? new List<ArcGISTiledLayer>())
                {
                    basemap.BaseLayers.Add(layer);
                }
    
                // If the tile cache files could not be found, load default world map
                if (_tileCacheFiles.Count <= 0 || basemap.BaseLayers.Count <= 0)
                    Map = new Map(getDefaultMapType());  // Load default map
                else
                {
                    Map = new Map(basemap);  // Load basemap with offline tile cache layers
                }
            }`
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, August 16, 2016 9:11 PM
  • User245536 posted

    @PhilipOGorman Thank you so much for getting back to me! Thats exactly what I was looking for however, I dont see definitions for InitTileCacheList and GetTileLayers. It looks like InitTileCache populates _tileCacheFiles do you have a definition on your side?

    Wednesday, August 17, 2016 3:51 PM
  • User98308 posted

    `
    ///

    /// Initialize offline tile cache file container. If any file cache files /// exist in memory matching a giving patter, add those file locations to /// a collection so they can be read later. /// /// Location on device where tile cache files are saved /// File search pattern. Note this is NOT Regex. See https://msdn.microsoft.com/en-us/library/dd383571(v=vs.110).aspx#Anchor_2 private void InitTileCacheList(string tileCacheLocation, string searchPattern) { _tileCacheFiles = new List();

            // Make sure the directory exists
            if (!System.IO.Directory.Exists(tileCacheLocation))
            {
                Debug.WriteLine("Could not find save directory");
                return;
            }
    
            // Get files in directory that match the search pattern
            var options = new System.IO.SearchOption();
            var filenames = System.IO.Directory.EnumerateFiles("/storage/emulated/0/SmartMaps/", searchPattern, options);
            foreach (var file in filenames)
            {
                _tileCacheFiles.Add(file);
            }
        }`
    
    Wednesday, August 17, 2016 4:06 PM
  • User98308 posted

    ` private async Task> GetTileLayers(List fileList) { List layerlist = new List();

            // For each file cache file, load in tiles as layers
            foreach (var tileCacheFile in _tileCacheFiles)
            {
                var layer = await LoadTileCache(tileCacheFile);
    
                // Add layer to basemap if loaded successfully
                if (layer.LoadStatus == Esri.ArcGISRuntime.LoadStatus.Loaded)
                    layerlist.Add(layer);
            }
    
            return layerlist;
        }
    

    `

    Wednesday, August 17, 2016 4:07 PM
  • User245536 posted

    @PhilipOGorman Awesome answer again I am playing with changing the file path to: System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments) in order to make it more cross platform friendly and seems to be working well. One issue though under GetTileLayers() is LoadTileCache supposed to be a defined function or a system function?

    Wednesday, August 17, 2016 10:30 PM
  • User98308 posted

    ` ///

    /// Load in basemap tiles from .tpk files. Look in directory for files, /// chosen from a search pattern. /// /// Directory that contains tile files /// Return an ArcGIS layer data structure containing the tile cache data private async Task LoadTileCache(string filePath) { // Open and load tile cache TileCache tileCache = new TileCache(filePath); await tileCache.LoadAsync();

            // Create layer from tile cache
            ArcGISTiledLayer layer = new ArcGISTiledLayer(tileCache);
    
            // Check that the layer was loaded successfully
            if (layer.LoadStatus != Esri.ArcGISRuntime.LoadStatus.Loaded)
                Debug.WriteLine("Could not load tile cache");
            else
                Debug.WriteLine("Tile cache loaded successfully.");
    
            return layer;
        }
    

    `

    Thursday, August 18, 2016 1:54 PM
  • User251819 posted

    @KarlRichey said: @PhilipOGorman Awesome answer again I am playing with changing the file path to: System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments) in order to make it more cross platform friendly and seems to be working well. One issue though under GetTileLayers() is LoadTileCache supposed to be a defined function or a system function?

    Hi @PhilipOGorman ,

    We are having an issue regarding the offline basemap. We are able to download the arcgis online base map in our device. using the code given in this forum,

    .NET Xamarin gist to download ArcGIS basemap tiles for offline use. · GitHub https://gist.github.com/anstrevor/90a202d8c1e587ad0803e053b33a73f9

    However after downloading when we are trying to load the offline map( using the sample code given in this link Offline mapping ideas — Xamarin Forums)

    https://forums.xamarin.com/discussion/73515/offline-mapping-ideas

    from our local storage and it is not loading in the background if we switch off the device internet connection. But surprisingly if we switch on the internet the offline map was loading in the background from the local storage.

    we are using Xamarin forms to develop the application. Any help concerning this issue will be highly appreciated.

    Thanks,

    Thursday, December 29, 2016 11:42 AM
  • User393823 posted

    @sammit said:

    @KarlRichey said: @PhilipOGorman Awesome answer again I am playing with changing the file path to: System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments) in order to make it more cross platform friendly and seems to be working well. One issue though under GetTileLayers() is LoadTileCache supposed to be a defined function or a system function?

    Hi @PhilipOGorman ,

    We are having an issue regarding the offline basemap. We are able to download the arcgis online base map in our device. using the code given in this forum,

    .NET Xamarin gist to download ArcGIS basemap tiles for offline use. · GitHub https://gist.github.com/anstrevor/90a202d8c1e587ad0803e053b33a73f9

    However after downloading when we are trying to load the offline map( using the sample code given in this link Offline mapping ideas — Xamarin Forums)

    https://forums.xamarin.com/discussion/73515/offline-mapping-ideas

    from our local storage and it is not loading in the background if we switch off the device internet connection. But surprisingly if we switch on the internet the offline map was loading in the background from the local storage.

    we are using Xamarin forms to develop the application. Any help concerning this issue will be highly appreciated.

    Thanks,

    Hi @sammit, Currently I have requirement to load basemap in offline mode. If the network is available I can use arcGIS online map , however if there is no network, I need to show offline BaseMap. Were you able to implement it ? If yes, can you please share a code snippet for same.

    Thanks in Advance.

    Wednesday, May 27, 2020 6:10 AM