Manage bookmarks

View on GitHubSample viewer app

Access and create bookmarks on a map.

Image of manage bookmarks

Use case

Bookmarks are used for easily storing and accessing saved locations on the map. Bookmarks are of interest in educational apps (e.g. touring historical sites) or more specifically, for a land management company wishing to visually monitor flood levels over time at a particular location. These locations can be saved as bookmarks and revisited easily each time their basemap data has been updated (e.g. working with up to date satellite imagery to monitor water levels).

How to use the sample

The map in the sample comes pre-populated with a set of bookmarks. To access a bookmark and move to that location, tap on a bookmark's name from the list. To add a bookmark, pan and/or zoom to a new location and tap on the 'Add Bookmark' button. Enter a unique name for the bookmark and tap ok, and the bookmark will be added to the list

How it works

  1. Instantiate a new ArcGISMap object and create a BookmarkList with ArcGISMap.getBookmarks().
  2. To create a new bookmark and add it to the bookmark list:
    • Instantiate a new Bookmark object passing in text (the name of the bookmark) and a Viewpoint as parameters.
    • Add the new bookmark to the book mark list with BookmarkList.add(bookmark).

Relevant API

  • Bookmark
  • BookmarkList
  • Viewpoint

Tags

bookmark, extent, location, zoom

Sample Code

MainActivity.java
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
/* 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.managebookmarks;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.BasemapStyle;
import com.esri.arcgisruntime.mapping.Bookmark;
import com.esri.arcgisruntime.mapping.BookmarkList;
import com.esri.arcgisruntime.mapping.Viewpoint;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

public class MainActivity extends AppCompatActivity {

  private MapView mMapView;
  private BookmarkList mBookmarks;
  private List<String> mBookmarksSpinnerList;
  private Bookmark mBookmark;
  private ArrayAdapter<String> mDataAdapter;

  @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 MapView from layout
    mMapView = findViewById(R.id.mapView);

    // create a map with the Basemap Style imagery with labels
    ArcGISMap mMap = new ArcGISMap(BasemapStyle.ARCGIS_IMAGERY);
    // set the map to be displayed in this view
    mMapView.setMap(mMap);

    // inflate the floating action button
    FloatingActionButton addBookmarkFab = findViewById(R.id.addbookmarkFAB);

    // show the dialog for acquiring bookmark name from the user
    addBookmarkFab.setOnClickListener(v -> showDialog(v.getContext()));

    // get the maps BookmarkList
    mBookmarks = mMap.getBookmarks();

    // add some default bookmarks to the map
    addDefaultBookmarks();

    // populate the spinner list with default bookmark names
    Spinner bookmarksSpinner = findViewById(R.id.bookmarksspinner);
    mBookmarksSpinnerList = new ArrayList<>();
    mBookmarksSpinnerList.add(mBookmarks.get(0).getName());
    mBookmarksSpinnerList.add(mBookmarks.get(1).getName());
    mBookmarksSpinnerList.add(mBookmarks.get(2).getName());
    mBookmarksSpinnerList.add(mBookmarks.get(3).getName());

    // initialize the adapter for the bookmarks spinner
    mDataAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, mBookmarksSpinnerList);
    mDataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    bookmarksSpinner.setAdapter(mDataAdapter);

    // when an item is selected in the spinner set the mapview viewpoint to the selected bookmark's viewpoint
    bookmarksSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
      @Override
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        mMapView.setBookmarkAsync(mBookmarks.get(position));
      }

      @Override
      public void onNothingSelected(AdapterView<?> parent) {
      }
    });
  }

  /**
   * adds the default bookmarks to the maps BookmarkList
   */
  private void addDefaultBookmarks() {

    Viewpoint viewpoint;

    //Mysterious Desert Pattern
    viewpoint = new Viewpoint(27.3805833, 33.6321389, 6e3);
    mBookmark = new Bookmark(getResources().getString(R.string.desert_pattern), viewpoint);
    mBookmarks.add(mBookmark);
    // Set the viewpoint to the default bookmark selected in the spinner
    mMapView.setBookmarkAsync(mBookmarks.get(0));

    //Strange Symbol
    viewpoint = new Viewpoint(37.401573, -116.867808, 6e3);
    mBookmark = new Bookmark(getResources().getString(R.string.strange_symbol), viewpoint);
    mBookmarks.add(mBookmark);

    //Guitar-Shaped Trees
    viewpoint = new Viewpoint(-33.867886, -63.985, 4e4);
    mBookmark = new Bookmark(getResources().getString(R.string.guitar_trees), viewpoint);
    mBookmarks.add(mBookmark);

    //Grand Prismatic Spring
    viewpoint = new Viewpoint(44.525049, -110.83819, 6e3);
    mBookmark = new Bookmark(getResources().getString(R.string.prismatic_spring), viewpoint);
    mBookmarks.add(mBookmark);

  }

  /**
   * add a new bookmark at the location being displayed in the MapView's current Viewpoint
   *
   * @param Name of the new bookmark
   */
  private void addBookmark(String Name) {

    mBookmark = new Bookmark(Name, mMapView.getCurrentViewpoint(Viewpoint.Type.BOUNDING_GEOMETRY));
    mBookmarks.add(mBookmark);
    mBookmarksSpinnerList.add(Name);
    mDataAdapter.notifyDataSetChanged();
  }

  /**
   * shows dialog that prompts user to add a name for the new Bookmark
   */
  private void showDialog(Context context) {

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(getResources().getString(R.string.alert_dialog_title));

    // Set up the input
    final EditText input = new EditText(this);
    input.setInputType(InputType.TYPE_CLASS_TEXT);
    builder.setView(input);

    // Set up the buttons
    builder.setPositiveButton("OK", (dialog, which) -> {
      // get the input from EditText
      String bookmarkName = input.getText().toString();
      // check if EditText is not empty & bookmark name has not been used
      if (bookmarkName.length() > 0 && !mBookmarksSpinnerList.contains(bookmarkName)) {
        addBookmark(bookmarkName);
      } else {
        // display toast explaining bookmark not set
        Toast.makeText(getApplicationContext(), getResources().getString(R.string.bookmark_not_saved),
            Toast.LENGTH_LONG).show();
        dialog.cancel();
      }
    });
    builder.setNegativeButton("Cancel", (dialog, which) -> dialog.cancel());

    builder.show();

  }

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

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

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

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