WP7Contrib – Bing Service Wrapper Part IV Location Simplified

Based on feedback around my first post in this series of talking about how to use Location I thought that I would revisit how you can use this really cool bit of stuff in your app. When I first wrote the original post it was much more bare bones however, there were lots of unanswered questions when I did and could be taken out of context. Therefore I wanted to reiterate how easy it is for you app to incorporate Geo Location features and enhance the User Experience.

So, lets start off with some new code and how you can pretty much copy and paste this boiler plate code into your app and start to take advantage of Geo Location. Ollie has done some refactoring of the defined constructors making the implementation simpler. Which means; if you are not using Dependency Injection; or MVVM; or just want something very specific from the service, these new constructors will give you the ability to do just that.

The code below can be found in the BingMapsLocationDemo folder under spikes of the WP7C along with lots of other samples including the original code that exercises the Bing Maps Wrapper.

First, we need an instance of the BingMapsService.

this.bingMapsService = 
    new BingMapsService("Your credentials id", "Your app id");

Next, we need to use our good friend the CriterionFactory that will do all the work required to generate the request that will be sent to the endpoint. There are a couple of flavours for Location, depending on what you want to do; for finding out the address of a GeoCoordinate you can use CreateLocationSearchForPoint; or for finding out the full address from a partial one you can use CreateLocationSearchForAddress.

var criterion =
    CriterionFactory.CreateLocationSearchForAddress(this.postCode.Text);

Finally, we need use Rx to perform the async call. Using the instance of the BingMapsService we call the method which in our case is SearchForLocationUsingAddress that takes the criterion created by the factory as a parameter.

this.bingMapsService.SearchForLocationUsingAddress(criterion)
        .ObserveOnDispatcher()
        .Subscribe(result =>
        {
         this.address.Text = result.Locations[0].Address.Locality;
         this.address.Text += Environment.NewLine;
         this.address.Text += result.Locations[0].Address.PostalCode;
         this.address.Text += Environment.NewLine;
         this.address.Text += result.Locations[0].Address.AdminDistrict;
         this.address.Text += Environment.NewLine;
         this.address.Text += result.Locations[0].Address.CountryRegion;
        });

Boom! Your done!

Hopefully, second time round this post is clearer and a far more succinct illustration of how to use the Location in your app. We will also continue to add some new projects into the Spikes folder to illustrate the other condensed constructors for Search, Imagery and Routes.

Be Sociable, Share!

Tags: , , ,

One Response to “WP7Contrib – Bing Service Wrapper Part IV Location Simplified”

  1. Wow great post! Microsoft rules!

    Joe S

Leave a Reply