Export tiles

View inJavaKotlinView on GitHubSample viewer app

Download tiles to a local tile cache file stored on the device.

Image of export tiles

Use case

Field workers with limited network connectivity can use exported tiles as a basemap for use offline.

How to use the sample

Pan and zoom into the desired area, making sure the area is within the red boundary. Click the 'Export tiles' button to start the process. On successful completion you will see a preview of the downloaded tile package.

How it works

  1. Create a map and set its minScale to 10,000,000. Limiting the scale in this sample limits the potential size of the selection area, thereby keeping the exported tile package to a reasonable size.
  2. Create an ExportTileCacheTask, passing in the URI of the tiled layer.
  3. Create default ExportTileCacheParameters for the task, specifying extent, minimum scale and maximum scale.
  4. Use the parameters and a path to create an ExportTileCacheJob from the task.
  5. Start the job, and when it completes successfully, get the resulting TileCache.
  6. Use the tile cache to create an ArcGISTiledLayer, and display it in the map.

Relevant API

  • ArcGISTiledLayer
  • ExportTileCacheJob
  • ExportTileCacheParameters
  • ExportTileCacheTask
  • TileCache

Additional information

ArcGIS tiled layers do not support reprojection, query, select, identify, or editing. See the Layer types discussion in the developers guide to learn more about the characteristics of ArcGIS tiled layers.

Tags

cache, download, offline

Sample Code

MainActivity.kt
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
 * Copyright 2020 Esri
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.esri.arcgisruntime.sample.exporttiles

import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment
import com.esri.arcgisruntime.concurrent.Job
import com.esri.arcgisruntime.concurrent.ListenableFuture
import com.esri.arcgisruntime.data.TileCache
import com.esri.arcgisruntime.geometry.Envelope
import com.esri.arcgisruntime.geometry.Point
import com.esri.arcgisruntime.layers.ArcGISTiledLayer
import com.esri.arcgisruntime.loadable.LoadStatus
import com.esri.arcgisruntime.mapping.ArcGISMap
import com.esri.arcgisruntime.mapping.Basemap
import com.esri.arcgisruntime.mapping.BasemapStyle
import com.esri.arcgisruntime.mapping.Viewpoint
import com.esri.arcgisruntime.mapping.view.Graphic
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay
import com.esri.arcgisruntime.mapping.view.MapView
import com.esri.arcgisruntime.sample.exporttiles.databinding.ActivityMainBinding
import com.esri.arcgisruntime.sample.exporttiles.databinding.ExportTilesDialogLayoutBinding
import com.esri.arcgisruntime.symbology.SimpleLineSymbol
import com.esri.arcgisruntime.tasks.tilecache.ExportTileCacheJob
import com.esri.arcgisruntime.tasks.tilecache.ExportTileCacheParameters
import com.esri.arcgisruntime.tasks.tilecache.ExportTileCacheTask
import java.io.File

class MainActivity : AppCompatActivity() {

    private val TAG: String = MainActivity::class.java.simpleName
    private var exportTileCacheJob: ExportTileCacheJob? = null
    private var downloadArea: Graphic? = null

