I want to change the DesiredAccuracy and ReportInterval in PositionChanged Event Handler, so that I can dynamically change the position update frequency at different locations.
I did something like this,
void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
geolocator.StatusChanged -= geolocator_StatusChanged;
geolocator.PositionChanged -= geolocator_PositionChanged;
geolocator.DesiredAccuracy = PositionAccuracy.High;
geolocator.ReportInterval = 5 * 1000;
geolocator.StatusChanged += geolocator_StatusChanged;
geolocator.PositionChanged += geolocator_PositionChanged;
}
But the problem is I got
$exception {System.Exception: Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT))
at
Windows.Devices.Geolocation.Geolocator.put_DesiredAccuracy(PositionAccuracy value)
I do not understand this exception because it doesn't state the reason.
How can I achieve this (to change the accuracy & reporting interval dynamically)?
Thanks.
According to this Microsoft article, your exception indicates that you've disabled the location service from your phones setting:
catch (Exception ex)
{
if ((uint)ex.HResult == 0x80004004)
{
// the application does not have the right capability or the location master switch is off
StatusTextBlock.Text = "location is disabled in phone settings.";
}
//else
{
// something else happened acquring the location
}
}
It might be best if you move to using GeoCoordinateWatcher and invoke Stop()/Start() before changing these properties. There are a few advantages to use GeoLocator over GeoCoordinateWatcher, but nothing critical for most apps. Since GeoCoordinateWatcher is still fully supported on WP8 it might just be easier on you to switch to it if that's feasible.
As you already did in your code, removing all handlers of StatusChanged
and PositionChanged
is required to be able to modify ReportInterval
etc. Otherwise this exception (0x80004004) will be thrown.
In my case, removing all handlers was not an option because i'm using the Geolocator
in the my WP8 app to keep my app alive in background. Removing the last handler will also suspend my app because, from WP's point of view, there's no reason to keep the app alive in background.
I found out that it is possible to overcome this problem by creating a temporary Geolocator
:
// Wire up a temporary Geolocator to prevent the app from closing
var tempGeolocator = new Geolocator
{
MovementThreshold = 1,
ReportInterval = 1
};
TypedEventHandler<Geolocator, PositionChangedEventArgs> dummyHandler = (sender, positionChangesEventArgs2) => { };
tempGeolocator.PositionChanged += dummyHandler;
Geolocator.PositionChanged -= OnGeolocatorOnPositionChanged;
Geolocator.ReportInterval = reportInterval;
Geolocator.PositionChanged += OnGeolocatorOnPositionChanged;
tempGeolocator.PositionChanged -= dummyHandler;
This way the app will not be killed by WP. Keep in mind that rebinding the PositionChanged
will also cause an immediate callback.
User contributions licensed under CC BY-SA 3.0