98 lines
3.3 KiB
Dart
98 lines
3.3 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
|
import 'package:geolocator/geolocator.dart';
|
|
import 'package:http/http.dart' as http;
|
|
|
|
|
|
class LocationRepository {
|
|
|
|
static final LocationRepository _instance = LocationRepository._internal();
|
|
|
|
/// Singleton instance getter
|
|
factory LocationRepository() => _instance;
|
|
|
|
//Constructor???
|
|
LocationRepository._internal() {
|
|
//init for something
|
|
}
|
|
|
|
|
|
/// Überprüft, ob der Standortdienst aktiviert ist.
|
|
Future<bool> isLocationServiceEnabled() async {
|
|
return await Geolocator.isLocationServiceEnabled();
|
|
}
|
|
|
|
/// Fragt die Berechtigung für den Standort ab.
|
|
Future<LocationPermission> checkPermission() async {
|
|
return await Geolocator.checkPermission();
|
|
}
|
|
|
|
/// Fordert die Berechtigung für den Standort an.
|
|
Future<LocationPermission> requestPermission() async {
|
|
return await Geolocator.requestPermission();
|
|
}
|
|
|
|
/// Liefert die aktuelle Position des Geräts.
|
|
/// Wirft eine Exception, wenn der Dienst nicht aktiviert ist oder keine Berechtigung vorliegt.
|
|
Future<Position> getCurrentPosition() async {
|
|
bool serviceEnabled = await isLocationServiceEnabled();
|
|
if (!serviceEnabled) {
|
|
// Standortdienste sind nicht aktiviert.
|
|
return Future.error('Location services are disabled.');
|
|
}
|
|
|
|
LocationPermission permission = await checkPermission();
|
|
if (permission == LocationPermission.denied) {
|
|
permission = await requestPermission();
|
|
if (permission == LocationPermission.denied) {
|
|
// Berechtigungen sind verweigert.
|
|
return Future.error('Location permissions are denied');
|
|
}
|
|
}
|
|
|
|
if (permission == LocationPermission.deniedForever) {
|
|
// Berechtigungen sind dauerhaft verweigert.
|
|
return Future.error(
|
|
'Location permissions are permanently denied, we cannot request permissions.',
|
|
);
|
|
}
|
|
|
|
// Wenn alles in Ordnung ist, die Position zurückgeben.
|
|
return await Geolocator.getCurrentPosition();
|
|
}
|
|
|
|
Future<String> getNearbyLocation(Map map) async {
|
|
String locationOrt = '?';
|
|
var lat = map['lat'];
|
|
var lng = map['lng'];
|
|
// Hier kannst du die Logik hinzufügen, um den Standort zu verwenden, z.B.
|
|
String ptvGeoLink =
|
|
'https://api.myptv.com/geocoding/v1/locations/by-position/$lat/$lng?language=de&apiKey=${dotenv.get('PTV_GEOLINK_API_KEY')}';
|
|
final client = http.Client();
|
|
var response = await client.get(
|
|
Uri.parse(ptvGeoLink),
|
|
headers: {'Content-Type': 'application/json', 'charset': 'utf-8'},
|
|
);
|
|
//Response Data status
|
|
if (response.statusCode == 200) {
|
|
//Response is succsessful
|
|
Map<String, dynamic> data = json.decode(
|
|
utf8.decode(response.bodyBytes),
|
|
); //get response data
|
|
Map<String, dynamic> mapOfAddressfromPosition =
|
|
data['locations'][0]['address']; //get response address of position
|
|
if (mapOfAddressfromPosition.isNotEmpty) {
|
|
locationOrt =
|
|
'${mapOfAddressfromPosition['street'].toString()} ${mapOfAddressfromPosition['houseNumber'].toString()}, ${mapOfAddressfromPosition['postalCode'].toString()} ${mapOfAddressfromPosition['city'].toString()}';
|
|
}
|
|
} else {
|
|
debugPrint(response.statusCode.toString());
|
|
}
|
|
client.close();
|
|
|
|
return locationOrt;
|
|
}
|
|
}
|