none
Bing Maps wpf Control, C#, Click Map Objects and Getting Session Key from Bing Maps Key RRS feed

  • Question

  • Regarding Clicking Map Objects... Bare with me, I'm coming from MapPoint. On mouse click event I want to determine the type of object clicked, cast it, and extract info from that object. In MapPoint is was a simple as determining the event sender typeof (i.e., pushpin, territory...). Can someone give me the specific code needed to accomplish this from the wpf control in C#?

    Regarding Obtaining a Session Key (to minimize Bing Maps transaction usage): Yes I've read the Bing Map Best Practices "Implementing Session Keys" but for the life of me I can't get the syntax right. Again if there's sample code specific to the wpf control using C#.

    Thanks... Matt 

    Thursday, April 7, 2016 4:35 PM

Answers

  • For clicking you can add events to each individual item. To check what type the object is you can typeof approach, or the faster is/as approach:

    https://www.microsoft.com/maps/product/print-rights.html

    https://msdn.microsoft.com/en-us/library/cscsdfbt.aspx

    Here is a quick code sample:

    private void mapItemClicked(object sender, RoutedEventArgs e)
    {
        if(sender is Pushpin)
        {
            //Pushpin was clicked.
        }
        else if(sender is MapPolyline)
        {
            //Polyline was clicked
        }
        else if (sender is MapPolygon)
        {
            //Polygon was clicked
        }
        else if (sender is Map)
        {
            //Map was clicked
        }
    }

    To get the session key, make sure that the map is in the visible display. If the map is hidden in a tab you won't be able to get the session key until the map is displayed. A good tip is to use the Loaded event of the map and then get the session key from there.

    string sessionKey = string.Empty;
    
    MyMap.Loaded += (s, e) =>
    {
        MyMap.CredentialsProvider.GetCredentials((c) =>
        {
            sessionKey = c.ApplicationId;
        });
    };


    http://rbrundritt.wordpress.com

    • Proposed as answer by Ricky_Brundritt Friday, April 8, 2016 12:31 AM
    • Marked as answer by mfritz6809 Saturday, April 9, 2016 10:16 AM
    Friday, April 8, 2016 12:31 AM
  • Make sure you are adding the event to the pushpin and not the map.

    http://rbrundritt.wordpress.com

    • Marked as answer by mfritz6809 Saturday, April 9, 2016 10:16 AM
    Friday, April 8, 2016 7:27 PM
  • Not aware of any books on the Bing Maps WPF control. I wrote a book a few years ago around the Windows 8 map control, however, a new map control was introduced in Windows 10 which derived from the WP8.1 map control which is much different, so the code in the book is outdated. However, there are some good concepts in there. You can download a copy of the book here: https://rbrundritt.wordpress.com/my-book/

    http://rbrundritt.wordpress.com

    Monday, April 11, 2016 4:45 PM

All replies

  • For clicking you can add events to each individual item. To check what type the object is you can typeof approach, or the faster is/as approach:

    https://www.microsoft.com/maps/product/print-rights.html

    https://msdn.microsoft.com/en-us/library/cscsdfbt.aspx

    Here is a quick code sample:

    private void mapItemClicked(object sender, RoutedEventArgs e)
    {
        if(sender is Pushpin)
        {
            //Pushpin was clicked.
        }
        else if(sender is MapPolyline)
        {
            //Polyline was clicked
        }
        else if (sender is MapPolygon)
        {
            //Polygon was clicked
        }
        else if (sender is Map)
        {
            //Map was clicked
        }
    }

    To get the session key, make sure that the map is in the visible display. If the map is hidden in a tab you won't be able to get the session key until the map is displayed. A good tip is to use the Loaded event of the map and then get the session key from there.

    string sessionKey = string.Empty;
    
    MyMap.Loaded += (s, e) =>
    {
        MyMap.CredentialsProvider.GetCredentials((c) =>
        {
            sessionKey = c.ApplicationId;
        });
    };


    http://rbrundritt.wordpress.com

    • Proposed as answer by Ricky_Brundritt Friday, April 8, 2016 12:31 AM
    • Marked as answer by mfritz6809 Saturday, April 9, 2016 10:16 AM
    Friday, April 8, 2016 12:31 AM
  • Thanks, Ricky. The 'obtain sessionKey'  code works fine (implemented inside a map loaded event as you suggested.

    However the code below to detect a mouse double-click on a pushpin always returns 'Map'. Where did I stray?

            private void MapWithPushpins_MouseDoubleClick(object sender, MouseButtonEventArgs e)
            {
                // disables the default mouse double-click action
                e.Handled = true;
               

                // raise messagebox nased on sender type
                if (sender is Pushpin)
                {
                    MessageBox.Show("Puspin");
                }
                else if (sender is Map)
                {
                    MessageBox.Show("Map"); <--- ALWAYS RETURNS 'Map'
                }
            }


    mfritz6809

    Friday, April 8, 2016 4:38 PM
  • Make sure you are adding the event to the pushpin and not the map.

    http://rbrundritt.wordpress.com

    • Marked as answer by mfritz6809 Saturday, April 9, 2016 10:16 AM
    Friday, April 8, 2016 7:27 PM
  • ahhh... got it. Add an event when creating each pushpin. And thanks for the "Pushpin Clustering" code which I'll incorporate as I generally work with many pushpins in a tight geography.

    As mentioned I'm coming from years of desktop MapPoint and I'm also new to wpf. Any books/tutorials you could recommend on wpf in general and Bing Maps in particular (focused on C#)? I'm an 'ol school guy that likes to curl up with a good book and dig in... :) Thanks! 

      

    mfritz6809

    Saturday, April 9, 2016 10:15 AM
  • Not aware of any books on the Bing Maps WPF control. I wrote a book a few years ago around the Windows 8 map control, however, a new map control was introduced in Windows 10 which derived from the WP8.1 map control which is much different, so the code in the book is outdated. However, there are some good concepts in there. You can download a copy of the book here: https://rbrundritt.wordpress.com/my-book/

    http://rbrundritt.wordpress.com

    Monday, April 11, 2016 4:45 PM