Location services are most commonly used in the mobile apps these days and it is necessary also, like for a restaurant app it is necessary to know where you are exactly before it starts suggesting you the options you can explore, without the use of location services the most successful apps today might have failed. In this post we will start learning how to use location service and maps in your windows phone application.
GeoLocation depends a lot on how you are running it though GPS is considered the most accurate option but consumes the battery faster and responds a bit slower, the other option we have is the cell data but again the issue with this is the accuracy i.e. it is not much accurate, the best results are experienced over WiFi or the new 4G mobile networks. In windows phone we can adjust the accuracy of the location services we are using, the options available to us are :
- PositionAccuracy.High - this provides the most accurate results but at the cost of increased battery use.
- PositionAccuracy.Default - this provides the results with optimal power.
- DesiredAccuracyInMeters - in this we can specify the value in meters the amount of accuracy we require in our app. (I preferably use this method only)
Getting Your Current Location
Before getting started with the code we need to add location capability to your app, for this goto WMAppManifest.xml under properties of your application.
private async void GetLocation(object sender, RoutedEventArgs e)
Geolocator gl= new Geolocator();
gl.DesiredAccuracyInMeters = 50;
Geoposition gp = await gl.GetGeopositionAsync(
string lat = gp.Coordinate.Latitude.ToString("0.00");
string longi = gp.Coordinate.Longitude.ToString("0.00");
MessageBox.Show("Latitude: " + lat + " Longitude: " + longi);
MessageBox.Show("location services disabled on phone");
We fist declared the GeoLocator object and mentioned the DesiredAccuracyInMeters, then we adjusted the parameters for GeoPosition i.e. maximumAge and timeout, maximumAge means the maximum acceptable age of cached location data and timeout is the maximum time allowed to get the location data else the exception occurs. GeoPosition.Coordinate gives the coordinate of the location captured.
Plotting Location On Map
Now we have got the location lets see how we can plot it on a map, we will follow a three simple step process to do so.
- Add required namespaces.
- Add map control to your app.
Map MyMap = new Map();
- Set the map centre and adjust zoom level accordingly. (ZoomLevel value can vary from 1 to 20)
MyMap.Center = new GeoCoordinate(gp.Coordinate.Latitude, gp.Coordinate.Longitude);
MyMap.ZoomLevel = 10;
As you see in the above output we are not able to see where exactly is our location in the map shown on the screen, pushpins are the icons used to mark positions on the map. So lets add a pushpin to the map and make our position on map clear. We can create a custom pushpin also and add a layer to the map, in this example we will use the standard pushpin available in the Windows Phone Toolkit. So our first task is to add the Windows Phone Toolkit to the project using Nuget packages.
Once you have added the Windows Phone Toolkit to your application you can start coding for Pushpin.
- Adding Namespaces.
- Creating Pushpin
Pushpin myPushpin = new Pushpin();
myPushpin.Content = "My Location";
- Creating MapOverlay, Adding Content, Adjusting Coordinate
MapOverlay MyOverlay = new MapOverlay();
MyOverlay.Content = myPushpin;
MyOverlay.GeoCoordinate = new GeoCoordinate(gp.Coordinate.Latitude, gp.Coordinate.Longitude);
MyOverlay.PositionOrigin = new Point(0, 0.5);
- Adding MapOverlay to MapLayer and MapLayer to you Map
MapLayer MyLayer = new MapLayer();
Now the output looks clear. This is a standard pushpin and in the next article we will learn to build a custom pushpin.