Introduction
With the change of Windows Phone 7.5 to Windows Phone 8 API we have many changes in terms of layout, architecture and bug fixes, but there are many changes regarding the Geo Location classes. With Window Phone 8 SDK we have improvised classes that does the same job and is easy to use.
Agenda
- Create a Demo Layout
- Add using Windows.Devices.GeoLocation in CS file
- Set geo location finder instance
- And finally, set the Geo Position instance that returns the value of Latitude and Longitude.
Procedures
Step 1
Create a Blank Windows Phone Silverlight Project and try to mock the following layout.
![layout of Windows Phone Silverlight]()
And, enable ID_CAP_LOCATION the Capability in Properties > WMAppManifest.xml as in the following:
![application UI of Windows phone]()
Step 2
Now, we need to define the Geo Locator.
So, create a callback method for the Button's click event.
- Geolocator locationFinder = new Geolocator
- {
- DesiredAccuracyInMeters=50,
- DesiredAccuracy=PositionAccuracy.Default
- };
Here, we created an instance of the Geolocator class where we set the two fields, in other words DesiredAccuracyInMeter and DesiredAccuracy.
Step 3
Next, we need to define the Geopositon class that will provide the value of Latitude and Longitude. try
- {
- Geoposition currentLocation = await locationFinder.GetGeopositionAsync(maximumAge: TimeSpan.FromSeconds(120), timeout: TimeSpan.FromSeconds(10));
-
- longitude = currentLocation.Coordinate.Longitude.ToString("0.00");
- latitude = currentLocation.Coordinate.Latitude.ToString("0.00");
-
-
- tbLongitude.Text = longitude;
- tbLatitude.Text = latitude;
- }
-
- catch(Exception ex)
- {
-
- MessageBox.Show("ERROR :"+ex.Message.ToString());
-
- }
-
- Here, we have initialized Geoposition instance with two arguments (maximum age and timeout).
-
- While going all this, you need some fields which must be global.
-
-
- string longitude;
- string latitude;
The final code will look like:
- private async void btnGeoLocation_Click(object sender, RoutedEventArgs e)
- {
-
-
-
- Geolocator locationFinder = new Geolocator
- {
- DesiredAccuracyInMeters=50,
- DesiredAccuracy=PositionAccuracy.Default
- };
-
-
- try
- {
- Geoposition currentLocation = await locationFinder.GetGeopositionAsync(maximumAge: TimeSpan.FromSeconds(120), timeout: TimeSpan.FromSeconds(10));
-
- longitude = currentLocation.Coordinate.Longitude.ToString("0.00");
- latitude = currentLocation.Coordinate.Latitude.ToString("0.00");
-
-
- tbLongitude.Text = longitude;
- tbLatitude.Text = latitude;
- }
- catch(Exception ex)
- {
- MessageBox.Show("ERROR :"+ex.Message.ToString());
- }
-
- }
Output
After doing all this, we will get something like this.
![Geo Location Service in Windows Phone]()
Conclusion
In this demo app we have done something old that we have done earlier in Windows Phone 7.5 SDK, but this time we have used Windows Phone 8 SDK's class that let us do the same job in a few lines of code. Next, we will try to put this latitude-Longitude in a map.
For this demo, if you experience any issue then try to figure it out from the enclosed solution file.