Showing posts with label Bing Maps. Show all posts
Showing posts with label Bing Maps. Show all posts

Thursday, 12 November 2009

Bing Maps and Windows Mobile Part 5

A question was recently asked on the Smart Device MSDN forums here. This regarded getting an address from latitude and longitude co-ordinates.

In an attempt to answer the question I had a quick look at the Bing Maps Web Service and found that ReverseGeocode method was available and posted a link to the relevant MSDN documentation here.

As I had already blogged about Bing Maps and Windows Mobile, I thought I would check what the method returned.

I used the Geocode method to return the Latitude and Longitude from my home address and then feed these two values back into the ReverseGeocode method and was returned an address string, although the house number was 15 numbers out.

The method I used is below, please note that the LatitudeSpecified and LongitudeSpecified had to set to ensure that a valid response was receieved.

private string ReverseGeocodeCoordinates(double latitude, double longitude)
{
ReverseGeocodeRequest reverseGeocodeRequest = new ReverseGeocodeRequest();
reverseGeocodeRequest.Credentials = new WindowsMobileBingMapsTester.net.virtualearth.dev.staging.Credentials();
reverseGeocodeRequest.Credentials.Token = GetClientToken();

reverseGeocodeRequest.Location = new WindowsMobileBingMapsTester.net.virtualearth.dev.staging.Location();
reverseGeocodeRequest.Location.Latitude = latitude;
reverseGeocodeRequest.Location.LatitudeSpecified = true;
reverseGeocodeRequest.Location.Longitude = longitude;
reverseGeocodeRequest.Location.LongitudeSpecified = true;

// Make the geocode request
GeocodeService geocodeService = new GeocodeService();
GeocodeResponse geocodeResponse = geocodeService.ReverseGeocode(reverseGeocodeRequest);

if (geocodeResponse.Results.Length > 0)
if (geocodeResponse.Results[0].Locations.Length > 0)
return geocodeResponse.Results[0].DisplayName;

return null;
}

Usage :-

string currentAddress = ReverseGeocodeCoordinates(47.608, -122.337);

Hopefully this will help those looking to determine an address from a set of co-ordinates.

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;
break;
case "ROAD":
mapUriOptions.Style = MapStyle.Road;
break;
case "AERIAL":
mapUriOptions.Style = MapStyle.Aerial;
break;
default:
mapUriOptions.Style = MapStyle.Road;
break;
}
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;
}

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));

Bing Maps and Windows Mobile Part 2

After waiting about an hour and a half I tried to add the Common web service again and after entering the correct credentials (Account Id rather than Account Name) I was able to add the reference.

I build the project and found a number of errors, namely due to the copy and paste from the original article, however after these were resolve the project built and deployed to the Windows Mobile 5 emulator.

I kept things simple by entering my home address and wanting to get out the Latitude and Longitude. Having entered only my house number and street, the code ran through however no locations were found. I then entered my complete address and I was able to get out a
GeocodeLocation object which allowed me to display a label with the two properties on it.

The following is the code I have currently, I will then look at downloading a map, bear in mind that you have to sign up for a developer account and the credentials you have to enter in both the dialog and the code are unique to yourself and have been removed in the posted code :-


private string GetClientToken()
{
CommonService commonService = new CommonService();
commonService.Url = "https://staging.common.virtualearth.net/find-30/common.asmx";
commonService.Credentials =
new System.Net.NetworkCredential("[ACCOUNTID]", "[ACCOUNTPASSWORD]");
// Create the TokenSpecification object to pass to GetClientToken.
TokenSpecification tokenSpec = new TokenSpecification();
//Use the currently exposed public IP for the page.
tokenSpec.ClientIPAddress = "[IPADDRESS]";
// The maximum allowable token duration is 480 minutes (8 hours).
// The minimum allowable duration is 15 minutes.
tokenSpec.TokenValidityDurationMinutes = 480;
// Now get a token from the Bing Maps Token Service.
return commonService.GetClientToken(tokenSpec);
}


private GeocodeLocation GeocodeAddress(string address)
{
GeocodeRequest geocodeRequest = new GeocodeRequest();
// Set the credentials using a valid Bing Maps token
geocodeRequest.Credentials = new Credentials();
geocodeRequest.Credentials.Token = GetClientToken();
// Set the full address query
geocodeRequest.Query = address;
// Set the options to only return high confidence results
ConfidenceFilter[] filters = new ConfidenceFilter[1];
filters[0] = new ConfidenceFilter();
filters[0].MinimumConfidence = Confidence.High;
GeocodeOptions geocodeOptions = new GeocodeOptions();
geocodeOptions.Filters = filters;
geocodeRequest.Options = geocodeOptions;
// Make the geocode request
GeocodeService geocodeService = new GeocodeService();
GeocodeResponse geocodeResponse = geocodeService.Geocode(geocodeRequest);
if (geocodeResponse.Results.Length > 0)
if (geocodeResponse.Results[0].Locations.Length > 0)
return geocodeResponse.Results[0].Locations[0];
return null;
}

And calling code :-

GeocodeLocation currentLocation = GeocodeAddress("[MY ADDRESS HERE]");
if (currentLocation != null)
{
this.label1.Text = "Lat: " + currentLocation.Latitude.ToString();
this.label1.Text += " Long : " + currentLocation.Longitude.ToString();
}

More to follow...

Bing Maps and Windows Mobile Part 1

Eric Lam had blogged about the Bing Maps Web Services SDK 1.0 (http://ericlam.spaces.live.com/blog/cns!5D73BE0B4076E647!4408.entry), so I decided to sign up for a developer account to have a play with the SDK.

After receiving the email I was instructed to sign in to Bing Maps CSS to create a password for my account. Having done this I then went about trying to get something up and running.

After browsing through the documentation, there was a link to the Developing a Mobile Application Using Bing Maps Web Services article (http://msdn.microsoft.com/en-us/library/dd483215.aspx), I thought this is exactly what I was looking for (I wanted to Geocode an address and then display a map inside a Smart Device project).

I started reading this article and it soon turned out that it was very much focused on developing a ASP.NET page that a Windows Mobile device could access, this wasn't exactly what I was looking for, but would be a useful resource in achieving my goal.

So I set about adding the first web reference (ASMX) for being able to setup a Client Token. I was then prompted for my Username and Password however it was going to take a couple of hours for my account to be fully activated so I went on to the next section, the Bing Maps Web Services.

The service URLs had an extension of SVC (WCF), I had never used WCF before so this was going to be a new experience. As the article was aimed at creating a ASP.NET page then Adding A Service Reference wouldn't be a problem, however you do not get this option for a Smart Device project.

After a little more searching I realised I had to generate the Proxy classes. I thought I needed to use the svcutil.exe tool, I went ahead and generated the proxy classes, added them to my Smart Device project and then built, I was then faced with a number of build errors. I then remembered that there was a NETCF version of this tool (netcfsvcutil.exe), so I then attempted to generate the proxy classes using this tool. The tool ran through to completion however did not produce any proxy classes.

I then did a bit more searching and found the following piece of documentation Bing Maps Web Services Metadata (http://msdn.microsoft.com/en-us/library/cc966738.aspx). This provided the metadata URLs which I could then add as references to my Smart Device project.

I now have all four Bing Map Web Services added to my project, I am now just waiting for my account to be activated to be able to add the Common web service and then generate a Client Token.

More details to follow...