Open existing map

View on GitHub
Sample viewer app

Display a web map.

Image of open map URL

Use case

For displaying web maps stored on ArcGISOnline (e.g. soil survey results, as demonstrated in this sample).

How to use the sample

A web map can be selected from the navigation menu. On selection the web map displays in the map view.

How it works

  1. Create a Portal.
  2. Create a PortalItem using the Portal and the web map ID: new PortalItem(portal, ID).
  3. Create a ArcGISMap using the portal item.
  4. Set the map to the MapView.

Relevant API

  • ArcGISMap
  • MapView
  • Portal
  • PortalItem

Additional information

Mobile map packages can also include address locators and geometric networks. For an example of this, see the "Mobile Map Search and Route Sample".

Tags

portal item, web map

Sample Code

MainActivity.java
                                                                                                                                                                       
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
/* Copyright 2016 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.openexistingmap;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.portal.Portal;
import com.esri.arcgisruntime.portal.PortalItem;

public class MainActivity extends AppCompatActivity {

  private MapView mMapView;
  private ArcGISMap mMap;
  private Portal mPortal;
  private PortalItem mPortalItem;

  private ListView mDrawerList;

  private ActionBarDrawerToggle mDrawerToggle;
  private DrawerLayout mDrawerLayout;
  private String mActivityTitle;

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

    mDrawerList = findViewById(R.id.navList);
    mDrawerLayout = findViewById(R.id.drawer_layout);
    mActivityTitle = getTitle().toString();

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

    // get the portal url for ArcGIS Online
    mPortal = new Portal(getResources().getString(R.string.portal_url));
    // get the pre-defined portal id and portal url
    mPortalItem = new PortalItem(mPortal, getResources().getString(R.string.webmap_houses_with_mortgages_id));
    // create a map from a PortalItem
    mMap = new ArcGISMap(mPortalItem);
    // set the map to be displayed in this view
    mMapView.setMap(mMap);

    // add the webmap titles to the drawer
    addDrawerItems();
    setupDrawer();

    // set icons on action bar
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
  }

  private void addDrawerItems() {
    String[] webmapTitles = { getResources().getString(R.string.webmap_houses_with_mortgages_title),
        getResources().getString(R.string.webmap_usa_tapestry_segmentation_title),
        getResources().getString(R.string.webmap_geology_us_title) };
    ArrayAdapter<String> mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, webmapTitles);
    mDrawerList.setAdapter(mAdapter);

    mDrawerList.setOnItemClickListener((adapterView, view, position, id) -> {
      if (position == 0) {
        mPortalItem = new PortalItem(mPortal, getResources().getString(R.string.webmap_houses_with_mortgages_id));
        // create a map from a PortalItem
        mMap = new ArcGISMap(mPortalItem);
        // set the map to be displayed in this view
        mMapView.setMap(mMap);
        // close the drawer
        mDrawerLayout.closeDrawer(adapterView);
      } else if (position == 1) {
        mPortalItem = new PortalItem(mPortal, getResources().getString(R.string.webmap_usa_tapestry_segmentation_id));
        // create a map from a PortalItem
        mMap = new ArcGISMap(mPortalItem);
        // set the map to be displayed in this view
        mMapView.setMap(mMap);
        // close the drawer
        mDrawerLayout.closeDrawer(adapterView);
      } else if (position == 2) {
        mPortalItem = new PortalItem(mPortal, getResources().getString(R.string.webmap_geology_us));
        // create a map from a PortalItem
        mMap = new ArcGISMap(mPortalItem);
        // set the map to be displayed in this view
        mMapView.setMap(mMap);
        // close the drawer
        mDrawerLayout.closeDrawer(adapterView);
      }
    });
  }

  private void setupDrawer() {
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {

      // called when drawer has settled in an open state
      @Override public void onDrawerOpened(View drawerView) {
        super.onDrawerOpened(drawerView);
        // change the title to the nav bar
        getSupportActionBar().setTitle(getResources().getString(R.string.navbar_title));
        // invalidate options menu
        invalidateOptionsMenu();
      }

      // called when drawer has settled in a closed state
      @Override public void onDrawerClosed(View view) {
        super.onDrawerClosed(view);
        // set title to the app
        getSupportActionBar().setTitle(mActivityTitle);
        // invalidate options menu
        invalidateOptionsMenu();
      }
    };
    // enable draw indicator
    mDrawerToggle.setDrawerIndicatorEnabled(true);
    // attach toggle to drawer layout
    mDrawerLayout.setDrawerListener(mDrawerToggle);
  }

  @Override
  protected void onPause() {
    super.onPause();
    mMapView.pause();
  }

  @Override
  protected void onResume() {
    super.onResume();
    mMapView.resume();
  }

  @Override protected void onDestroy() {
    super.onDestroy();
    mMapView.dispose();
  }

  @Override
  protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    // activate the navigation drawer toggle
    return mDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);

  }
}

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