0

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<-------
}
Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
Uopo
  • 5
  • 2

1 Answers1

1

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.

Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
  • If the answer solved your problem, please consider accepting the answer :-). Happy coding! – Martin Zikmund Feb 14 '18 at 16:33
  • thanks for all, anyway with that code worked only the first time, now i go only in catch with the exception which told me that "for the application exist yet a geofence with that ID" so i put a GeofenceMonitor.Current.Geofences.Remove( geofence ); before the add and nothing changed. does i miss something ? sure i need to see the example but in the map sample there are 11 scenarios and with a first look and search i havent found nothing on geofence. – Uopo Feb 14 '18 at 16:39
  • You first have to find existing one using Where or Clear the collection ( GeofenceMonitor.Current.Geofences.Clear();) – Martin Zikmund Feb 14 '18 at 16:43
  • The Remove does not work, because the instance is a different reference than the one that already exists in the collection. You could find the existing one in the GeofenceMonitor.Current.Geofences collection – Martin Zikmund Feb 14 '18 at 16:46
  • Let me know how it goes :-) ! – Martin Zikmund Feb 14 '18 at 22:42