如何在WP8.1后台任务中添加新的GeoFence?

时间:2015-02-12 13:42:40

标签: c# windows-phone-8.1 geofencing

我有一些代码允许我在Windows Phone 8.1中添加GeoFences并触发backgroundtasks。在后台任务中,我想添加一个新的GeoFence但是在某些坐标上会导致aghost.exe退出并出现代码1错误。有没有办法在后台持续添加地理围栏,以免崩溃?

1 个答案:

答案 0 :(得分:1)

应该不是问题。我使用地理围栏进行永久位置跟踪。这是我正在使用的一段代码。地理围栏对我有用(除了已知问题)。

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);
}