Monday, 3 August 2009

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...

No comments:

Post a Comment