A bit a fun for the last article of 2013.
If you are targeting the .Net Framework 4.0 or better in your applications and your apps are running on Windows 7 or better, you have access to the System.Device.Location namespace which can help you find where you are.
The accuracy of your position will greatly vary according to the provider (such as GPS, Wi-Fi triangulation, cell phone tower triangulation) used by your computer to report its position.
Requirements
You will need to use and target .Net Framework 4.0 (or better) and run the application on Windows 7 (or better).
The Demo application
This month demo application is provided in both VB and C#. The solution has been created using Visual Studio 2013.
The user interface
For the purposes of the demo, the UI shows 3 simple controls:
Figure 1: The demo application in action
Accuracy
I wouldn’t call the cops based on position reported by the classes of this namespace. Especially not if your position is reported through your wired network connection. While doing my tests, I found that when I was using my Wi-Fi home network and when I was tethering my cell phone band width, the accuracy was about 70 meters. But when I switched to my wired connection (the same providing my Wi-Fi network!), it was reporting a 27000 meters accuracy!
The code
I am pretty sure you want to see how much code is actually required. Will you believe me if I tell you about 50 lines? And these includes spacing!
At the very top of the form, you need to import the namespace:
Imports System.Device.Location
Then, when the button is clicked, you need to create an instance, add a handler on the interesting event, and start the instance just created:
'Create the instance Dim watcher As New GeoCoordinateWatcher(GeoPositionAccuracy.High) 'Add an handler to the PositionChanged event AddHandler watcher.PositionChanged, AddressOf watcher_PositionChanged 'Start the watcher Dim started As Boolean = watcher.TryStart(False, TimeSpan.FromMilliseconds(1000)) 'Report error if required If Not started Then MessageBox.Show("GeoCoordinateWatcher timed out on start.") End If
The PositionChanged event handler has a single line of code. It just call other methods providing the position just raised:
ReportPositionToList(e.Position.Location) ReportPositionToMap(e.Position.Location)
The ReportPositionToList method displays the latitude and longitude of the position along with a timestamp and an accuracy indicator:
ListBox1.Items.Add(String.Format("@{0} = Latitude: {1}, Longitude: {2}, Accuracy: {3}", Now.ToString("HH:mm:ss"), pCoordinate.Latitude, pCoordinate.Longitude, pCoordinate.HorizontalAccuracy)) ListBox1.SelectedIndex = ListBox1.Items.Count - 1
Finally, the ReportPositionToMap shows the position on a map (using Google) in the web browser control:
Dim latlong As String = pCoordinate.Latitude.ToString() + "," + pCoordinate.Longitude.ToString() 'Dim url As String = "http://www.bing.com/maps/?v=2&where1=" + latlong + "&lvl=15" Dim w As Integer = WebBrowser1.Size.Width If w > 50 Then w -= 50 Dim h As Integer = WebBrowser1.Size.Height If h > 50 Then h -= 50 Dim url As String = String.Format("http://maps.googleapis.com/maps/api/staticmap?center={0}&zoom=15&size={1}x{2}&sensor=false&markers=size:mid%7Ccolor:red%7C{0}", latlong, w, h) WebBrowser1.Navigate(url)
That’s it!
Regarding the maps
As you can see in my ReportPositionToMap, I have commented out a URL that was using Bing maps but I ended up using the Google Static Maps API.
The reason is that you are required to provide a key which I found a bit too much for the purpose of this article. Check this page for more details.
Other platforms
If you are building a WPF application, you should really look at the Bing Maps Windows Presentation Foundation Control.
If you are building a Windows Store application (Windows 8), you should use the Windows.Devices.Geolocation namespace.
Conclusion
A short article to end the year.
I hope you liked it and that you will be back next year for more articles!