Help with downloading a map for offline use within an app?

445
1
04-16-2018 02:35 PM
CoraColeman
New Contributor III

Hello, here is what my code in my onCreate method looks like now (I am following the code found on the Create an offline map guide page). I am using a button to begin the downloading process...I am confused because I do not find that the map is downloaded in the Downloads folder -- this code doesn't do anything but display my portal item:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_offline);

    // inflate MapView from layout
    aMapView = (MapView) findViewById(R.id.mapView);

    // get the portal url for ArcGIS Online
    aPortal = new Portal(getResources().getString(R.string.portal_url));
    // get the pre-defined portal id and portal url
    aPortalItem = new PortalItem(aPortal, getResources().getString(R.string.webmap_glacier_national_park_id));
    // create a map from a PortalItem
    map = new ArcGISMap(aPortalItem);
    // set the map to be displayed in this view
    aMapView.setMap(map);
    Log.d("MAP SET", "we have set our map!");
final Button startdownload = (Button) findViewById(R.id.startdownload);
startdownload.setOnClickListener(v -> {   
// create an offline map task
        OfflineMapTask offlineMapTask = new OfflineMapTask(map);

        //get all of the preplanned map areas in the web map
        ListenableFuture<List<PreplannedMapArea>> mapAreasFuture = offlineMapTask.getPreplannedMapAreasAsync();
        mapAreasFuture.addDoneListener(() -> {
            try {
                // get the list of areas
                List<PreplannedMapArea> mapAreas = mapAreasFuture.get();

                // loop through the map areas
                for (PreplannedMapArea mapArea : mapAreas) {
                    // load each map area
                    mapArea.loadAsync();

                    // create the job
                    DownloadPreplannedOfflineMapJob downloadJob =
                            offlineMapTask.downloadPreplannedOfflineMap(mapArea, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString());

                    //start the job
                    downloadJob.start();

                    // listen for job complete
                    downloadJob.addJobDoneListener(() -> {
                        // get the download result
                        DownloadPreplannedOfflineMapResult downloadResult = downloadJob.getResult();

                        // check for errors
                        if (downloadResult.hasErrors()) {
                            // code here to examine errors:
                            Map<Layer, ArcGISRuntimeException> layerErrors = downloadResult.getLayerErrors();
                            Map<FeatureTable, ArcGISRuntimeException> tableErrors = downloadResult.getTableErrors();

                        } else {
                            // no errors so use the map straight away
                            map = downloadResult.getOfflineMap();

                        }
                    });

                    mapArea.addDoneLoadingListener(() -> {
                        // get the map area geometry so the is can used to display a graphic on the map
                        Geometry areaGeometry = mapArea.getAreaOfInterest();

                        // get the area title so it can be used in a UI component
                        String areaTitle = mapArea.getPortalItem().getTitle();

                        // UI code for showing map areas goes here:
                    });
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        });
    });
}
Tags (1)
1 Reply
AnastasiiaChechulina
New Contributor

try to change

map = downloadResult.getOfflineMap()

to

aMapView.setMap(downloadResult.getOfflineMap())

and you have to wait some minutes for area to be downloaded
0 Kudos