processLocationUpdate method

void processLocationUpdate(
  1. double latitude,
  2. double longitude
)

Processes a location update and emits events for polygon boundary crossings.

Call this method with each location update to trigger enter/exit events.

Implementation

void processLocationUpdate(double latitude, double longitude) {
  final now = DateTime.now();
  final triggerPoint = GeoPoint(latitude: latitude, longitude: longitude);

  for (final polygon in _polygons.values) {
    final wasInside = _insideState[polygon.identifier] ?? false;
    final isNowInside = polygon.containsPoint(latitude, longitude);

    if (!wasInside && isNowInside) {
      // Entered polygon
      _insideState[polygon.identifier] = true;

      if (polygon.notifyOnEntry) {
        _eventController.add(PolygonGeofenceEvent(
          geofence: polygon,
          type: PolygonGeofenceEventType.enter,
          timestamp: now,
          triggerLocation: triggerPoint,
        ));
        debugPrint('[PolygonGeofenceService] ENTER: ${polygon.identifier}');
      }
    } else if (wasInside && !isNowInside) {
      // Exited polygon
      _insideState[polygon.identifier] = false;

      if (polygon.notifyOnExit) {
        _eventController.add(PolygonGeofenceEvent(
          geofence: polygon,
          type: PolygonGeofenceEventType.exit,
          timestamp: now,
          triggerLocation: triggerPoint,
        ));
        debugPrint('[PolygonGeofenceService] EXIT: ${polygon.identifier}');
      }
    }
  }
}