70 lines
2.0 KiB
Dart
70 lines
2.0 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:google_maps_flutter/google_maps_flutter.dart';
|
|
import 'package:geolocator/geolocator.dart';
|
|
|
|
class MapView extends StatefulWidget {
|
|
const MapView({super.key});
|
|
|
|
@override
|
|
State<MapView> createState() => _MapViewState();
|
|
}
|
|
|
|
class _MapViewState extends State<MapView> {
|
|
GoogleMapController? _mapController;
|
|
LatLng? _currentLatLng;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_getCurrentLocation();
|
|
}
|
|
|
|
Future<void> _getCurrentLocation() async {
|
|
bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
|
|
if (!serviceEnabled) {
|
|
return Future.error('Location services are disabled.');
|
|
}
|
|
|
|
LocationPermission permission = await Geolocator.checkPermission();
|
|
if (permission == LocationPermission.denied) {
|
|
permission = await Geolocator.requestPermission();
|
|
if (permission == LocationPermission.denied) {
|
|
return Future.error('Location permission denied');
|
|
}
|
|
}
|
|
|
|
if (permission == LocationPermission.deniedForever) {
|
|
return Future.error('Location permission permanently denied');
|
|
}
|
|
|
|
Position position = await Geolocator.getCurrentPosition(
|
|
desiredAccuracy: LocationAccuracy.high);
|
|
|
|
setState(() {
|
|
_currentLatLng = LatLng(position.latitude, position.longitude);
|
|
});
|
|
|
|
_mapController?.animateCamera(CameraUpdate.newCameraPosition(
|
|
CameraPosition(target: _currentLatLng!, zoom: 15),
|
|
));
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text("Select Location")),
|
|
body: _currentLatLng == null
|
|
? const Center(child: CircularProgressIndicator())
|
|
: GoogleMap(
|
|
initialCameraPosition:
|
|
CameraPosition(target: _currentLatLng!, zoom: 15),
|
|
myLocationEnabled: true,
|
|
myLocationButtonEnabled: true,
|
|
onMapCreated: (controller) {
|
|
_mapController = controller;
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|