Is there a way to display a map for a given area completely offline using HTML and JavaScript? I am looking for a mobile-friendly (read Cordova-enabled) solution.
            Asked
            
        
        
            Active
            
        
            Viewed 4.1k times
        
    2 Answers
43
            
            
        There is an elegant solution for this problem in this blog post. I have compiled a full code example from it. Here are the steps:
1. Create map tiles
- download Mobile Atlas Creator
 - create a new atlas with OSMdroid ZIP format
 - make map and zoom selection, add your selection to the atlas
 - click "Create atlas"
 - unzip the atlas file
 - your tiles have this format: {atlas_name}/{z}/{x}/{y}.png ({z} stands for "zoom")
 
2. Set up HTML and JavaScript
- copy your atlas folder to your HTML root
 - download leaflet.js and leaflet.css and copy them to html root
 - create index.html with the code below
- adjust starting coordinates and zoom on the line where var mymap is defined
 - change atlasName to your folder name, set your desired maxZoom
 
 
3. You are all set! Enjoy!
- run index.html in your browser
 
<!DOCTYPE html> 
<html> 
 <head> 
  <title>Leaflet offline map</title>
  <link rel="stylesheet" charset="utf-8" href="leaflet.css" />
 <script type="text/javascript" charset="utf-8" src="leaflet.js"></script>
  <script>
   function onLoad() {
    var mymap = L.map('mapid').setView([50.08748, 14.42132], 16);
    L.tileLayer('atlasName/{z}/{x}/{y}.png',
    {    maxZoom: 16  }).addTo(mymap);
   }
  </script> 
 </head>
 <body onload="onLoad();"> 
  <div id="mapid" style="height: 500px;"></div>
 </body>
</html>
        Kozuch
        
- 2,272
 - 3
 - 26
 - 38
 
- 
                    Is there a way to use `.svg` instead? Downloading `.png` doesn't seem right. – Johhan Santana Oct 13 '22 at 21:09
 
-2
            
            
        you should do these steps one by one
- Download the mbtiles file of the specific area from https://openmaptiles.org/
 - establish the Map Server by Docker
 - implement the web pages by Leaflet.js and use the map server IP address in your codes.
 
        nassim taghipour
        
- 11
 - 1
 
- 
                    2
 - 
                    this answer might not be correct for this particular question. But it kinda helped me. Thanks – Arjun Atlast Jul 31 '22 at 11:09
 - 
                    This answer could be worded better but I think running a tile server locally is valid if you want to test the app offline. This wouldn't really work for a packaged and deployable solution though – benjrb Sep 05 '22 at 12:14