153 lines
5.4 KiB
Dart
153 lines
5.4 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:flutter_map/flutter_map.dart';
|
|
import 'package:flutter_map_location_marker/flutter_map_location_marker.dart';
|
|
import 'package:latlong2/latlong.dart';
|
|
import 'package:gifunavi/pages/index/index_controller.dart';
|
|
import 'package:gifunavi/utils/location_controller.dart';
|
|
import 'package:gifunavi/pages/destination/destination_controller.dart';
|
|
import 'package:gifunavi/pages/settings/settings_controller.dart';
|
|
import 'package:gifunavi/widgets/base_layer_widget.dart';
|
|
import 'package:gifunavi/widgets/game_state_view.dart';
|
|
import 'package:gifunavi/widgets/current_position_widget.dart';
|
|
|
|
class MapWidget extends StatelessWidget {
|
|
final IndexController indexController = Get.find<IndexController>();
|
|
final DestinationController destinationController = Get.find<DestinationController>();
|
|
final LocationController locationController = Get.find<LocationController>();
|
|
final SettingsController settingsController = Get.find<SettingsController>();
|
|
|
|
MapWidget({Key? key}) : super(key: key) {
|
|
_initializeControllers();
|
|
}
|
|
|
|
void _initializeControllers() {
|
|
indexController.initMapController();
|
|
_startIdleTimer();
|
|
|
|
ever(indexController.isMapControllerReady, (_) {
|
|
if (indexController.isMapControllerReady.value) {
|
|
_initMarkers();
|
|
}
|
|
});
|
|
}
|
|
|
|
void _startIdleTimer() {
|
|
if (!settingsController.autoReturnDisabled.value) {
|
|
Future.delayed(settingsController.timerDuration.value, _centerMapOnUser);
|
|
}
|
|
}
|
|
|
|
void _centerMapOnUser() {
|
|
destinationController.centerMapToCurrentLocation();
|
|
_startIdleTimer();
|
|
}
|
|
|
|
Future<void> _initMarkers() async {
|
|
indexController.markers.value = await _getMarkers();
|
|
}
|
|
|
|
Future<List<Marker>> _getMarkers() async {
|
|
if (indexController.isLoadingLocations.value) {
|
|
await indexController.waitForLocationsToLoad();
|
|
}
|
|
|
|
List<Marker> markers = [];
|
|
if (indexController.locations.isNotEmpty && indexController.locations[0].features.isNotEmpty) {
|
|
for (var feature in indexController.locations[0].features) {
|
|
// マーカーの作成ロジック
|
|
// 実際のマーカー作成ロジックをここに実装してください
|
|
}
|
|
} else {
|
|
debugPrint('No locations or features available');
|
|
}
|
|
return markers;
|
|
}
|
|
|
|
List<LatLng>? getPoints() {
|
|
return indexController.routePoints.map((p) => LatLng(p.latitude, p.longitude)).toList();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Stack(
|
|
children: [
|
|
Obx(() => indexController.isLoading.value
|
|
? const Center(child: CircularProgressIndicator())
|
|
: FlutterMap(
|
|
mapController: indexController.mapController,
|
|
options: MapOptions(
|
|
maxZoom: 18.4,
|
|
onMapReady: () {
|
|
indexController.isMapControllerReady.value = true;
|
|
},
|
|
initialCenter: const LatLng(37.15319600454702, 139.58765950528198),
|
|
bounds: indexController.currentBound.isNotEmpty
|
|
? indexController.currentBound[0]
|
|
: LatLngBounds.fromPoints([
|
|
const LatLng(35.03999881162295, 136.40587119778962),
|
|
const LatLng(36.642756778706904, 137.95226720406063)
|
|
]),
|
|
initialZoom: 1,
|
|
interactiveFlags: InteractiveFlag.pinchZoom | InteractiveFlag.drag,
|
|
onPositionChanged: (MapPosition pos, bool hasGesture) {
|
|
if (hasGesture) {
|
|
_startIdleTimer();
|
|
}
|
|
indexController.currentBound = [pos.bounds!];
|
|
},
|
|
onMapEvent: (MapEvent mapEvent) {
|
|
if (mapEvent is MapEventMove) {
|
|
destinationController.shouldShowBottomSheet = true;
|
|
}
|
|
},
|
|
),
|
|
children: [
|
|
const BaseLayer(),
|
|
Obx(() => indexController.routePointLenght > 0
|
|
? PolylineLayer(
|
|
polylines: [
|
|
Polyline(
|
|
points: getPoints()!,
|
|
strokeWidth: 6.0,
|
|
color: Colors.indigo,
|
|
),
|
|
],
|
|
)
|
|
: Container()),
|
|
CurrentLocationLayer(
|
|
positionStream: locationController.locationMarkerPositionStream,
|
|
style: const LocationMarkerStyle(
|
|
marker: Stack(
|
|
children: [
|
|
CircleAvatar(
|
|
radius: 13.5,
|
|
backgroundColor: Colors.blue,
|
|
child: Icon(Icons.navigation, color: Colors.white),
|
|
),
|
|
],
|
|
),
|
|
markerSize: Size(27, 27),
|
|
markerDirection: MarkerDirection.heading,
|
|
),
|
|
),
|
|
Obx(() => MarkerLayer(markers: indexController.markers)),
|
|
],
|
|
)),
|
|
const Positioned(top: 0, left: 0, child: GameStateWidget()),
|
|
const Positioned(bottom: 10, right: 10, child: CurrentPosition()),
|
|
Obx(() => indexController.currentMarkerPosition.value != null
|
|
? Container() // 現在のマーカー位置が更新されたときの処理
|
|
: Container()),
|
|
],
|
|
);
|
|
}
|
|
}
|
|
|
|
class MapResetController extends GetxController {
|
|
void Function()? resetIdleTimer;
|
|
|
|
void setResetIdleTimer(void Function() resetFunction) {
|
|
resetIdleTimer = resetFunction;
|
|
}
|
|
} |