Click or drag to resize

BookmarkCollection Class

A collection of bookmarks.
Inheritance Hierarchy
SystemObject
  Esri.ArcGISRuntimeRuntimeCollectionBookmark
    Esri.ArcGISRuntimeRuntimeObservableCollectionBookmark
      Esri.ArcGISRuntime.MappingBookmarkCollection

Namespace:  Esri.ArcGISRuntime.Mapping
Assembly:  Esri.ArcGISRuntime (in Esri.ArcGISRuntime.dll) Version: 100.7.0.0
Syntax
C#
public class BookmarkCollection : RuntimeObservableCollection<Bookmark>

The BookmarkCollection type exposes the following members.

Properties
Methods
  NameDescription
Public methodCode exampleAdd
Adds an item to the collection.
(Inherited from RuntimeCollectionT.)
Public methodAddRange (Inherited from RuntimeObservableCollectionT.)
Protected methodBlockReentrancy
Disallow reentrant attempts to change this collection. E.g. a event handler of the CollectionChanged event is not allowed to make changes to this collection.
(Inherited from RuntimeObservableCollectionT.)
Protected methodCheckReentrancy
Check and assert for reentrant attempts to change this collection.
(Inherited from RuntimeObservableCollectionT.)
Public methodCode exampleClear
Removes all items from the collection
(Inherited from RuntimeCollectionT.)
Protected methodClearItems
Called by base class Collection<T> when the list is being cleared; raises a CollectionChanged event to any listeners.
(Inherited from RuntimeObservableCollectionT.)
Public methodCode exampleContains
Determines whether the collection contains a specific value.
(Inherited from RuntimeCollectionT.)
Public methodCopyTo
Copies the elements of the collection to an Array, starting at a particular Array index.
(Inherited from RuntimeCollectionT.)
Public methodCode exampleGetEnumerator
Returns an enumerator that iterates through the collection.
(Inherited from RuntimeCollectionT.)
Public methodCode exampleIndexOf
Determines the index of a specific item in the collection.
(Inherited from RuntimeCollectionT.)
Public methodCode exampleInsert
Inserts an item to the collection at the specified index.
(Inherited from RuntimeCollectionT.)
Protected methodInsertItem
Called by base class Collection<T> when an item is added to list; raises a CollectionChanged event to any listeners.
(Inherited from RuntimeObservableCollectionT.)
Public methodMove
Moves the item at the specified index to a new location in the collection.
(Inherited from RuntimeCollectionT.)
Protected methodMoveItem
Called by base class ObservableCollection<T> when an item is to be moved within the list; raises a CollectionChanged event to any listeners.
(Inherited from RuntimeObservableCollectionT.)
Protected methodOnCollectionChanged
Raises the CollectionChanged event with the provided arguments.
(Inherited from RuntimeObservableCollectionT.)
Protected methodOnPropertyChanged
Raises a PropertyChanged event (per INotifyPropertyChanged).
(Inherited from RuntimeObservableCollectionT.)
Public methodCode exampleRemove
Removes the first occurrence of a specific object from the collection
(Inherited from RuntimeCollectionT.)
Public methodCode exampleRemoveAt
Removes the item at the specified index.
(Inherited from RuntimeCollectionT.)
Protected methodRemoveItem
Called by base class Collection<T> when an item is removed from list; raises a CollectionChanged event to any listeners.
(Inherited from RuntimeObservableCollectionT.)
Protected methodSetItem
Called by base class Collection<T> when an item is set in list; raises a CollectionChanged event to any listeners.
(Inherited from RuntimeObservableCollectionT.)
Top
Events
Examples

Android

Example Name: ManageBookmarks

Access and create bookmarks on a map.

Code example screen shot.

C#
// Copyright 2017 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.

using Android.App;
using Android.OS;
using Android.Views;
using Android.Widget;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.UI.Controls;
using System;

