在地理围栏上输入不要打开OnGeofenceStateChanged

时间:2018-02-12 21:10:12

标签: c# uwp monitoring geofencing uwp-maps

我正在关注guide here

当一切都完成后(我创建Geofence并将其放在地图上),如果我进入导航到它的Geofence区域或在我的位置创建它没有任何反应。

未调用OnGeofenceStateChanged事件,因此我不知道如何管理Geofence区域事件中的输入。

我做错了什么?

代码

private void DrawLOCNAME(double lat, double lon, string LOCNAME)
{    
    // Set the fence ID.
    string fenceId = "LOCNAME";

    // Define the fence location and radius.
    BasicGeoposition position;
    position.Latitude = lat;
    position.Longitude = lon;
    position.Altitude = 0.0;

    // Set a circular region for the geofence.
    Geocircle geocircle = new Geocircle(position, 2000);

    // Set the monitored states.
    MonitoredGeofenceStates monitoredStates =
        MonitoredGeofenceStates.Entered |
        MonitoredGeofenceStates.Exited |
        MonitoredGeofenceStates.Removed;

    // Set how long you need to be in geofence for the enter event to fire.
    TimeSpan dwellTime = TimeSpan.FromSeconds(1);

    //non so se è giusto ssettarlo a zero così
    TimeSpan duration = TimeSpan.FromDays(1);

    // Set up the start time of the geofence.
    DateTimeOffset startTime = DateTime.Now;

    // Create the geofence.
    Geofence geofence = new Geofence(fenceId, geocircle, monitoredStates, false, dwellTime, startTime, duration);

    // Register for state change events.
    GeofenceMonitor.Current.GeofenceStateChanged += OnGeofenceStateChanged;
    //GeofenceMonitor.Current.StatusChanged += OnGeofenceStatusChanged;

    // Center the map over the POI.
    Mappe.Center = snPoint;
    //Mappe.ZoomLevel = 14;
}

public async void OnGeofenceStateChanged(GeofenceMonitor sender, object args)
{
    var reports = sender.ReadReports();

    //BLABLABLA IS NOT IMPORTANT 

    ------->I'M NOT ABLE TO ENTER HERE<-------
}

1 个答案:

答案 0 :(得分:1)

您必须确保将您创建的Geofence添加到GeofenceMonitor.Current.Geofences集合中,以便监视器了解它。从文档中不是很清楚,但这是必须的。另外根据示例(示例在geofences字段中存储对集合的引用并直接使用它而不是GeofenceMonitor.Current.Geofences),建议在例外处理程序中将添加包装到列表中监视器无法添加它:

try
{
    GeofenceMonitor.Current.Geofences.Add( geofence );
}
catch
{
    //adding failed
}

我现在已发送拉取请求来更新文档以说明Geofence注册。