Getting Started

Basic steps for working with ArcGIS.com base maps for offline.

Step 1: Fork or clone offline-editor-js

Here are the important directories to know:

  • \dist - concatenated library source and minified library files.
  • \samples - examples that demonstrate the library's functionality.
  • \vendor - contains IndexedDBShim and offline.js libraries

Step 2: Fill in the basics

Add the basic library references. Then test to make sure map loads.


                        
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>
    <title>Simple Map</title>
    <link rel="stylesheet" href="http://js.arcgis.com/3.11/esri/css/esri.css">
    <style>
        html, body, #map {
            height: 100%;
            width: 100%;
            margin: 0;
            padding: 0;
        }
        body {
            background-color: #000000;
            overflow: hidden;
            font-family: "Trebuchet MS";
        }
    </style>

    <!-- Include a reference to offline.js which detects online/offline conditions -->
    <script src="../vendor/offline/offline.min.js"></script>
    <script>
        // Set the online/offline detection options.
        // More info at: http://github.hubspot.com/offline/docs/welcome/
        Offline.options = {
            checks: {
                image: {
                    url: function() {
                        return 'http://esri.github.io/offline-editor-js/tiny-image.png?_=' +
                            (Math.floor(Math.random() * 1000000000));
                    }
                },
                active: 'image'
            }
        }
    </script>

    <!-- Include a reference to IndexedDBShim for library to work on Safari 7.x -->
    <script src="../vendor/IndexedDBShim/dist/IndexedDBShim.js"></script>

    <script src="http://js.arcgis.com/3.11/"></script>
</head>

<body>
    <div id="map"></div>

    <script>
        var map;

        // Make sure to reference the tiles library within the require statement!
        require([
            "esri/map",
            "dojo/on",
            "esri/arcgis/utils",
            "../offline-editor-js-3/dist/offline-tiles-basic-src.js",
            "dojo/domReady!"],
            function(Map,on,arcgisUtils) {

                // Load the map
                arcgisUtils.createMap("bbc1a04a3eca4430be144d7a08b43a17","map").then(function(response){
                    var map = response.map;
                });
        });
    </script>
</body>
</html>

                        
                    
NOTE: Replace paths with your references.

Step 3: Extend basemap using the offline library

This initializes the offline-tiles-basic library and tells it which tiled map service layer to use for offline. Test to make sure map loads.


                        
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>
    <title>Simple Map</title>
    <link rel="stylesheet" href="http://js.arcgis.com/3.10/js/esri/css/esri.css">
    <style>
        html, body, #map {
            height: 100%;
            width: 100%;
            margin: 0;
            padding: 0;
        }
        body {
            background-color: #000000;
            overflow: hidden;
            font-family: "Trebuchet MS";
        }
    </style>

    <!-- Include a reference to offline.js which detects online/offline conditions -->
    <script src="../vendor/offline/offline.min.js"></script>
    <script>
        // Set the online/offline detection options.
        // More info at: http://github.hubspot.com/offline/docs/welcome/
        Offline.options = {
            checks: {
                image: {
                    url: function() {
                        return 'http://esri.github.io/offline-editor-js/tiny-image.png?_=' +
                            (Math.floor(Math.random() * 1000000000));
                    }
                },
                active: 'image'
            }
        }
    </script>

    <!-- Include a reference to IndexedDBShim for library to work on Safari 7.x -->
    <script src="../vendor/IndexedDBShim/dist/IndexedDBShim.js"></script>

    <script src="http://js.arcgis.com/3.10/"></script>
</head>

<body>
    <div id="map"></div>

    <script>
        var map;

        // Make sure to reference the tiles library within the require statement!
        require([
            "esri/map",
            "dojo/on",
            "esri/arcgis/utils",
            "../offline-editor-js-3/dist/offline-tiles-basic-src.js",
            "dojo/domReady!"],
            function(Map,on,arcgisUtils) {

                var basemapLayer;

                // Initialize the offline tiles library.
                var offlineTilesEnabler = new O.esri.Tiles.OfflineTilesEnabler();

                // Load the map
                arcgisUtils.createMap("bbc1a04a3eca4430be144d7a08b43a17","map").then(function(response){
                    var map = response.map;

                    // Get a reference to the primary tiled basemap layer
                    basemapLayer = map.getLayer( map.layerIds[0] );

                    // Extend the tiled basemap layer for offline use
                    offlineTilesEnabler.extend(basemapLayer,function(success) {
                        if (success) {
                            console.log("SUCCESS")
                        }
                    })
                });
        });
    </script>