namespace ArcGISRuntime.Samples.ManageBookmarks
{
    [Activity (ConfigurationChanges=Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize)]
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        "Manage bookmarks",
        "Map",
        "This sample demonstrates how to access and add bookmarks to a map.",
        "")]
    public class ManageBookmarks : Activity
    {
        // Hold a reference to the map view.
        private MapView _myMapView;

        // Dialog for entering the name of a new bookmark
        private AlertDialog _newBookmarkDialog = null;

        // Button to show available bookmarks (in a menu)
        private Button _bookmarksButton;

        // Text input for the name of a new bookmark
        private EditText _bookmarkNameText;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            Title = "Manage bookmarks";

            // Create the UI
            CreateLayout();

            // Initialize the app
            Initialize();
        }

        private void Initialize()
        {
            // Create a new map with a World Imagery base map
            Map myMap = new Map(Basemap.CreateImageryWithLabels());

            // Add the map to the MapView
            _myMapView.Map = myMap;

            // Create a set of predefined bookmarks; each one follows the pattern of:
            // ~ Initialize a viewpoint pointing to a latitude longitude
            // ~ Create a new bookmark
            // ~ Give the bookmark a name
            // ~ Assign the viewpoint
            // ~ Add the bookmark to bookmark collection of the map

            // Bookmark-1
            Viewpoint myViewpoint1 = new Viewpoint(27.3805833, 33.6321389, 6000);
            Bookmark myBookmark1 = new Bookmark
            {
                Name = "Mysterious Desert Pattern",
                Viewpoint = myViewpoint1
            };
            _myMapView.Map.Bookmarks.Add(myBookmark1);

            // Bookmark-2
            Viewpoint myViewpoint2 = new Viewpoint(-39.299987, 174.060858, 600000);
            Bookmark myBookmark2 = new Bookmark
            {
                Name = "Dormant Volcano",
                Viewpoint = myViewpoint2
            };
            _myMapView.Map.Bookmarks.Add(myBookmark2);

            // Bookmark-3
            Viewpoint myViewpoint3 = new Viewpoint(-33.867886, -63.985, 40000);
            Bookmark myBookmark3 = new Bookmark
            {
                Name = "Guitar-Shaped Forest",
                Viewpoint = myViewpoint3
            };
            _myMapView.Map.Bookmarks.Add(myBookmark3);

            // Bookmark-4
            Viewpoint myViewpoint4 = new Viewpoint(44.525049, -110.83819, 6000);
            Bookmark myBookmark4 = new Bookmark
            {
                Name = "Grand Prismatic Spring",
                Viewpoint = myViewpoint4
            };
            _myMapView.Map.Bookmarks.Add(myBookmark4);
        }

        private void CreateLayout()
        {
            // Create a new vertical layout for the app
            LinearLayout layout = new LinearLayout(this) { Orientation = Orientation.Vertical };

            // Create button to show bookmarks
            _bookmarksButton = new Button(this)
            {
                Text = "Bookmarks"
            };
            _bookmarksButton.Click += OnBookmarksClicked;

            // Add bookmarks button to the layout
            layout.AddView(_bookmarksButton);

            // Add the map view to the layout
            _myMapView = new MapView(this);
            layout.AddView(_myMapView);

            // Show the layout in the app
            SetContentView(layout);
        }

        private void OnBookmarksClicked(object sender, EventArgs e)
        {
            // Create menu to show bookmarks
            PopupMenu bookmarksMenu = new PopupMenu(this, _bookmarksButton);
            bookmarksMenu.MenuItemClick += OnBookmarksMenuItemClicked;

            // Create a menu option for each of the map's bookmarks
            foreach (Bookmark mark in _myMapView.Map.Bookmarks)
            {
                bookmarksMenu.Menu.Add(mark.Name);
            }

            // Add a final menu item for adding a new bookmark for the current viewpoint
            bookmarksMenu.Menu.Add("Add ...");

            // Show menu in the view
            bookmarksMenu.Show();
        }

        private void OnBookmarksMenuItemClicked(object sender, PopupMenu.MenuItemClickEventArgs e)
        {
            // Get title from the selected item
            string selectedBookmarkName = e.Item.TitleCondensedFormatted.ToString();

            // If this is the "Add ..." choice, call a function to create a new bookmark
            if (selectedBookmarkName == "Add ...")
            {
                AddBookmark();
                return;
            }

            // Get the collection of bookmarks in the map
            BookmarkCollection myBookmarkCollection = _myMapView.Map.Bookmarks;

            // Loop through bookmarks
            foreach (Bookmark myBookmark in myBookmarkCollection)
            {
                // Get this bookmark name
                string theBookmarkName = myBookmark.Name;

                // If this is the selected bookmark, use it to set the map's viewpoint
                if (theBookmarkName == selectedBookmarkName)
                {
                    _myMapView.SetViewpoint(myBookmark.Viewpoint);

                    // Set the name of the button to display the current bookmark
                    _bookmarksButton.Text = selectedBookmarkName;

                    break;
                }
            }
        }

        private void AddBookmark()
        {
            // Create a dialog for entering the bookmark name
            AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);

            // Create the layout
            LinearLayout dialogLayout = new LinearLayout(this)
            {
                Orientation = Orientation.Vertical
            };

            // Create a layout for the text entry
            LinearLayout nameTextLayout = new LinearLayout(this)
            {
                Orientation = Orientation.Horizontal
            };
            nameTextLayout.SetPadding(10,0,10,0);

            // EditText control for entering the bookmark name
            _bookmarkNameText = new EditText(this);

            // Layout parameters for making views fill available width
            LinearLayout.LayoutParams fillWidthParam = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MatchParent,
                ViewGroup.LayoutParams.WrapContent,
                1.0f
            );
            _bookmarkNameText.LayoutParameters = fillWidthParam;

            // Label for the text entry
            TextView nameLabel = new TextView(this)
            {
                Text = "Name:"
            };

            // Add the controls to the layout
            nameTextLayout.AddView(nameLabel);
            nameTextLayout.AddView(_bookmarkNameText);

            // Create a layout for the dialog buttons (OK and Cancel)
            LinearLayout buttonLayout = new LinearLayout(this)
            {
                Orientation = Orientation.Horizontal
            };

            // Button to cancel the new bookmark
            Button cancelButton = new Button(this)
            {
                Text = "Cancel",
                LayoutParameters = fillWidthParam
            };
            cancelButton.Click += (s, e) => _newBookmarkDialog.Dismiss();

            // Button to save the current viewpoint as a new bookmark
            Button okButton = new Button(this)
            {
                Text = "OK",
                LayoutParameters = fillWidthParam
            };
            okButton.Click += CreateNewBookmark;

            // Add the buttons to the layout
            buttonLayout.AddView(cancelButton);
            buttonLayout.AddView(okButton);

            // Build the dialog with the text control and button layouts
            dialogLayout.AddView(nameTextLayout);
            dialogLayout.AddView(buttonLayout);

            // Set dialog content
            dialogBuilder.SetView(dialogLayout);
            dialogBuilder.SetTitle("New Bookmark");

            // Show the dialog 
            _newBookmarkDialog = dialogBuilder.Show();
        }

        // Handler for the click event of the New Bookmark dialog's OK button
        private void CreateNewBookmark(object sender, EventArgs e)
        {
            if (_newBookmarkDialog != null)
            {
                // See if the bookmark name conflicts with an existing name
                string bookmarkName = _bookmarkNameText.Text.Trim();
                bool nameExists = false;
                foreach (Bookmark bookmark in _myMapView.Map.Bookmarks)
                {
                    // See if this bookmark exists (or conflicts with the "Add ..." menu choice)
                    if (bookmarkName.ToLower() == bookmark.Name.ToLower() || bookmarkName.ToLower() == "add ...")
                    {
                        nameExists = true;
                        break;
                    }
                }

                // If the name is an empty string or exists in the collection, warn the user and return
                if (String.IsNullOrEmpty(bookmarkName) || nameExists)
                {
                    AlertDialog.Builder dlgBuilder = new AlertDialog.Builder(this);
                    dlgBuilder.SetTitle("Error");
                    dlgBuilder.SetMessage("Please enter a unique name for the bookmark.");
                    dlgBuilder.Show();
                    return;
                }

                // Create a new bookmark
                Bookmark newBookmark = new Bookmark();

                // Use the current viewpoint for the bookmark
                Viewpoint currentViewpoint = _myMapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry);
                newBookmark.Viewpoint = currentViewpoint;

                // Set the name with the value entered by the user
                newBookmark.Name = bookmarkName;

                // Add the bookmark to the map's bookmark collection
                _myMapView.Map.Bookmarks.Add(newBookmark);

                // Show this bookmark name as the button text
                _bookmarksButton.Text = bookmarkName;

                // Dismiss the dialog for entering the bookmark name
                _newBookmarkDialog.Dismiss();
            }
        }
    }
}
Examples

