Reputation: 5
I'm following the guide here.
And when all is done (I create the Geofence
and put it on the map), if I enter the Geofence
area navigating to it or creating it on my position nothing happens.
The OnGeofenceStateChanged
event is not called, and so I don't know how to manage the enter in Geofence
area event.
What do I do wrong?
Code
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<-------
}
Upvotes: 0
Views: 124
Reputation: 39082
You have to make sure to add the Geofence
you create to the GeofenceMonitor.Current.Geofences
collection so that the monitor knows about it. It is not very clear from the docs, but this is where it has to be. Also according to the samples (the sample stores the reference to the collection in a geofences
field and uses it instead of GeofenceMonitor.Current.Geofences
directly), it is recommended to wrap the addition to list in a exception handler if the monitor fails to add it:
try
{
GeofenceMonitor.Current.Geofences.Add( geofence );
}
catch
{
//adding failed
}
I have now sent a pull request to update the docs to illustrate Geofence
registration as well.
Upvotes: 1