Having signed up for Microsoft’s Windows Phone 7 developer program and awaiting for Phone 7 launch this fall, I’m actually pretty much late with the development of 5 apps I need to have ready yesterday. This afternoon was perfect for giving myself a crash course of Microsoft Windows Phone 7 app development. I have to mention that I didn’t have a clue about Phone 7 except that I knew it’s XAML and C# .NET so I wanted to try a simple Hello World application but soon realized it’s way to simple for that so I went just a bit further.
I ended up creating an app which uses Phone 7 geo-location services to obtain GPS position, I used Bing’s geo location services to do reverse geocoding and get the ZIP code of the device’s current location and finally I used a public SOAP weather web service to get current weather conditions and display them. Besides a short guide in this blog I also attached the complete Visual Studio 2010 solution and source code, you can find it at the end of this post.
Installing Windows Phone 7 tools
I already had Visual Studio 2010 along with other tools installed on my box so I just headed to msdn and downloaded the Phone 7 tools. To make sure everything was OK, opened up VS and created a Windows Phone 7 application.
My first Windows Phone 7 application (a.k.a. Hello World)
After creating a new WP7 app project you get something like this on your screen.
So far so good, I see a familiar XAML UI interface designer and XAML code editor. I changed the label to “Weather” and started Windows Phone 7 emulator for the first time. It took a while before it loaded up but eventually I got this:
Getting geo location on Phone 7
Geolocation API (dll) was not referenced in WP7 app project by default so I needed to add the reference manually. The DLL file I was looking for was System.Device.dll.
Now creating location aware app is pretty easy and it’s just instancing GeoCoordinateWatcher and assigning a callback for its event PositionChanged.
GeoCoordinateWatcher watcher = null; watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default); watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged); watcher.Start(); |
Callback is triggered once the application gets location coordinates and its EventArgs hold the result we need. I just need current location and I don’t want to be notified when the position changes so that’s why I’m stopping location acquiring the moment I first get it.
void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
//stop & clean up, we don’t need it anymore
watcher.Stop();
watcher.Dispose();
watcher = null;
location = e.Position.Location;
}
Pretty easy. Now that I have the GPS coordinates, I want to use reverse geocoding to get the information of my whereabouts that actually make sense for humans (not that GPS latitude and longitude don’t).
Reverse geocoding using Bing geo location services
First thing I needed is Bing maps API key in order to be able to query its SOAP web service. You get one (for free) at https://www.bingmapsportal.com/. Now we add a web (service) reference to the Bing geo location WCF service. I called it BingMaps and I only used GeoLocation service.
ReverseGeocodeRequest reverseGeocodeRequest = new ReverseGeocodeRequest();
// Set the credentials using a valid Bing Maps key
reverseGeocodeRequest.Credentials = new BingMaps.Credentials();
reverseGeocodeRequest.Credentials.ApplicationId = "ENTER YOUR BING MAPS KEY HERE";
// Set the point to use to find a matching address
BingMaps.Location point = new BingMaps.Location();
point.Latitude = location.Latitude;
point.Longitude = location.Longitude;
reverseGeocodeRequest.Location = point;
// Make the reverse geocode request
GeocodeServiceClient geocodeService = new GeocodeServiceClient("BasicHttpBinding_IGeocodeService");
geocodeService.ReverseGeocodeCompleted += new EventHandler<ReverseGeocodeCompletedEventArgs>(geocodeService_ReverseGeocodeCompleted);
geocodeService.ReverseGeocodeAsync(reverseGeocodeRequest);
Once Bing finishes with reverse geocoding, geocodeService_ReverseGeocodeCompleted callback is triggered with the results in EventArgs. So now I got my address, town, country, ZIP code etc… I was shooting for ZIP code cause that’s what the weather service wants as an argument.
Getting weather conditions for current location
I used a public free weather SOAP web service found here. It does current weather as well as weather forecast but for the sake of this example I just wanted and used current weather method. After adding web reference to this weather service WSDL, the code was rather simple.
private void GetWeather()
{
WeatherWsc.WeatherSoapClient wsc = new WeatherWsc.WeatherSoapClient();
wsc.GetCityWeatherByZIPCompleted += new EventHandler<WeatherWsc.GetCityWeatherByZIPCompletedEventArgs>(wsc_GetCityWeatherByZIPCompleted);
wsc.GetCityWeatherByZIPAsync(zip);
}
void wsc_GetCityWeatherByZIPCompleted(object sender, WeatherWsc.GetCityWeatherByZIPCompletedEventArgs e)
{
txtStatus.Text = "Finished";
prgProgress.IsEnabled = false;
prgProgress.Visibility = System.Windows.Visibility.Collapsed;
txtLoc.Text = e.Result.City;
txtTemp.Text = e.Result.Temperature + "F";
txtRest.Text = string.Format("Pressure: {0}{1}{2}", e.Result.Pressure, System.Environment.NewLine, e.Result.Description);
}
Putting it all together: an example location aware weather Windows Phone 7 app
Surprisingly enough this was pretty easy task for me even if I didn’t know anything about Phone 7 programming. Yes, I am proficient in C# .NET development but still, I also code Java and Android apps don’t look so simple to me. It took only 30 mins to do this. So kudos to Microsoft from an Apple fanboy!
Pretty nice, huh? Complete Visual Studio 2010 source code is available for Hello Windows Phone 7. You can use this example however you want but please note that I don’t provide any guarantees for this code nor I’m responsible for what you do with it in any way. And you are also encouraged to check it for bugs and add exception handling which has been totally omitted for the sake of demonstration.





