UWP map with autosuggestion with session key for non-billable transaction

SmilingMoon 981 Reputation points
2021-05-28T22:38:44.92+00:00

Hello,
I try to build simple Map user control in UWP app to allow user to select a coordination exactly like the Windows Map app.
I thought it's simple. It's really hard. Documents are so confusing.

100618-image.png

I'm not sure UWP supports Bing Map features fully.
To get autosuggestion box, I try to use BingMapsRESTToolkit,
but it throws parsing error related ":LocalBusiness" like the one reported on May 15, 2020.
https://github.com/microsoft/BingMapsRESTToolkit/issues/46
It's been been an year and it's NOT fixed.
=> IF Anyone know how to avoid this, it's the best solution for me.

So, I gave up using the toolkit and try to use REST API HTTP Request following https://learn.microsoft.com/en-us/bingmaps/rest-services/autosuggest

I can do this, but I cannot figure out how to follow the best practice using SessionKey
from the doc: https://learn.microsoft.com/en-us/bingmaps/rest-services/using-the-rest-services-with-net

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    
});    

"Map." is WFP map control and UWP map control does NOT have "CredentialProvider.GetCredentials()"

So, I got stuck with UWP map control because BingMapToolkit that implements the best practice is broken and UWP Map control does not support "Map.CredentialProvider.GetCredentials()"

Anyone knows how to fix this? What am I missing here?

Universal Windows Platform (UWP)
Windows Maps
Windows Maps
A Microsoft app that provides voice navigation and turn-by-turn driving, transit, and walking directions.
257 questions
{count} votes

1 answer

Sort by: Most helpful
  1. SmilingMoon 981 Reputation points
    2021-06-01T18:04:48.1+00:00

    101491-2021-06-01-10-55-36-skyonthewayphotomaster-debuggi.png

    Here is the steps

    async private void btFileGridTest_Click(object sender, RoutedEventArgs e)  
    {  
                if (ovm.Current_FileInfo == null) return;  
                BasicGeoposition userPos = ovm.Current_FileInfo.GPSLocation;  
                await MapHelper.AutoSuggestWithToolkit("Big 5", userPos, 10);  
    }  
      
    static async public Task AutoSuggestWithToolkit(string searchQuery, BasicGeoposition locationToRef, int radius)  
            {  
      
                try // => The following BingMapToolkit throws exception: https://github.com/microsoft/BingMapsRESTToolkit/issues/46  
                {  
                    Coordinate cord = ToCoordinate(locationToRef);  
                    Console.WriteLine("Running Geocode Test");  
                    var request = new AutosuggestRequest()  
                    {  
                        BingMapsKey = _ApiKey,  
                        //UserLocation = cord,  
                        Query = searchQuery, // "Seattle"  
                        MaxResults = 7,  
                        //AutoLocation = AutosuggestLocationType.  
                    };  
                    request.UserLoc = new CoordWithRadius() { Latitude = locationToRef.Latitude, Longitude = locationToRef.Longitude, Radius = radius };  
                    //if (cord != null)  
                    //{  
                    //    request.UserLocation = cord;  
                    //}  
      
                    BingMapsRESTToolkit.Resource[] resources = await GetResourcesFromRequest(request);  
      
                    foreach (BingMapsRESTToolkit.Resource resource in resources)  
                    {  
                        //App.l($"type={resource.Type}, ", 3);  
      
                        Console.WriteLine((resource as Location).Name);  
                    }  
                }  
                catch (Exception ex)  
                {  
                    App.l($"Exception={ex.Message}, detail={ex.StackTrace} ", 3);  
                }  
            }  
      
      
    async static private Task<BingMapsRESTToolkit.Resource[]> GetResourcesFromRequest(BingMapsRESTToolkit.BaseRestRequest rest_request)  
            {  
                if(IsAPIKeyNotValid)  
                    throw new Exception("API Key is Denied"); //when key is not valid, prevent further calling api.  
      
                var r = await ServiceManager.GetResponseAsync(rest_request); //.GetAwaiter().GetResult();  
                if(r.AuthenticationResultCode == "DeniedCredentials")  
                {  
                    //key exceeded limit of usage. and key is denied to use.  
                    IsAPIKeyNotValid = true;  
                    throw new Exception("API Key is Denied");  
                }  
      
                if (!(r != null && r.ResourceSets != null &&  
                    r.ResourceSets.Length > 0 &&  
                    r.ResourceSets[0].Resources != null &&  
                    r.ResourceSets[0].Resources.Length > 0))  
                {  
                    return null;  
                }  
                    //throw new Exception("No results found.");  
      
                return r.ResourceSets[0].Resources;  
            }  
      
      
    Exception occurred with query "Big 5"  
    Element ':item' contains data of the ':LocalBusiness' data contract. The deserializer has no knowledge of any type that maps to this contract. Add the type corresponding to 'LocalBusiness' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.  
      
    Exception occurred with query "Hawaii"  
    Element ':item' contains data of the ':Place' data contract. The deserializer has no knowledge of any type that maps to this contract. Add the type corresponding to 'Place' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.  
      
    

    101380-2021-06-01-11-02-30-skyonthewayphotomaster-debuggi.png

    The methods are from samples from the Bigmap Toolkit and slightly modified to accept the parameters I want.

    The url used for the request is
    Domain "https://dev.virtualearth.net/REST/v1/"


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.