2024-09-02 ほぼOK
This commit is contained in:
@ -129,20 +129,25 @@ class LocationController extends GetxController {
|
||||
|
||||
// 現在位置を調整するメソッドを追加
|
||||
LatLng? adjustCurrentLocation(Position? position) {
|
||||
if (position == null) {
|
||||
if (position == null) { // positionがnullなら、lastValidLocationを使用する。
|
||||
if( lastValidLocation!=null ) {
|
||||
debugPrint("== 現在位置なし。最後の位置を使用 ==");
|
||||
//debugPrint("=== adjustCurrentLocation (Position:Null and using LastValidLocation ${lastValidLocation})===");
|
||||
return LatLng(lastValidLocation!.latitude, lastValidLocation!.longitude);
|
||||
}else {
|
||||
print("=== adjustCurrentLocation (Position:Null and No LastValidLocation ... )===");
|
||||
debugPrint("== 現在位置なし。最後の位置も無し ==");
|
||||
//print("=== adjustCurrentLocation (Position:Null and No LastValidLocation ... )===");
|
||||
return null;
|
||||
}
|
||||
//return lastValidLocation ?? LatLng(0, 0);
|
||||
}
|
||||
final signalStrength = getGpsSignalStrength(position);
|
||||
if (signalStrength == 'high' || signalStrength == 'medium') {
|
||||
debugPrint("== 信号強度 ${signalStrength} ==> 最新位置を使用 ==");
|
||||
//debugPrint("=== adjustCurrentLocation (Position:Get and return Valid location:${position} ... )===");
|
||||
lastValidLocation = LatLng(position.latitude, position.longitude);
|
||||
}else{
|
||||
debugPrint("== 信号強度 ${signalStrength} ==> 最後の位置を使用 ==");
|
||||
}
|
||||
return lastValidLocation ?? LatLng(lastValidLocation!.latitude, lastValidLocation!.longitude);
|
||||
}
|
||||
@ -169,10 +174,39 @@ class LocationController extends GetxController {
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
// Start listening to location updates when the controller is initialized
|
||||
startPositionStream();
|
||||
_initLocationService();
|
||||
|
||||
}
|
||||
|
||||
Future<void> _initLocationService() async {
|
||||
try {
|
||||
bool serviceEnabled;
|
||||
LocationPermission permission;
|
||||
|
||||
serviceEnabled = await Geolocator.isLocationServiceEnabled();
|
||||
if (!serviceEnabled) {
|
||||
return Future.error('Location services are disabled.');
|
||||
}
|
||||
|
||||
permission = await Geolocator.checkPermission();
|
||||
if (permission == LocationPermission.denied) {
|
||||
permission = await Geolocator.requestPermission();
|
||||
if (permission == LocationPermission.denied) {
|
||||
return Future.error('Location permissions are denied');
|
||||
}
|
||||
}
|
||||
|
||||
if (permission == LocationPermission.deniedForever) {
|
||||
return Future.error(
|
||||
'Location permissions are permanently denied, we cannot request permissions.');
|
||||
}
|
||||
|
||||
startPositionStream();
|
||||
} catch( e ){
|
||||
print('Error initializing location service: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// 位置情報のストリームを開始するメソッドです。
|
||||
// 位置情報サービスが有効か確認し、無効な場合はダイアログを表示します。
|
||||
// 位置情報の権限を確認し、必要な権限がない場合は権限をリクエストします。
|
||||
@ -185,7 +219,26 @@ class LocationController extends GetxController {
|
||||
// 2024-4-8 Akira : See 2809
|
||||
// stopPositionStreamメソッドを追加して、既存のストリームをキャンセルするようにしました。また、ストリームが完了したらnullに設定し、エラー発生時にストリームをキャンセルするようにしました。
|
||||
//
|
||||
void startPositionStream() async {
|
||||
void startPositionStream() {
|
||||
positionStream = Geolocator.getPositionStream(
|
||||
locationSettings: const LocationSettings(
|
||||
accuracy: LocationAccuracy.high,
|
||||
distanceFilter: 5, //10, 10mから5mに変更
|
||||
),
|
||||
).listen((Position position) {
|
||||
currentPosition.value = position;
|
||||
//debugPrint("== startPositionStream: ${position} ==");
|
||||
locationMarkerPositionStreamController.add(
|
||||
LocationMarkerPosition(
|
||||
latitude: position.latitude,
|
||||
longitude: position.longitude,
|
||||
accuracy: position.accuracy,
|
||||
),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
void startPositionStream_old() async {
|
||||
// Check for location service and permissions before starting the stream
|
||||
// 位置情報サービスの有効性をチェックし、無効な場合はエラーハンドリングを行います。
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user