Xamarin Forms Android

Example Name: ManageBookmarks

Access and create bookmarks on a map.

Code example screen shot.

C#
// 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.

using Esri.ArcGISRuntime.Mapping;
using System;
using System.Linq;
using Xamarin.Forms;

namespace ArcGISRuntime.Samples.ManageBookmarks
{
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        "Manage bookmarks",
        "Map",
        "This sample demonstrates how to access and add bookmarks to a map.",
        "")]
    public partial class ManageBookmarks : ContentPage
    {
        public ManageBookmarks()
        {
            InitializeComponent();

            // Create the UI, setup the control references and execute initialization
            Initialize();
        }

        private void Initialize()
        {
            // Create new map with a base map
            Map myMap = new Map(Basemap.CreateImageryWithLabels());

            // Add the map to the mapview
            MyMapView.Map = myMap;

            // Create a set of predefined bookmarks; each one follows the pattern of:
            // ~ Initialize a viewpoint pointing to a latitude longitude
            // ~ Create a new bookmark
            // ~ Give the bookmark a name
            // ~ Assign the viewpoint
            // ~ Add the bookmark to bookmark collection of the map
            // ~ Add the bookmark name to the UI combo box for the user to choose from

            // Bookmark-1
            Viewpoint myViewpoint1 = new Viewpoint(27.3805833, 33.6321389, 6000);
            Bookmark myBookmark1 = new Bookmark
            {
                Name = "Mysterious Desert Pattern",
                Viewpoint = myViewpoint1
            };
            MyMapView.Map.Bookmarks.Add(myBookmark1);
            bookmarkPicker.Items.Add(myBookmark1.Name);

            // Bookmark-2
            Viewpoint myViewpoint2 = new Viewpoint(-39.299987, 174.060858, 600000);
            Bookmark myBookmark2 = new Bookmark
            {
                Name = "Dormant Volcano",
                Viewpoint = myViewpoint2
            };
            MyMapView.Map.Bookmarks.Add(myBookmark2);
            bookmarkPicker.Items.Add(myBookmark2.Name);

            // Bookmark-3
            Viewpoint myViewpoint3 = new Viewpoint(-33.867886, -63.985, 40000);
            Bookmark myBookmark3 = new Bookmark
            {
                Name = "Guitar-Shaped Forest",
                Viewpoint = myViewpoint3
            };
            MyMapView.Map.Bookmarks.Add(myBookmark3);
            bookmarkPicker.Items.Add(myBookmark3.Name);

            // Bookmark-4
            Viewpoint myViewpoint4 = new Viewpoint(44.525049, -110.83819, 6000);
            Bookmark myBookmark4 = new Bookmark
            {
                Name = "Grand Prismatic Spring",
                Viewpoint = myViewpoint4
            };
            MyMapView.Map.Bookmarks.Add(myBookmark4);
            bookmarkPicker.Items.Add(myBookmark4.Name);

            // Set the initial combo box selection to the last bookmark added
            bookmarkPicker.SelectedIndex = 3;

            // Zoom to the last bookmark
            myMap.InitialViewpoint = myMap.Bookmarks.Last().Viewpoint;
        }

        private void BookmarkPicker_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Get the selected bookmarks name
            string selectedBookmarkName = bookmarkPicker.Items[bookmarkPicker.SelectedIndex];

            // Get the collection of bookmarks in the map
            BookmarkCollection myBookmarkCollection = MyMapView.Map.Bookmarks;

            // Loop through each bookmark
            foreach (Bookmark myBookmark in myBookmarkCollection)
            {
                // Get the bookmarks name
                string theBookmarkName = myBookmark.Name;

                // If the selected bookmarks name matches one in the bookmark collection
                // set that to be the maps viewpoint
                if (theBookmarkName == selectedBookmarkName.ToString())
                {
                    MyMapView.SetViewpoint(myBookmark.Viewpoint);
                }
            }
        }

        // Member (aka. global) variables for the dynamically created page allowing
        // the user to add additional bookmarks
        public Entry myEntryBookmarkName;

        public ContentPage bookmarkAddPage;

        private async void ButtonAddBookmark_Clicked(object sender, EventArgs e)
        {
            // Create root layout
            StackLayout layout = new StackLayout();

            // Label for the UI to let the user know to add a bookmark
            Label myLabel = new Label
            {
                Text = "Bookmark Name:"
            };
            layout.Children.Add(myLabel);

            // Entry location for the user to enter the bookmark name
            myEntryBookmarkName = new Entry();
            layout.Children.Add(myEntryBookmarkName);

            // Button to accept the users bookmark name
            Button okButton = new Button
            {
                Text = "OK"
            };
            okButton.Clicked += OkButton_Clicked;
            layout.Children.Add(okButton);

            // Create internal page for the navigation page
            bookmarkAddPage = new ContentPage()
            {
                Content = layout,
                Title = "Add Bookmark"
            };

            // Navigate to the dynamically created user interaction page
            await Navigation.PushAsync(bookmarkAddPage);
        }

        private void OkButton_Clicked(object sender, EventArgs e)
        {
            // Get the name from the text field
            string name = myEntryBookmarkName.Text;

            // Exit if the name is empty
            if (String.IsNullOrEmpty(name))
                return;

            // Check to see if there is a bookmark with same name
            bool doesNameExist = MyMapView.Map.Bookmarks.Any(b => b.Name == name);
            if (doesNameExist)
                return;

            // Create a new bookmark
            Bookmark myBookmark = new Bookmark
            {
                Name = name,

                // Get the current viewpoint from map and assign it to bookmark
                Viewpoint = MyMapView.GetCurrentViewpoint(ViewpointType.BoundingGeometry)
            };

            // Add the bookmark to bookmark collection of the map
            MyMapView.Map.Bookmarks.Add(myBookmark);

            // Add the bookmark name to the list of choices in the picker
            bookmarkPicker.Items.Add(name);

            // Close the user interaction page to add a bookmark
            Navigation.RemovePage(bookmarkAddPage);
            bookmarkAddPage = null;
        }
    }
}
XAML
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"             
             xmlns:esriUI="clr-namespace:Esri.ArcGISRuntime.Xamarin.Forms;assembly=Esri.ArcGISRuntime.Xamarin.Forms"
             xmlns:mapping="clr-namespace:Esri.ArcGISRuntime.Mapping;assembly=Esri.ArcGISRuntime" 
             x:Class="ArcGISRuntime.Samples.ManageBookmarks.ManageBookmarks">
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="auto" />
      <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Grid Grid.Row="0">
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="200"/>
        <ColumnDefinition/>
      </Grid.ColumnDefinitions>
      <Label Grid.Column="0"
              HorizontalTextAlignment="Center" VerticalTextAlignment="Center"
              Text="Go to:" />
      <Picker x:Name="bookmarkPicker"
                Grid.Column="1"
              SelectedIndexChanged="BookmarkPicker_SelectedIndexChanged"/>
      <Button x:Name="ButtonAddBookmark" 
                Grid.Column="2"
              Text="New ..." 
              Clicked="ButtonAddBookmark_Clicked" />
    </Grid>
    <esriUI:MapView x:Name="MyMapView" Grid.Row="1"/>
  </Grid>
</ContentPage>
See Also
Additional Examples
Hyperlink to ExampleDescription
ManageBookmarksAccess and create bookmarks on a map.