Monday 3 August 2009

Bing Maps and Windows Mobile Part 3

Using the method GetMapUri from here http://msdn.microsoft.com/en-us/library/dd483215.aspx 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 WindowsMobileBingMapsTester.net.virtualearth.dev.staging1.Location();
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 WindowsMobileBingMapsTester.net.virtualearth.dev.staging1.Credentials();
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;
break;
case "ROAD":
mapUriOptions.Style = MapStyle.Road;
break;
case "AERIAL":
mapUriOptions.Style = MapStyle.Aerial;
break;
default:
mapUriOptions.Style = MapStyle.Road;
break;
}
mapUriOptions.ZoomLevel = zoom;
// Set the size of the requested image to match the size of the image control
mapUriOptions.ImageSize = new WindowsMobileBingMapsTester.net.virtualearth.dev.staging1.SizeOfint();
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 :-


WindowsMobileBingMapsTester.net.virtualearth.dev.staging.GeocodeLocation currentLocation = GeocodeAddress("1 Microsoft Way, Redmond, WA");
this.webBrowser1.Url = new Uri(GetMapUri(currentLocation.Latitude, currentLocation.Longitude, 17, "HYBRID", 220, 220));

2 comments:

  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.

    Simone

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

    Thanks guys.

    ReplyDelete