Select to view content in your preferred language

Custom bookmark addin?

976
3
01-08-2014 10:09 AM
KarenEllett
Regular Contributor
I believe in a previous post it was said that you can't create a custom addin that mimics the behavior of the bookmark sample in the API samples.  The one in the viewer allows you to set the bookmarks in the builder, but the API one allows the user to set and save bookmarks.  It was said that the bookmarks wouldn't persist in the viewer, once the browser was closed. 

Is this still the case? Has anyone had any luck creating an addin (I assume it would use isolated storage?) that would save user created bookmarks, or have any advice on where I might look to start creating one?  Or is it impossible?

Thanks!
0 Kudos
3 Replies
brettangel
Frequent Contributor
I believe in a previous post it was said that you can't create a custom addin that mimics the behavior of the bookmark sample in the API samples.  The one in the viewer allows you to set the bookmarks in the builder, but the API one allows the user to set and save bookmarks.  It was said that the bookmarks wouldn't persist in the viewer, once the browser was closed. 

Is this still the case? Has anyone had any luck creating an addin (I assume it would use isolated storage?) that would save user created bookmarks, or have any advice on where I might look to start creating one?  Or is it impossible?

Thanks!


I used the example from https://developers.arcgis.com/silverlight/api-reference/#ESRI.ArcGIS.Client.Toolkit~ESRI.ArcGIS.Clie... to create a custom bookmark with a "Copy Bookmark Link" button.  I believe isolated storage is defaulted to always on with bookmarks.  The xaml was pretty easy to modify.  Hope this helps.
0 Kudos
KarenEllett
Regular Contributor
I agree, the xaml should be pretty easy, it's the code behind that's got me stumped.  Is there any way you could upload your code so I could take a look at it, as an example?  Thanks!
0 Kudos
brettangel
Frequent Contributor
No problem.  I utilize one bookmark for users and one for predefined areas.

