2024-05-30 01:10:53 +02:00
|
|
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
2024-05-24 00:30:08 +02:00
|
|
|
import '../constants.dart';
|
2024-05-27 14:40:46 +02:00
|
|
|
import '../utils/math.dart';
|
2024-05-09 21:55:17 +02:00
|
|
|
|
2024-05-09 13:31:00 +02:00
|
|
|
class MyLocation {
|
|
|
|
final String street;
|
|
|
|
final String country;
|
|
|
|
|
|
|
|
/// DE: Bundesland
|
|
|
|
final String? administrativeArea;
|
2024-06-09 00:04:13 +02:00
|
|
|
|
2024-05-09 13:31:00 +02:00
|
|
|
/// City
|
|
|
|
final String locality;
|
2024-06-09 00:04:13 +02:00
|
|
|
|
2024-05-09 13:31:00 +02:00
|
|
|
/// DE: Stadtteil
|
|
|
|
final String? subLocality;
|
|
|
|
final String? postalCode;
|
|
|
|
final double? latitude;
|
|
|
|
final double? longitude;
|
|
|
|
|
|
|
|
MyLocation({
|
|
|
|
required this.street,
|
|
|
|
required this.country,
|
|
|
|
required this.administrativeArea,
|
|
|
|
required this.locality,
|
|
|
|
required this.subLocality,
|
|
|
|
required this.postalCode,
|
|
|
|
required this.latitude,
|
|
|
|
required this.longitude,
|
|
|
|
});
|
|
|
|
|
2024-05-30 01:10:53 +02:00
|
|
|
factory MyLocation.fromDocument(DocumentSnapshot doc) {
|
|
|
|
Map<String, dynamic> data = doc.data() as Map<String, dynamic>;
|
|
|
|
return MyLocation(
|
2024-06-17 12:51:30 +02:00
|
|
|
street: data[Constants.dbFieldLocationStreet],
|
|
|
|
country: data[Constants.dbFieldLocationCountry],
|
2024-05-30 01:10:53 +02:00
|
|
|
administrativeArea: data[Constants.dbFieldLocationArea],
|
2024-06-17 12:51:30 +02:00
|
|
|
locality: data[Constants.dbFieldLocationLocality],
|
2024-05-30 01:10:53 +02:00
|
|
|
subLocality: data[Constants.dbFieldLocationSubLocality],
|
|
|
|
postalCode: data[Constants.dbFieldLocationPostalCode],
|
2024-06-17 12:51:30 +02:00
|
|
|
latitude: data[Constants.dbFieldLocationLatitude],
|
|
|
|
longitude: data[Constants.dbFieldLocationLongitude],
|
2024-05-30 01:10:53 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-05-09 13:31:00 +02:00
|
|
|
// convert to a map
|
|
|
|
Map<String, dynamic> toMap() {
|
|
|
|
return {
|
2024-05-24 00:30:08 +02:00
|
|
|
Constants.dbFieldLocationStreet: street,
|
|
|
|
Constants.dbFieldLocationCountry: country,
|
|
|
|
Constants.dbFieldLocationArea: administrativeArea,
|
|
|
|
Constants.dbFieldLocationLocality: locality,
|
|
|
|
Constants.dbFieldLocationSubLocality: subLocality,
|
|
|
|
Constants.dbFieldLocationPostalCode: postalCode,
|
|
|
|
Constants.dbFieldLocationLatitude: latitude,
|
|
|
|
Constants.dbFieldLocationLongitude: longitude,
|
2024-05-09 13:31:00 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-05-09 21:55:17 +02:00
|
|
|
/// Returns: locality, country. In case of an error: latitude, longitude.
|
|
|
|
String toStringDegree() {
|
|
|
|
try {
|
2024-05-27 14:40:46 +02:00
|
|
|
String latResult = convertDecimalToDMS(latitude!, isLatitude: true);
|
|
|
|
String longResult = convertDecimalToDMS(longitude!, isLatitude: false);
|
2024-05-09 21:55:17 +02:00
|
|
|
return '$latResult, $longResult';
|
|
|
|
} catch (e) {
|
|
|
|
// on error return origin values
|
|
|
|
return '$latitude, $longitude';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-09 00:04:13 +02:00
|
|
|
/// Returns the location formatted as 'locality, country'
|
|
|
|
/// if both values are present or an empty string if both values are empty.
|
2024-05-24 00:30:08 +02:00
|
|
|
@override
|
2024-05-09 21:55:17 +02:00
|
|
|
String toString() {
|
2024-06-09 00:04:13 +02:00
|
|
|
if (locality.isNotEmpty && country.isNotEmpty) {
|
|
|
|
return '$locality, $country';
|
|
|
|
} else if (country.isNotEmpty) {
|
|
|
|
return country;
|
|
|
|
} else if (locality.isNotEmpty) {
|
|
|
|
return locality;
|
|
|
|
}
|
|
|
|
return '';
|
2024-05-09 21:55:17 +02:00
|
|
|
}
|
|
|
|
|
2024-05-09 13:31:00 +02:00
|
|
|
@override
|
|
|
|
int get hashCode => Object.hash(latitude, longitude);
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool operator ==(Object other) =>
|
|
|
|
other is MyLocation &&
|
|
|
|
latitude == other.latitude &&
|
|
|
|
longitude == other.longitude;
|
|
|
|
}
|