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