[ATTACH=CONFIG]31150[/ATTACH]

    public partial class CustomBookmark : UserControl, ICommand
    {

        public int _SelectedIndex = -1;
        public string _SelectedName = null;
        private CustomBookmark mywindow = null; 

        private CustomBookmark myBookmark = null;
        Bookmark.MapBookmark bookmark = new Bookmark.MapBookmark();

        public CustomBookmark()
        {
            InitializeComponent();

            MapApplication.Current.Initialized += Layers_LayersInitialized;
        }

        void Layers_LayersInitialized(object sender, EventArgs args)
        {
            myBookmark = myBookmark ?? new CustomBookmark();

            Envelope myExtent = new Envelope(-13544922.588580575, 6058265.2502668081, -13621063.588580577, 6025801.7182497317);
            TestBookmarks.AddBookmark("District All", myExtent);   
        }

        private void BookmarkList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            // This function obtains the index location of the last cell a user clicked in the DataGrid
            // sub-component in the Bookmark Control. It stores that value in the Global variable _SelectedIndex.

            // Note: There is a bug in the Bookmark.SelectionChanged Event where it always fires twice when a
            // DataGrid cell is selected. The first time the Bookmark.SelectionChanged Event fires the correct
            // DataGrid.SelectedIndex value is obtained but then the Event fires a second time and always
            // set the DataGrid.SelectedIndex value to -1. So this code is a workaround.

            // Get the DataGrid sub-component of the Bookmark Control.
            DataGrid myDataGrid = (DataGrid)sender;

            // Get the ObservableCollection<MapBookmark> objects (aka. the named Map.Extent values).
            System.Collections.ObjectModel.ObservableCollection<ESRI.ArcGIS.Client.Toolkit.Bookmark.MapBookmark> theBookmarks = null;
            theBookmarks = MyBookmarks.Bookmarks;
            //theBookmarks = myDataGrid.ItemsSource; // This would work too!

            // Only perform this operation if there at least one named Map.Extent (aka. a bookmark) value.
            if (theBookmarks.Count > 0)
            {
                // Screen out the second firing of this Event.
                if (myDataGrid.SelectedIndex != -1)
                {
                    // Set the Member variable to the currently selected cell in the DataGrid sub-component of the BookMark 
                    // Control. NOTE: the DataGrid.SelectionMode was set to 'Single' in XAML so that only one bookmark could 
                    // be selected at a time for the ClearSelectedBookmark Button to work appropriately.
                    _SelectedIndex = myDataGrid.SelectedIndex;
                    _SelectedName = myDataGrid.SelectedItem.ToString();
               }
            }
        }

        private void ClearSelectedBookmark_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // This function deletes a named Map.Extent (aka. a bookmark) from the DataGrid portion of the 
            // Bookmark Control via its index location. The _SelectedIndex object is a global (aka. Member) 
            // variable and hold the current value of the DataGrid.SelectedItem.

            // Only process _SelectedIndex values not equal to -1.
            if (_SelectedIndex != -1)
            {
                // Delete the specific named Map.Extent (aka. a bookmark) at the specified _SelectedIndex value.
                MyBookmarks.DeleteBookmarkAt(_SelectedIndex);
               
            }
        }

        private void AddBookmarkNEW_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // This function obtains the sub-component TextBox that of the Bookmark Control where the user 
            // enters a name for the Map.Extent from the XAML hierarchy defined in the ControlTemplate. In
            // the case of the XAML code you need to go up two levels (i.e. Parent) in order to use the
            // .FindName() function to obtain the desired TextBox by its name. Then add a named Map.Extent
            // (aka. a bookmark) to the DataGrid sub-components of the Bookmark Control. Finally, clear out 
            // the text of the TextBox after the newly added Bookmark was added.

            // Traverse through the ControlTemplate hierarchy of controls to find the desired TextBox.
            Button myButton = (Button)sender;
            Grid myGrid1 = (Grid)myButton.Parent;
            Grid mygrid2 = (Grid)myGrid1.Parent;
            TextBox myTextBox = (TextBox)mygrid2.FindName("AddBookmarkName");

            // Add a bookmark for the current Map.Extent using the name in the TextBox provided by the user.
            MyBookmarks.AddBookmark(myTextBox.Text, MapApplication.Current.Map.Extent);

            // Clear out the text in the TextBox for the next round.
            myTextBox.Text = "";
        }

        private void CopyBookmarkLink_Click(object sender, RoutedEventArgs e)
        {
            // Get the ObservableCollection<MapBookmark> objects (aka. the named Map.Extent values).
            System.Collections.ObjectModel.ObservableCollection<ESRI.ArcGIS.Client.Toolkit.Bookmark.MapBookmark> theBookmarks = null;
            theBookmarks = MyBookmarks.Bookmarks;

            // Only perform this operation if there at least one named Map.Extent (aka. a bookmark) value.
            if (theBookmarks.Count > 0)
            {
                foreach (ESRI.ArcGIS.Client.Toolkit.Bookmark.MapBookmark bookmark in theBookmarks)
                {
                    if (bookmark.Name == _SelectedName)
                    {
                        string link = HtmlPage.Document.DocumentUri.ToString() + "/index.htm?extent=" + bookmark.Extent.XMin + "," + bookmark.Extent.YMin + "," + bookmark.Extent.XMax + "," + bookmark.Extent.YMax;
                        MessageBox.Show(bookmark.Name + " link copied to clipboard.", bookmark.Name + " Copied", MessageBoxButton.OK);
                        Clipboard.SetText(HtmlPage.Document.DocumentUri.ToString() + "/index.htm?extent=" + MapApplication.Current.Map.Extent.XMin + "," + MapApplication.Current.Map.Extent.YMin + "," +       MapApplication.Current.Map.Extent.XMax + "," + MapApplication.Current.Map.Extent.YMax);
        
                    }
                }
            }
        }

        public void Execute(object parameter)
        {
            mywindow = mywindow ?? new CustomBookmark();
            MapApplication.Current.ShowWindow("", mywindow);
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;

    }
}
0 Kudos