A more accurate geofencing

This commit is contained in:
Marc Rejohn Castillano 2026-02-18 20:43:33 +08:00
parent 8c1bb7646e
commit 15ce7b7a10
2 changed files with 54 additions and 10 deletions

View File

@ -407,16 +407,20 @@ class _ScheduleTile extends ConsumerWidget {
), ),
); );
final isInside = geofence.hasPolygon if (!geofence.hasPolygon) {
? geofence.containsPolygon(position.latitude, position.longitude) if (!context.mounted) return;
: geofence.hasCircle && await _showAlert(
Geolocator.distanceBetween( context,
title: 'Geofence missing',
message: 'Geofence polygon is not configured.',
);
return;
}
final isInside = geofence.containsPolygon(
position.latitude, position.latitude,
position.longitude, position.longitude,
geofence.lat!, );
geofence.lng!,
) <=
geofence.radiusMeters!;
if (!isInside) { if (!isInside) {
if (!context.mounted) return; if (!context.mounted) return;

40
test/geofence_test.dart Normal file
View File

@ -0,0 +1,40 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:tasq/models/app_settings.dart';
void main() {
// Polygon taken from user's KML (CRMC)
final polygonJson = [
{"lat": 7.2025231, "lng": 124.2339774},
{"lat": 7.2018791, "lng": 124.2334463},
{"lat": 7.2013362, "lng": 124.2332049},
{"lat": 7.2011393, "lng": 124.2332907},
{"lat": 7.2009531, "lng": 124.2334195},
{"lat": 7.200655, "lng": 124.2339344},
{"lat": 7.2000749, "lng": 124.2348465},
{"lat": 7.199501, "lng": 124.2357645},
{"lat": 7.1990597, "lng": 124.2364595},
{"lat": 7.1986557, "lng": 124.2371331},
{"lat": 7.1992252, "lng": 124.237168},
{"lat": 7.199494, "lng": 124.2372713},
{"lat": 7.1997415, "lng": 124.2374604},
{"lat": 7.1999383, "lng": 124.2377071},
{"lat": 7.2001938, "lng": 124.2380934},
{"lat": 7.2011411, "lng": 124.2364357},
{"lat": 7.2025231, "lng": 124.2339774},
];
test('GeofenceConfig.fromJson parses polygon and containsPolygon works', () {
final cfg = GeofenceConfig.fromJson({"polygon": polygonJson});
expect(cfg.hasPolygon, isTrue);
// Point clearly inside the CRMC polygon
final insideLat = 7.2009;
final insideLng = 124.2360;
expect(cfg.containsPolygon(insideLat, insideLng), isTrue);
// Point clearly outside (north of polygon)
final outsideLat = 7.2060;
final outsideLng = 124.2360;
expect(cfg.containsPolygon(outsideLat, outsideLng), isFalse);
});
}