Monday, 3 August 2009

Bing Maps and Windows Mobile Part 3

Using the method GetMapUri from here I was able to retrieve a URL, however I have been unable to see a map yet, just a nice grey square with part of the words "Staging" on it. This was using both my home address and 1 Microsoft Way, as suggested in the article.

One point to note is that when running the GetMapUri method, the line
MapUriResponse mapUriResponse = imageryService.GetMapUri(mapUriRequest); complained with an soap exception regarding the width of the mapUriOptions.ImageSize object not be set (The argument value must be between 80 and 900.Parameter name: Options.ImageSize.WidthActual value was 0.). I found that the following two statements also need to be added to prevent this exception :-
mapUriOptions.ImageSize.HeightSpecified = true;
mapUriOptions.ImageSize.WidthSpecified = true;

With these properties set, the code executes successfully, however I am presented with the grey square as previously mentioned.

Reusing the Token has had little effect.

I will post the code that I have, however I'm unsure why I am not getting a map :-

private string GetMapUri(double latitude, double longitude, int zoom, string mapStyle, int width, int height)
Pushpin[] pins = new Pushpin[1];
Pushpin pushpin = new Pushpin();
pushpin.Location = new;
pushpin.Location.Latitude = latitude;
pushpin.Location.Longitude = longitude;
pushpin.IconStyle = "2";
pins[0] = pushpin;
MapUriRequest mapUriRequest = new MapUriRequest();
// Set credentials using a valid Bing Maps Token
mapUriRequest.Credentials = new;
mapUriRequest.Credentials.Token = GetClientToken();
// Set the location of the requested image
mapUriRequest.Pushpins = pins;
// Set the map style and zoom level
MapUriOptions mapUriOptions = new MapUriOptions();
switch (mapStyle.ToUpper())
case "HYBRID":
mapUriOptions.Style = MapStyle.AerialWithLabels;
case "ROAD":
mapUriOptions.Style = MapStyle.Road;
case "AERIAL":
mapUriOptions.Style = MapStyle.Aerial;
mapUriOptions.Style = MapStyle.Road;
mapUriOptions.ZoomLevel = zoom;
// Set the size of the requested image to match the size of the image control
mapUriOptions.ImageSize = new;
mapUriOptions.ImageSize.Height = height;
mapUriOptions.ImageSize.Width = width;
mapUriOptions.ImageSize.HeightSpecified = true;
mapUriOptions.ImageSize.WidthSpecified = true;
mapUriRequest.Options = mapUriOptions;
int width1 = mapUriRequest.Options.ImageSize.Width;
ImageryService imageryService = new ImageryService();
MapUriResponse mapUriResponse = imageryService.GetMapUri(mapUriRequest);
return mapUriResponse.Uri;

My usage :- currentLocation = GeocodeAddress("1 Microsoft Way, Redmond, WA");
this.webBrowser1.Url = new Uri(GetMapUri(currentLocation.Latitude, currentLocation.Longitude, 17, "HYBRID", 220, 220));


  1. you need to tell the web service that all settings are specified. So you have to add:

    pushpin.Location.LatitudeSpecified = true;
    pushpin.Location.LongitudeSpecified = true;

    mapUriOptions.StyleSpecified = true;

    mapUriOptions.ZoomLevelSpecified = true;

    Once done it works fine.

    The idea came from your post because I had the imagesize issue as well :)

    Thank you mate.


  2. Dude thanks a lot, i was suffering with the same problem, now it works perfect =D

    Thanks guys.

  3. Excellent post and wonderful blog, I really like this type of interesting articles keep it u.
    Toshiba laptops