How to get the Geo location information in windows phone 8.1?
you have the GeoLocator class in Windows.Devices.GeoLocation namespace .
First thing you have to make sure as a developer is to enable the location capabilities in the Package.appxmanifest as shown below .
Below is a sample code to fetch the Location information .
private Geolocator _geolocator = null;
async private void GetGeolocation()
{
_geolocator = new Geolocator();
// Desired Accuracy needs to be set
// before polling for desired accuracy.
_geolocator.DesiredAccuracyInMeters = 50;
try
{
// Carry out the operation
Geoposition pos = await _geolocator.GetGeopositionAsync();
}
catch
{
/*Operation aborted Your App does not have permission to access location data.
Make sure you have defined ID_CAP_LOCATION in the application manifest and that on your phone,
you have turned on location by checking Settings > Location.*/
//if the location service is turned off then you can take the user to the location setting with the below code
await Launcher.LaunchUriAsync(new Uri("ms-settings-location:"));
}
}
** you can set the time out period to fetch the location information and also you can set the pool time interval before which the location API is called again.
Geoposition pos = await _geolocator.GetGeopositionAsync(TimeSpan.FromMinutes(10), TimeSpan.FromMinutes(15));
**above piece of code says that if a request is made get the location information then the system will check the time interval since the last request was made , if the request is made with in 10 minutes then the last cached value will be returned , if not he new values will be fetched and also the API will be timeout if the request does not respond with in 15 minutes of the request .
How to Cancel a request made to get the Geo location information?
if you have requested for location information and for some reason you want to cancel the request all you have to do is use the CancellationToken Object in conjunction with the GeoLocator object as shown below.private CancellationTokenSource _cts = null;
async private void GetGeolocation()
{
_geolocator = new Geolocator();
_cts = new CancellationTokenSource();
CancellationToken token = _cts.Token;
// Desired Accuracy needs to be set
// before polling for desired accuracy.
_geolocator.DesiredAccuracyInMeters = 50;
try
{
// Carry out the operation
Geoposition pos = await _geolocator.GetGeopositionAsync().AsTask(token);
}
catch
{
/*Operation aborted Your App does not have permission to access location data.
Make sure you have defined ID_CAP_LOCATION in the application manifest and that on your phone,
you have turned on location by checking Settings > Location.*/
//if the location service is turned off then you can take the user to the location setting with the below code
await Launcher.LaunchUriAsync(new Uri("ms-settings-location:"));
}
}
//Call this Method to cancel the current location access request
cancelGelocationRequest()
{
if (_cts != null)
{
_cts.Cancel();
_cts = null;
}
}
How to Track Location Changes?
If you want a real time info as soon as the user location changes then you have to subscribe to StatusChanged and PositionChanged Events of the GeoLocator object .
Below is the code snippet for the same .
Below is the code snippet for the same .
private Geolocator _geolocator = new Geolocator();
TrackMyLocation()
{
_geolocator.StatusChanged += _geolocator_StatusChanged;
_geolocator.PositionChanged += _geolocator_PositionChanged;
}
void _geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
// will get a new GeoPostion object in the args paramter which gives the updated lat long values
}
void _geolocator_StatusChanged(Geolocator sender, StatusChangedEventArgs args)
{
//gives s postionstatus object with below values
/* Ready
Initilizing
NoData
Disabled
NotInitilized
NotAvalible
*/
}


Thank you so much for sharing this one really well defined all peaceful info regarding Tracking app,I Really like it,Love it- real time location tracking app
ReplyDelete