Ari
Ari

Reputation: 583

How do I add a new GeoFence in a WP8.1 background task?

I have some code that allows me to add GeoFences to and trigger backgroundtasks in windows phone 8.1. In the background task I want to add a new GeoFence however on some coordinates this leads to a aghost.exe has exited with code 1 error. Is there a way to add geofences in the background consistently so that it doesn't crash?

Upvotes: 1

Views: 212

Answers (1)

Fred
Fred

Reputation: 3362

Shouldn't be a problem. I do permanent location tracking using geofences. This is a piece of code I'm using. Geofencing works for me (except for the known issues).

public void AddGeoFence(Geopoint gp, String name, double radius)
{
    // Always remove the old fence if there is any
    var oldFence = GeofenceMonitor.Current.Geofences.Where(gf => gf.Id == name).FirstOrDefault();
    if (oldFence != null)
        GeofenceMonitor.Current.Geofences.Remove(oldFence);
    Geocircle gc = new Geocircle(gp.Position, radius);
    // Just listen for exit geofence
    MonitoredGeofenceStates mask = 0;
    mask |= MonitoredGeofenceStates.Exited;
    // Construct and add the fence
    Geofence newFence = new Geofence(new string(name.ToCharArray()), gc, mask, false, TimeSpan.FromSeconds(7), DateTimeOffset.Now, new TimeSpan(0));
    GeofenceMonitor.Current.Geofences.Add(newFence);
}

Upvotes: 1

Related Questions