Skip to content

Commit

Permalink
feat: check man out of safe zone and changed zone color
Browse files Browse the repository at this point in the history
  • Loading branch information
SP-XD committed Apr 17, 2023
1 parent ac6a985 commit 671927e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
20 changes: 15 additions & 5 deletions lib/screens/homescreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class RocketSocket extends StatefulWidget {
class _RocketSocketState extends State<RocketSocket> {
final mapController = MapController();
LatLng? currentLocation;
LatLng? safeAreaLocation;
bool? isTransmitting = false;
WSServices wsServices = WSServices();
Timer? timer;
Expand All @@ -30,7 +31,10 @@ class _RocketSocketState extends State<RocketSocket> {
void initState() {
setUserId();
isTransmitting = false;

// dummy safe area location
safeAreaLocation = LatLng(22.5310, 88.3260);
// fake until current location is fetched
currentLocation = LatLng(22.5726, 88.3639);
getCurrentLocation();
super.initState();
}
Expand Down Expand Up @@ -95,6 +99,7 @@ class _RocketSocketState extends State<RocketSocket> {
.listen((Position? position) {
setState(() {
currentLocation = LatLng(position!.latitude, position.longitude);
// safeAreaLocation = currentLocation;
mapController.move(currentLocation!, 15);
});
});
Expand Down Expand Up @@ -127,6 +132,11 @@ class _RocketSocketState extends State<RocketSocket> {
// Temporary timer for testing
debugPrint("Start Transmitting");
timer = Timer.periodic(const Duration(seconds: 1), (timer) {
manOutOfBound = isManOutOfBound(
safeAreaLocation!.latitude,
safeAreaLocation!.longitude,
currentLocation!.latitude,
currentLocation!.longitude);
setState(() {
transmitLocation();
getCurrentLocation();
Expand Down Expand Up @@ -162,7 +172,7 @@ class _RocketSocketState extends State<RocketSocket> {
child: FlutterMap(
mapController: mapController,
options: MapOptions(
center: currentLocation ?? LatLng(22.5726, 88.3639),
center: currentLocation,
// bounds: LatLngBounds(
// LatLng(29, 77.8963), LatLng(29.8659, 77.8963)),
),
Expand All @@ -177,9 +187,9 @@ class _RocketSocketState extends State<RocketSocket> {
circles: [
CircleMarker(
// hardcoded area bounds for ground personal
point: LatLng(22.5310, 88.3260),
color: Colors.red.withOpacity(0.5),
borderColor: Colors.red,
point: safeAreaLocation!,
color: Colors.green.withOpacity(0.5),
borderColor: Colors.green,
borderStrokeWidth: 1,
useRadiusInMeter: true,
radius: 8000,
Expand Down
20 changes: 20 additions & 0 deletions lib/utils/utils.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:uuid/uuid.dart';
Expand All @@ -15,3 +17,21 @@ getUserId() async {
String? userId = prefs.getString("userId"); //returns null if doesn't exist
return userId;
}

double calculateDistance(lat1, lon1, lat2, lon2) {
var p = 0.017453292519943295;
var c = cos;
var a = 0.5 -
c((lat2 - lat1) * p) / 2 +
c(lat1 * p) * c(lat2 * p) * (1 - c((lon2 - lon1) * p)) / 2;
// result in meters
return 12742 * asin(sqrt(a)) * 1000;
}

bool isManOutOfBound(lat1, lon1, lat2, lon2) {
var distance = calculateDistance(lat1, lon1, lat2, lon2);
if (distance >= 8000) {
return true;
}
return false;
}

0 comments on commit 671927e

Please sign in to comment.