Change basemaps
Change a map's basemap. A basemap is beneath all layers on an ArcGISMap
and is used to provide visual reference for the operational layers.
Use case
Basemaps should be selected contextually. For example in maritime applications it would be more appropriate to use a basemap of the world's oceans, as opposed to a basemap of the world's streets.
How to use the sample
Use the navigation drawer to select the active basemap from the list of available basemaps.
How it works
- Create an
ArcGISMap
object. - Set the map to the
MapView
object. - Choose a new basemap type and set it on the map.
Relevant API
- ArcGISMap
- BasemapStyle
- MapView
Tags
basemap, 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
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
/* 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.switchbasemaps;
import android.content.res.Configuration;
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.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.BasemapStyle;
import com.esri.arcgisruntime.mapping.view.MapView;
public class MainActivity extends AppCompatActivity {
private MapView mMapView;
private ArcGISMap mMap;
private String[] mNavigationDrawerItemTitles;
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);
// authentication with an API key or named user is required to access basemaps and other
// location services
ArcGISRuntimeEnvironment.setApiKey(BuildConfig.API_KEY);
// inflate navigation drawer
mNavigationDrawerItemTitles = getResources().getStringArray(R.array.basemap_types);
mDrawerList = findViewById(R.id.navList);
mDrawerLayout = findViewById(R.id.drawer_layout);
// get app title
mActivityTitle = getTitle().toString();
addDrawerItems();
setupDrawer();
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
// set opening basemap title to Topographic
getSupportActionBar().setTitle(mNavigationDrawerItemTitles[2]);
}
// inflate MapView from layout
mMapView = findViewById(R.id.mapView);
// create a map with Topographic Basemap
mMap = new ArcGISMap(Basemap.Type.TOPOGRAPHIC, 47.6047381, -122.3334255, 12);
// set the map to be displayed in this view
mMapView.setMap(mMap);
}
/**
* Add navigation drawer items
*/
private void addDrawerItems() {
ArrayAdapter<String> mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
mNavigationDrawerItemTitles);
mDrawerList.setAdapter(mAdapter);
mDrawerList.setOnItemClickListener((adapterView, view, position, id) -> selectBasemap(position));
}
/**
* Set up the navigation drawer
*/
private void setupDrawer() {
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
/** Called when a drawer has settled in a completely open state. */
@Override public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle(mActivityTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely closed state. */
@Override public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.addDrawerListener(mDrawerToggle);
}
/**
* Select the Basemap item based on position in the navigation drawer
*
* @param position order int in navigation drawer
*/
private void selectBasemap(int position) {
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerLayout.closeDrawer(mDrawerList);
// if-else is used because this sample is used elsewhere as a Library module
if (position == 0) {
// position 0 = Streets
mMap.setBasemap(new Basemap(BasemapStyle.ARCGIS_STREETS));
getSupportActionBar().setTitle(mNavigationDrawerItemTitles[0]);
} else if (position == 1) {
// position 1 = Navigation Vector
mMap.setBasemap(new Basemap(BasemapStyle.ARCGIS_NAVIGATION));
getSupportActionBar().setTitle(mNavigationDrawerItemTitles[1]);
} else if (position == 2) {
// position 2 = Topographic
mMap.setBasemap(new Basemap(BasemapStyle.ARCGIS_TOPOGRAPHIC));
getSupportActionBar().setTitle(mNavigationDrawerItemTitles[2]);
} else if (position == 3) {
// position 3 = Terrain
mMap.setBasemap(new Basemap(BasemapStyle.ARCGIS_TERRAIN));
getSupportActionBar().setTitle(mNavigationDrawerItemTitles[3]);
} else if (position == 4) {
// position 4 = Gray Canvas
mMap.setBasemap(new Basemap(BasemapStyle.ARCGIS_LIGHT_GRAY));
getSupportActionBar().setTitle(mNavigationDrawerItemTitles[4]);
} else if (position == 5) {
// position 5 = OSM Light Gray
mMap.setBasemap(new Basemap(BasemapStyle.OSM_LIGHT_GRAY));
getSupportActionBar().setTitle(mNavigationDrawerItemTitles[5]);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Activate the navigation drawer toggle
return mDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
protected void onResume() {
super.onResume();
mMapView.resume();
}
@Override
protected void onPause() {
super.onPause();
mMapView.pause();
}
@Override
protected void onDestroy() {
super.onDestroy();
mMapView.dispose();
}
}