    private val activityMainBinding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }

    private val exportTilesButton: Button by lazy {
        activityMainBinding.exportTilesButton
    }

    private val mapPreviewLayout: ConstraintLayout by lazy {
        activityMainBinding.mapPreviewLayout
    }

    private val previewTextView: TextView by lazy {
        activityMainBinding.previewTextView
    }

    private val mapView: MapView by lazy {
        activityMainBinding.mapView
    }

    private val closeButton: Button by lazy {
        activityMainBinding.closeButton
    }

    private val previewMapView: MapView by lazy {
        activityMainBinding.previewMapView
    }

    private val dimBackground: View by lazy {
        activityMainBinding.dimBackground
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(activityMainBinding.root)

        // authentication with an API key or named user is required
        // to access basemaps and other location services
        ArcGISRuntimeEnvironment.setApiKey(BuildConfig.API_KEY)

        // create an ArcGISTiledLayer to use as the basemap

        val map = ArcGISMap().apply {
            basemap = Basemap(BasemapStyle.ARCGIS_IMAGERY)
            minScale = 10000000.0
        }

        // create a graphic and graphics overlay to show a red box around the tiles to be downloaded
        downloadArea = Graphic()
        downloadArea?.symbol = SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, Color.RED, 2f)
        val graphicsOverlay = GraphicsOverlay()
        graphicsOverlay.graphics.add(downloadArea)

        // set the map to the map view
        mapView.map = map
        mapView.setViewpoint(Viewpoint(35.0, -117.0, 10000000.0))
        // add the graphics overlay to the map view
        mapView.graphicsOverlays.add(graphicsOverlay)

        mapView.addViewpointChangedListener {
            updateDownloadAreaGeometry()
        }

        map.addDoneLoadingListener {

            val tiledLayer: ArcGISTiledLayer = map.basemap.baseLayers[0] as ArcGISTiledLayer
            // when the button is clicked, export the tiles to the temporary directory
            exportTilesButton.setOnClickListener {

                updateDownloadAreaGeometry()
                val exportTileCacheTask = ExportTileCacheTask(tiledLayer.uri)
                // set up the export tile cache parameters
                val parametersFuture: ListenableFuture<ExportTileCacheParameters> =
                    exportTileCacheTask.createDefaultExportTileCacheParametersAsync(
                        downloadArea?.geometry,
                        mapView.mapScale,
                        mapView.mapScale * 0.1
                    )

                parametersFuture.addDoneListener {
                    try {
                        val parameters: ExportTileCacheParameters = parametersFuture.get()
                        // create a temporary directory in the app's cache for saving exported tiles
                        val exportTilesDirectory =
                            File(externalCacheDir, getString(R.string.tile_cache_folder))

                        // export tiles to temporary cache on device
                        exportTileCacheJob =
                            exportTileCacheTask.exportTileCache(
                                parameters,
                                exportTilesDirectory.path + "file.tpkx"
                            ).apply {
                                // start the export tile cache job
                                start()

                                val dialogLayoutBinding =
                                    ExportTilesDialogLayoutBinding.inflate(layoutInflater)
                                // show progress of the export tile cache job on the progress bar
                                val dialog = createProgressDialog(this)
                                dialog.setView(dialogLayoutBinding.root)
                                dialog.show()

                                // on progress change
                                addProgressChangedListener {
                                    dialogLayoutBinding.progressBar.progress = progress
                                    dialogLayoutBinding.progressTextView.text = "$progress%"
                                }

                                // when the job has completed, close the dialog and show the job result in the map preview
                                addJobDoneListener {
                                    dialog.dismiss()
                                    if (status == Job.Status.SUCCEEDED) {
                                        showMapPreview(result)
                                        downloadArea?.isVisible = false

                                    } else {
                                        ("Job did not succeed: " + error.additionalMessage).also {
                                            Toast.makeText(this@MainActivity, it, Toast.LENGTH_LONG)
                                                .show()
                                            Log.e(TAG, error.additionalMessage)
                                        }
                                    }
                                }
                            }
                    } catch (e: Exception) {
                        val error = "Error generating tile cache parameters: ${e.message}"
                        Toast.makeText(this, error, Toast.LENGTH_LONG).show()
                        Log.e(TAG, error)
                    }
                }
            }
        }

        // get correct view order set up on start
        clearPreview(mapView)
    }

    /**
     * Updates the [downloadArea]'s geometry on ViewPoint change
     * or when export tiles is clicked.
     */
    private fun updateDownloadAreaGeometry() {
        try {
            mapView.apply {
                if (mapView.map.loadStatus == LoadStatus.LOADED) {
                    // upper left corner of the downloaded tile cache area
                    val minScreenPoint: android.graphics.Point = android.graphics.Point(150, 175)
                    // lower right corner of the downloaded tile cache area
                    val maxScreenPoint: android.graphics.Point = android.graphics.Point(
                        mapView.width - 150,
                        mapView.height - 250
                    )
                    // convert screen points to map points
                    val minPoint: Point = mapView.screenToLocation(minScreenPoint)
                    val maxPoint: Point = mapView.screenToLocation(maxScreenPoint)
                    // use the points to define and return an envelope
                    downloadArea?.geometry = Envelope(minPoint, maxPoint)
                }
            }
        } catch (e: java.lang.Exception) {
            // Silently fail, since mapView has not been rendered yet.
        }
    }

    /**
     * Show tile cache preview window including containing the exported tiles.
     *
     * @param result takes the TileCache from the ExportTileCacheJob
     */
    private fun showMapPreview(result: TileCache) {

        // set up the preview map view
        previewMapView.apply {
            map = ArcGISMap(Basemap(ArcGISTiledLayer(result)))
            setViewpoint(mapView.getCurrentViewpoint(Viewpoint.Type.CENTER_AND_SCALE))
        }
        // control UI visibility
        previewMapView.getChildAt(0).visibility = View.VISIBLE
        show(closeButton, dimBackground, previewTextView, previewMapView)
        exportTilesButton.visibility = View.GONE

        // required for some Android devices running older OS (combats Z-ordering bug in Android API)
        mapPreviewLayout.bringToFront()
    }

    /**
     * Clear the preview window.
     */
    fun clearPreview(view: View) {

        previewMapView.getChildAt(0).visibility = View.INVISIBLE
        hide(closeButton, dimBackground, previewTextView, previewMapView)
        // control UI visibility
        show(exportTilesButton, mapView)
        downloadArea?.isVisible = true

        // required for some Android devices running older OS (combats Z-ordering bug in Android API)
        mapView.bringToFront()
    }

    /**
     * Create a progress dialog box for tracking the export tile cache job.
     *
     * @param exportTileCacheJob the export tile cache job progress to be tracked
     * @return an AlertDialog set with the dialog layout view
     */
    private fun createProgressDialog(exportTileCacheJob: ExportTileCacheJob): AlertDialog {
        val builder = AlertDialog.Builder(this@MainActivity).apply {
            setTitle("Exporting tiles...")
            // provide a cancel button on the dialog
            setNeutralButton("Cancel") { _, _ ->
                exportTileCacheJob.cancelAsync()
            }
            setCancelable(false)
            val dialogLayoutBinding = ExportTilesDialogLayoutBinding.inflate(layoutInflater)
            setView(dialogLayoutBinding.root)
        }
        return builder.create()
    }

    /**
     * Makes the given views in the UI visible.
     *
     * @param views the views to be made visible
     */
    private fun show(vararg views: View) {
        for (view in views) {
            view.visibility = View.VISIBLE
        }
    }

    /**
     * Makes the given views in the UI visible.
     *
     * @param views the views to be made visible
     */
    private fun hide(vararg views: View) {
        for (view in views) {
            view.visibility = View.INVISIBLE
        }
    }

    override fun onResume() {
        super.onResume()
        mapView.resume()
        previewMapView.resume()
    }

    override fun onPause() {
        mapView.pause()
        previewMapView.pause()
        super.onPause()
    }

    override fun onDestroy() {
        mapView.dispose()
        super.onDestroy()
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.