Next I needed to actually get the Image from Bing. Now let’s do that using the following function:
private async Task<string> GetImagery(string locationString)
{ //http://msdn.microsoft.com/en-us/library/dd221354.aspx
string key = "[[insert your Bing Maps Key here]]";
MapUriRequest mapUriRequest = new MapUriRequest();
// Set credentials using a valid Bing Maps key
mapUriRequest.Credentials =
new HouseofRepresentatives.ImageryService.Credentials();
mapUriRequest.Credentials.ApplicationId = key;
// Set the location of the requested image
mapUriRequest.Center = new HouseofRepresentatives.ImageryService.Location();
string[] digits = locationString.Split(',');
mapUriRequest.Center.Latitude = double.Parse(digits[0].Trim());
mapUriRequest.Center.Longitude = double.Parse(digits[1].Trim());
// Set the map style and zoom level
MapUriOptions mapUriOptions = new MapUriOptions();
mapUriOptions.Style = MapStyle.AerialWithLabels;
mapUriOptions.ZoomLevel = 17;
// Set the size of the requested image in pixels
mapUriOptions.ImageSize = new HouseofRepresentatives.ImageryService.SizeOfint();
mapUriOptions.ImageSize.Height = 240;
mapUriOptions.ImageSize.Width = 480;
mapUriRequest.Options = mapUriOptions;
//Make the request and return the URI
ImageryServiceClient imageryService =
new ImageryServiceClient(ImageryServiceClient.EndpointConfiguration.BasicHttpBinding_IImageryService);
try
{
MapUriResponse mapUriResponse = await imageryService.GetMapUriAsync(mapUriRequest);
if (mapUriResponse.Uri != null)
{
return mapUriResponse.Uri;
}
else { return ""; }
}
catch (Exception ex)
{
return "";
}
}
Ok… now that I have my image Uri, it’s time to use it to load a picture:
public async Task<bool> UpdateMap(string address = "")
{
if (address != "")
{
//then user is specifying an address instead of a specific long/lat.
//We need to get that info in order to proceed...
string point = await getLocationPoint(address);
if (point != "")
{
string[] p = point.Split(',');
Latitude = p[0];
Longitude = p[1];
}
}
if (Longitude != "" && Latitude != "")
{
string uripath = await GetImagery(Longitude + "," + Latitude);
if (uripath != "")
{
this.Map = new BitmapImage(new Uri(uripath));
return true;
}
else
{
this.Map = new BitmapImage(new Uri(imageBaseUri, "Data/blank.png"));
return false;
}
}
else
{
this.Map = new BitmapImage(new Uri(imageBaseUri, "Data/blank.png"));
return false;
}
}
If you’ve stuck with me this far… you are a programmer… LOL.
In each of my functions I utilized the new async/await features. This means when I call either of the two functions as below, it will load the image once the OS has downloaded and cached it. The image will automatically appear when ready…
//calling the get-map function with an address
UpdateMap("Arkansas");
//or
UpdateMap("1 Microsoft Way, Redmond, Washington");
//or i can configure my lat/lon then update
Latitude = "39.450000762939453";
Longitude = "-98.907997131347656";
UpdateMap();
If you guys have any questions or comments, be sure to post them below!