Destination controller から Location controller へGPS信号シミュレーションを変更

This commit is contained in:
2024-04-09 15:06:41 +09:00
parent 8ad2e5988d
commit f6b2a6c7d4
9 changed files with 184 additions and 63 deletions

View File

@ -2,7 +2,8 @@ import 'dart:async';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:geolocator/geolocator.dart';
import 'package:rogapp/widgets/debug_widget.dart';
import 'package:latlong2/latlong.dart';
//import 'package:rogapp/widgets/debug_widget.dart';
import 'package:flutter_map_location_marker/flutter_map_location_marker.dart';
// LocationControllerクラスは、GetxControllerを継承したクラスであり、位置情報の管理を担当しています。
@ -10,12 +11,21 @@ import 'package:flutter_map_location_marker/flutter_map_location_marker.dart';
// LocationControllerは、アプリ全体で位置情報を一元管理するための重要なコンポーネントです。
// 他のコンポーネントは、LocationControllerから位置情報を取得し、位置情報に関連する機能を実装することができます。
//
// Features:
// * 現在の位置情報を保持し、他のコンポーネントからアクセスできるようにします。
// * 位置情報のストリームを管理し、位置情報の更新を監視します。
// * 位置情報サービスの有効性と権限の確認を行い、適切な処理を行います。
// * 位置情報のストリームを開始、停止、再開する機能を提供します。
// * 位置マーカーの位置情報をStreamControllerを通じて他のコンポーネントに通知します。
//
// Logic:
// 1. startPositionStreamメソッドで、Geolocator.getPositionStreamを使用して位置情報のストリームを開始します。
// 2. ストリームから位置情報を受信すると、LocationMarkerPositionオブジェクトを作成し、locationMarkerPositionStreamControllerに追加します。
// 3. 位置情報が取得できなかった場合や、エラーが発生した場合は、適切な処理を行います。
// 4. stopPositionStreamメソッドで、位置情報のストリームを一時停止することができます。
// 5. resumePositionStreamメソッドで、一時停止中の位置情報のストリームを再開することができます。
// 6. onCloseメソッドで、コントローラーのクローズ時に位置情報のストリームをキャンセルします。
//
class LocationController extends GetxController {
// Reactive variable to hold the current position
Rx<Position?> currentPosition = Rx<Position?>(null);
@ -25,6 +35,57 @@ class LocationController extends GetxController {
StreamSubscription<Position>? positionStream;
// 位置情報のストリームを保持する変数です。StreamSubscription<Position>型で宣言されています。
//===== Akira Added 2024-4-9 start
// GPSシミュレーション用の変数を追加
bool isSimulationMode = true;
LatLng? lastValidLocation;
// GPSシミュレーション用のメソッドを追加
void setSimulationMode(bool value) {
isSimulationMode = value;
}
String latestSignalStrength="low";
// GPS信号の強弱を判断するメソッドを追加. 10m 以内強、30m以内中、それ以上
//
String getGpsSignalStrength(Position? position) {
if (position == null) {
latestSignalStrength="low";
return 'low';
}
final accuracy = position.accuracy;
if(isSimulationMode){
return _simulatedSignalStrength.value; // GPS信号強度シミュレーション
}else {
if (accuracy <= 10) {
latestSignalStrength="high";
return 'high';
} else if (accuracy <= 30) {
latestSignalStrength="medium";
return 'medium';
} else {
latestSignalStrength="low";
return 'low';
}
}
}
// 現在位置を調整するメソッドを追加
LatLng? adjustCurrentLocation(Position? position) {
if (position == null) {
return null;
//return lastValidLocation ?? LatLng(0, 0);
}
final signalStrength = getGpsSignalStrength(position);
if (signalStrength == 'high' || signalStrength == 'medium') {
lastValidLocation = LatLng(position.latitude, position.longitude);
}
return lastValidLocation ?? LatLng(position.latitude, position.longitude);
}
//===== Akira Added 2024-4-9 end
final locationMarkerPositionStreamController =
StreamController<LocationMarkerPosition?>.broadcast();
// 位置マーカーの位置情報を送信するためのStreamControllerです。
@ -36,7 +97,7 @@ class LocationController extends GetxController {
//
// GPS信号強度をシミュレートするための変数
Rx<String> _simulatedSignalStrength = Rx<String>('high');
final Rx<String> _simulatedSignalStrength = Rx<String>('high');
// GPS信号強度をシミュレートするための関数
void setSimulatedSignalStrength(String strength) {
@ -82,6 +143,8 @@ class LocationController extends GetxController {
//
void startPositionStream() async {
// Check for location service and permissions before starting the stream
// 位置情報サービスの有効性をチェックし、無効な場合はエラーハンドリングを行います。
//
bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
// Use GetX's context to show a dialog
@ -107,6 +170,8 @@ class LocationController extends GetxController {
return;
}
// 位置情報の権限をチェックし、拒否されている場合はエラーハンドリングを行います。
//
LocationPermission permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
@ -158,15 +223,37 @@ class LocationController extends GetxController {
return;
}
// 位置情報の設定を行います。
// Set up the location options
const locationOptions =
LocationSettings(accuracy: LocationAccuracy.high, distanceFilter: 0);
// 既存の位置情報のストリームをキャンセルします。
await positionStream?.cancel();
// 新しい位置情報のストリームを開始します。
//
positionStream = Geolocator.getPositionStream(locationSettings: locationOptions).listen(
(Position? position) {
final adjustedLocation = adjustCurrentLocation(position);
if (adjustedLocation!=null) {
final locationMarkerPosition = LocationMarkerPosition(
latitude: adjustedLocation.latitude,
longitude: adjustedLocation.longitude,
accuracy: position?.accuracy ?? 0,
);
locationMarkerPositionStreamController.add(locationMarkerPosition);
}else{
// 位置情報が取得できなかった場合、
// locationMarkerPositionStreamControllerにnullを追加します。
locationMarkerPositionStreamController.add(null);
}
/*
if (position != null) {
// ストリームから位置情報を受信した場合、
// LocationMarkerPositionオブジェクトを作成し、
// locationMarkerPositionStreamControllerに追加します。
//
final LocationMarkerPosition locationMarkerPosition =
LocationMarkerPosition(
latitude: position.latitude,
@ -174,10 +261,14 @@ class LocationController extends GetxController {
accuracy: position.accuracy);
locationMarkerPositionStreamController.add(locationMarkerPosition);
} else {
// 位置情報が取得できなかった場合、
// locationMarkerPositionStreamControllerにnullを追加します。
locationMarkerPositionStreamController.add(null);
}
*/
},
onError: (e) {
// エラーが発生した場合、locationMarkerPositionStreamControllerにエラーを追加します。
locationMarkerPositionStreamController.addError(e);
},
onDone: () {
@ -187,6 +278,8 @@ class LocationController extends GetxController {
);
// Resume stream if it was paused previously
// ストリームが一時停止中の場合、ストリームを再開します。
//
if (isStreamPaused) {
isStreamPaused = false;
positionStream!.resume();