mirror of
https://github.com/PranshulGG/WeatherMaster.git
synced 2026-10-22 19:13:39 -05:00
54 lines
1.4 KiB
Dart
54 lines
1.4 KiB
Dart
import 'package:geolocator/geolocator.dart';
|
|
import 'package:geocoding/geocoding.dart';
|
|
|
|
Future<Position> getCurrentPosition() async {
|
|
bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
|
|
if (!serviceEnabled) {
|
|
throw Exception("Location services are disabled.");
|
|
}
|
|
|
|
LocationPermission permission = await Geolocator.checkPermission();
|
|
if (permission == LocationPermission.denied) {
|
|
permission = await Geolocator.requestPermission();
|
|
if (permission == LocationPermission.denied) {
|
|
throw Exception("Location permission denied.");
|
|
}
|
|
}
|
|
|
|
if (permission == LocationPermission.deniedForever) {
|
|
throw Exception("Location permission permanently denied.");
|
|
}
|
|
|
|
return await Geolocator.getCurrentPosition(
|
|
desiredAccuracy: LocationAccuracy.high);
|
|
}
|
|
|
|
|
|
|
|
Future<Map<String, String>> reverseGeocode(double latitude, double longitude) async {
|
|
try {
|
|
List<Placemark> placemarks = await placemarkFromCoordinates(latitude, longitude);
|
|
|
|
if (placemarks.isNotEmpty) {
|
|
final Placemark place = placemarks.first;
|
|
|
|
return {
|
|
'city': place.locality ?? place.subAdministrativeArea ?? '',
|
|
'country': place.country ?? '',
|
|
};
|
|
} else {
|
|
return {
|
|
'city': '',
|
|
'country': '',
|
|
};
|
|
}
|
|
} catch (e) {
|
|
print('Reverse geocoding failed: $e');
|
|
return {
|
|
'city': '',
|
|
'country': '',
|
|
};
|
|
}
|
|
}
|
|
|