question

SmilingMoon avatar image
0 Votes"
SmilingMoon asked AryaDing-MSFT commented

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

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://docs.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://docs.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?





windows-uwpwindows-maps
image.png (496.2 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@EywaOnTheWay What steps you take before you met LocalBusiness parsing error? What is the error code?

0 Votes 0 ·

Please see the post below. I supposed to reply, but I posted it as answer due to the character limit here.

0 Votes 0 ·

1 Answer

SmilingMoon avatar image
0 Votes"
SmilingMoon answered AryaDing-MSFT commented

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/"


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@EywaOnTheWay It is recommended to use the uwp Mapcontrol and customized Autosuggestbox to do this, which is easier to resolve this issue. Then you could use MapLocationFinder Class to search the location according to the input.

0 Votes 0 ·