</body>
</html>

                        
                    
NOTE: Replace paths with your references.

Step 4: Configure tiles download.

Enable the ability to download tiles as well the ability to toggle online and offline.



                            

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>
    <title>Simple Map</title>
    <link rel="stylesheet" href="http://js.arcgis.com/3.10/js/esri/css/esri.css">
    <style>
        html, body, #map {
            height: 100%;
            width: 100%;
            margin: 0;
            padding: 0;
        }
        body {
            background-color: #000000;
            overflow: hidden;
            font-family: "Trebuchet MS";
        }
    </style>

    <!-- Include a reference to offline.js which detects online/offline conditions -->
    <script src="../vendor/offline/offline.min.js"></script>
    <script>
        // Set the online/offline detection options.
        // More info at: http://github.hubspot.com/offline/docs/welcome/
        Offline.options = {
            checks: {
                image: {
                    url: function() {
                        return 'http://esri.github.io/offline-editor-js/tiny-image.png?_=' +
                            (Math.floor(Math.random() * 1000000000));
                    }
                },
                active: 'image'
            }
        }
    </script>

    <!-- Include a reference to IndexedDBShim for library to work on Safari 7.x -->
    <script src="../vendor/IndexedDBShim/dist/IndexedDBShim.js"></script>

    <script src="http://js.arcgis.com/3.10/"></script>
</head>

<body>
    <button class="basic-btn" id="btn-get-tiles">1. Download Tiles</button>
    <button class="basic-btn" id="btn-online-offline">2. Go Offline</button>
    <button class="basic-btn" id="btn-pan-left">3. Pan left</button>
    <div id="map"></div>

    <script>
        var map;

        // Make sure to reference the tiles library within the require statement!
        require([
            "esri/map",
            "dojo/on",
            "esri/arcgis/utils",
            "../offline-editor-js-3/dist/offline-tiles-basic-src.js",
            "dojo/domReady!"],
            function(Map,on,arcgisUtils) {

                var basemapLayer;

                // Check if browser state is online or offline
                Offline.check();
                Offline.on('up down', updateState );

                // Set up button click listeners.
                var btnGetTiles = document.getElementById("btn-get-tiles");
                var btnOnlineOffline = document.getElementById("btn-online-offline");
                var btnPanLeft = document.getElementById("btn-pan-left");

                on(btnGetTiles,"click",downloadTiles);
                on(btnOnlineOffline,"click",goOnlineOffline);
                on(btnPanLeft,"click",panLeft);

                // Initialize the offline tiles library.
                var offlineTilesEnabler = new O.esri.Tiles.OfflineTilesEnabler();

                // Load the map
                arcgisUtils.createMap("bbc1a04a3eca4430be144d7a08b43a17","map").then(function(response){
                    var map = response.map;

                    // Get a reference to the primary tiled basemap layer
                    basemapLayer = map.getLayer( map.layerIds[0] );

                    // Extend the tiled basemap layer for offline use
                    offlineTilesEnabler.extend(basemapLayer,function(success) {
                        if (success) {
                            console.log("SUCCESS")
                        }
                    })
                });

                // Force the tileLayer between online and offline
                function goOnlineOffline(){
                    if(btnOnlineOffline.innerHTML == "2. Go Offline"){
                        btnOnlineOffline.innerHTML = "2. Go Online";
                        basemapLayer.goOffline();
                        document.body.style.backgroundColor = "red";
                        console.log("Map is offline");
                    }
                    else{
                        btnOnlineOffline.innerHTML = "2. Go Offline";
                        basemapLayer.goOnline();
                        document.body.style.backgroundColor = "black";
                        console.log("Map is online");
                    }
                }

                // Set the ArcGIS.com map online or offline.
                // When set offline it will look for tiles in the tiles database
                function updateState(){
                    if(Offline.state === 'up'){
                        if(typeof basemapLayer != "undefined") basemapLayer.goOnline();
                    }
                    else{
                        if(typeof basemapLayer != "undefined") basemapLayer.goOffline();
                    }
                }

                function panLeft(){
                    map.panLeft();
                }

            } // main function
        );
    </script>
</body>
</html>

                            
                        
NOTE: Replace paths with your references.