Tuesday, 4 August 2009

Bing Maps and Windows Mobile Part 4

I knew of the following Device How Do I video (Display Virtual Earth Maps inside a PictureBox Control? - http://msdn.microsoft.com/en-gb/netframework/dd920289.aspx) and after looking at the code, it provided the missing piece in the jig saw from the original article I was working from (http://msdn.microsoft.com/en-us/library/dd483215.aspx).

The missing pieces were a few calls to a number of the specified properties as below :-

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

I now get a nice map as expected. It is slightly unusual that you set the property's value and then have to state that the value was specified, you would think that by just setting the value in the first place then you would have specified the value. It is also strange that these specified properties were not listed in the code from the MSDN article.

So the code as posted previously should now look like the following :-

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.LatitudeSpecified = true;
pushpin.Location.Longitude = longitude;
pushpin.Location.LongitudeSpecified = true;
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;
case "ROAD":
mapUriOptions.Style = MapStyle.Road;
case "AERIAL":
mapUriOptions.Style = MapStyle.Aerial;
mapUriOptions.Style = MapStyle.Road;
mapUriOptions.ZoomLevel = zoom;
mapUriOptions.ZoomLevelSpecified = true;
// 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;

1 comment:

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