Android のバックグラウンドGPSを組み込み

This commit is contained in:
2024-05-06 00:06:38 +09:00
parent 7a97127a19
commit dd9343bef7
89 changed files with 521 additions and 273 deletions

View File

@ -39,6 +39,8 @@ class LocationController extends GetxController {
LatLng? lastValidLocation;
DateTime lastGPSDataReceivedTime = DateTime.now(); // 最後にGPSデータを受け取った時刻
bool gpsDebugMode = false;
/*
// GPSシミュレーション用のメソッドを追加
void setSimulationMode(bool value) {
isSimulationMode = value;
@ -52,7 +54,7 @@ class LocationController extends GetxController {
bool isSimulationMode = false;
// GPS信号強度をシミュレートするための変数
final Rx<String> _simulatedSignalStrength = Rx<String>('low');
final Rx<String> _simulatedSignalStrength = Rx<String>('high');
// GPS信号強度をシミュレートするための関数
void setSimulatedSignalStrength(String strength) {
@ -72,31 +74,38 @@ class LocationController extends GetxController {
return _simulatedSignalStrength.value;
}
*/
//
// ====== Akira , GPS信号強度をシミュレート ==== ここまで
// GPS信号が弱い場合のフラグ. 本番では、true,high にする。
bool isGpsSignalWeak = true;
RxString latestSignalStrength = 'low'.obs;
// GPS信号が弱い場合のフラグ. 本番では、false,high にする。
bool isGpsSignalWeak = false;
RxString latestSignalStrength = 'high'.obs;
//final _latestSignalStrength = 'low'.obs; // 初期値を設定
//String get latestSignalStrength => _latestSignalStrength.value;
Stream<String> get gpsSignalStrengthStream => latestSignalStrength.stream;
bool isRunningBackgroundGPS=false;
int activeEngineCount = 0;
// GPS信号の強弱を判断するメソッドを追加. 10m 以内強、30m以内中、それ以上
//
String getGpsSignalStrength(Position? position) {
if (position == null) {
gpsDebugMode ? debugPrint("getGpsSignalStrength position is null.") : null;
latestSignalStrength.value = "low";
isGpsSignalWeak = true;
return 'low';
}
final accuracy = position.accuracy;
//debugPrint("getGpsSignalStrength : ${accuracy}");
gpsDebugMode ? debugPrint("getGpsSignalStrength : ${accuracy}") : null;
/*
if(isSimulationMode){
return _simulatedSignalStrength.value; // GPS信号強度シミュレーション
}else {
*/
if (accuracy <= 10) {
latestSignalStrength.value = "high";
isGpsSignalWeak = false;
@ -110,7 +119,7 @@ class LocationController extends GetxController {
isGpsSignalWeak = true;
return 'low';
}
}
// }
}
// 現在位置を調整するメソッドを追加
@ -130,7 +139,7 @@ class LocationController extends GetxController {
//debugPrint("=== adjustCurrentLocation (Position:Get and return Valid location:${position} ... )===");
lastValidLocation = LatLng(position.latitude, position.longitude);
}
return lastValidLocation ?? LatLng(position.latitude, position.longitude);
return lastValidLocation ?? LatLng(lastValidLocation!.latitude, lastValidLocation!.longitude);
}
//===== Akira Added 2024-4-9 end
@ -256,7 +265,7 @@ class LocationController extends GetxController {
// 位置情報の設定を行います。z11
// Set up the location options
const locationOptions =
LocationSettings(accuracy: LocationAccuracy.high, distanceFilter: 0);
LocationSettings(accuracy: LocationAccuracy.medium, distanceFilter: 0);
// 既存の位置情報のストリームをキャンセルします。
await positionStream?.cancel();
@ -265,11 +274,14 @@ class LocationController extends GetxController {
//
positionStream = Geolocator.getPositionStream(locationSettings: locationOptions).listen(
(Position? position) {
gpsDebugMode ? debugPrint("Position = ${position}"):null;
final signalStrength = getGpsSignalStrength(position);
if (signalStrength == 'low') {
isGpsSignalWeak = true;
gpsDebugMode ? debugPrint("LocationController.getPositionStream : isGpsSignalWeak = ${isGpsSignalWeak}"):null;
} else {
isGpsSignalWeak = false;
gpsDebugMode ? debugPrint("LocationController.getPositionStream : isGpsSignalWeak = ${isGpsSignalWeak}"):null;
}
final DestinationController destinationController = Get.find<DestinationController>();
@ -349,9 +361,15 @@ class LocationController extends GetxController {
void handleLocationUpdate(LocationMarkerPosition? position) async {
if (position != null) {
//debugPrint("position = ${position}");
/*
//debugPrint("locationController.handleLocationUpdate");
try {
if (position != null) {
double currentLat = position.latitude;
double currentLon = position.longitude;
//debugPrint("Flutter: Received GPS signal. Latitude: $currentLat, Longitude: $currentLon");
//debugPrint("position = ${position}");
/*
currentPosition.value = position;
final locationMarkerPosition = LocationMarkerPosition(
latitude: position.latitude,
@ -359,8 +377,13 @@ class LocationController extends GetxController {
accuracy: position.accuracy,
);
*/
lastGPSDataReceivedTime = DateTime.now(); // 最後にGPS信号を受け取った時刻
locationMarkerPositionStreamController.add(position);
lastGPSDataReceivedTime = DateTime.now(); // 最後にGPS信号を受け取った時刻
locationMarkerPositionStreamController.add(position);
}else{
gpsDebugMode ? debugPrint("Flutter: No GPS signal received."):null;
}
} catch( e ) {
debugPrint("Flutter: Error in handleLocationUpdate: $e");
}
}