none
How to acquire Bing Maps SessionId for non-billable Bing Rest service using the WinRT API RRS feed

  • Question

  • 
    I understand that using SessionId in Bing Maps Rest service calls are not billable after the first query per client, which is good. Regrettably it is not completly clear how to acquire this SessinID in the WinRT/Store App environment. The example on the MSDN site presents the following code in C# to acquire this ID:
    
    Map.CredentialsProvider.GetCredentials((c) =>
        {
            string sessionKey = c.ApplicationId;
    
            //Generate a request URL for the Bing Maps REST services.
            //Use the session key in the request as the Bing Maps key
        });
    

    The only issue I currently have is that the Map class nor its instance does not have this CredentialsProvider featuremethod. All I can get out from the Map instance is the Bing Map keys using its Credentials property, which returns the string that is added in the XAML declaration of the Map instance.
    
    If someone could shed some light on how to acquire the relevant SessionID using Bing Maps Windows Store API that would be great! I'm not even sure if I need to generate this in my code or it is generated by the platform itself...Thank you!
    

    Sunday, May 22, 2016 8:25 AM

Answers

  • Hmmmm...I don't think I have the getCredentials method available for the map instance at all. Might be in the standard WPF or Silverlight API but I cannot seem to find it in the WinRT one.

    In the meantime I did find the solution - thanks for the hint anyway.The solution is as follows:I initialize the Map instance on a Page. Calling the XAML-declared name of the Map instance will give me a method GetSessionIdAsync, which is the one I was looking for. I am not sure what is the access declaration of this method, but if I try to navigate to the same map instance from an another UI element via the UI object model this method is not visible (could be declared private??).

    If you try to call this method from the constructor of the Page then it might not going to have the session ID initialized then you will get an empty string after calling the GetResult method on the returning Task object of the GetSessionIdAsync method. However, if you call this after the whole Page load then you should get the SessionID string.

    So the following code:

     System.Diagnostics.Debug.WriteLine(mainMap.GetSessionIdAsync().GetResults().ToString());

    Did print out the following:

    Av6HNdHGvjM4jSJTKVCI6tqOCeR8N_Kr_8gilOTgX5po4V0_OPGze-_dP_n5pZmL

    This looks like a session ID to me - will try out with the services tomorrow.

    • Proposed as answer by Ricky_Brundritt Monday, May 23, 2016 1:42 AM
    • Marked as answer by tamas_ Monday, May 23, 2016 6:05 AM
    Sunday, May 22, 2016 3:30 PM

All replies

  • After the map loads you can do the following to get a session key.

    map.getCredentials(function(c){

    sessionKey = c;

    //use session key with rest services or save for later in the app.

    }


    [Blog] [twitter] [LinkedIn]

    Sunday, May 22, 2016 2:54 PM
  • Hmmmm...I don't think I have the getCredentials method available for the map instance at all. Might be in the standard WPF or Silverlight API but I cannot seem to find it in the WinRT one.

    In the meantime I did find the solution - thanks for the hint anyway.The solution is as follows:I initialize the Map instance on a Page. Calling the XAML-declared name of the Map instance will give me a method GetSessionIdAsync, which is the one I was looking for. I am not sure what is the access declaration of this method, but if I try to navigate to the same map instance from an another UI element via the UI object model this method is not visible (could be declared private??).

    If you try to call this method from the constructor of the Page then it might not going to have the session ID initialized then you will get an empty string after calling the GetResult method on the returning Task object of the GetSessionIdAsync method. However, if you call this after the whole Page load then you should get the SessionID string.

    So the following code:

     System.Diagnostics.Debug.WriteLine(mainMap.GetSessionIdAsync().GetResults().ToString());

    Did print out the following:

    Av6HNdHGvjM4jSJTKVCI6tqOCeR8N_Kr_8gilOTgX5po4V0_OPGze-_dP_n5pZmL

    This looks like a session ID to me - will try out with the services tomorrow.

    • Proposed as answer by Ricky_Brundritt Monday, May 23, 2016 1:42 AM
    • Marked as answer by tamas_ Monday, May 23, 2016 6:05 AM
    Sunday, May 22, 2016 3:30 PM
  • Ah, yes, when you said WinRT I thought you were using the JavaScript map control. The method you mentioned is the correct one, and that string you posted is a session key.

    That said, I would recommend targeting Windows 8 unless you absolutely have too. Windows 10 has a higher market share than windows 8. There is also a new universal windows map control in windows 10 that is built right into the windows SDK.


    [Blog] [twitter] [LinkedIn]

    Monday, May 23, 2016 1:41 AM
  • Yes, the Windows 10 UWP system and the Win10 related Bing Maps API seems pretty good, this application is dedicated for users using Win 8.1 and above only for Desktop and Surface/Tablet devices, so in our case the UWP is not that important. I did see though that the StreetSide is also integrated into the Win10 Bing Maps environment, which is a very lucrative way to attract us to migrate to Win10 API in the future - especially when the coverage will go truly global.

    Thanks for the help Ricky

    Monday, May 23, 2016 6:05 AM