As you well know new generation of apps interact with user more.I personally call them "clever-apps".Clever apps gets information from you and at background they provide with much content regarding the information given.
Some informations can be text based,images and many more including Location. Location-based apps are on market for a couple of years now and it shows they will still be around.Location is perfect instrument to access users spot in map and provide the user with content.The content can be nearest "things",friendship like Happn,networking like Swarm or e-commerce products.
In UWP apps,we access users location easily.
Here,I will be showing you how to achive this:
Note: Before starting make sure you have set permissions for accessing location in your manifest file.
You can open Manifest XML file and add that capabilities:
- <Capabilities>
- <DeviceCapability Name="location"/>
- </Capabilities>
Or you can open Manifest in Designer View,get to Capabilities section and tick "Location" capability.
Lets write some to access it!
First of all,I'd like to mention that you don't get location directly without asking user!
You request it:
- var access = await Geolocator.RequestAccessAsync();
You won't be getting location information until user accepts it.And it shows only once,so if you accept it,next run the app won't be asking you again.
Next,you need to use cases to see if access is allowed:
- switch (access)
- {
- case GeolocationAccessStatus.Allowed:
- Geolocator geolocator = new Geolocator { DesiredAccuracyInMeters = _desireAccuracyInMetersValue };
- geolocator.StatusChanged += OnStatusChanged;
- Geoposition pos = await geolocator.GetGeopositionAsync();
- break;
-
- case GeolocationAccessStatus.Denied:
-
- break;
- }
Here we control 2 cases whether its allowed or not.If allowed,we'll be getting information.
You should also check if location status changes due to disconnectivity.
- async private void OnStatusChanged(Geolocator sender, StatusChangedEventArgs e)
- {
- await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
- {
- switch (e.Status)
- {
- case PositionStatus.Ready:
-
- break;
-
- case PositionStatus.Initializing:
-
- break;
-
- case PositionStatus.NoData:
-
- break;
-
- case PositionStatus.Disabled:
-
- break;
-
- case PositionStatus.NotInitialized:
-
- break;
-
- case PositionStatus.NotAvailable:
-
- break;
-
- default:
-
- break;
- }
- });
- }
As a last job,you need to get the Latitude and Longitude information from GeoPosition object:
-
- Lat = (float)pos.Coordinate.Point.Position.Latitude;
-
-
- Lon = (float)pos.Coordinate.Point.Position.Longitude;
Thats all for accessing user location. Latitude and Longitude is all you need for getting users location.