cofounderella/lib/models/location.dart

85 lines
2.5 KiB
Dart
Raw Normal View History

import 'package:cloud_firestore/cloud_firestore.dart';
2024-05-24 00:30:08 +02:00
import '../constants.dart';
import '../utils/math.dart';
2024-05-09 21:55:17 +02:00
class MyLocation {
final String street;
final String country;
/// DE: Bundesland
final String? administrativeArea;
/// City
final String locality;
/// 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,
});
factory MyLocation.fromDocument(DocumentSnapshot doc) {
Map<String, dynamic> data = doc.data() as Map<String, dynamic>;
return MyLocation(
street: data[Constants.dbFieldLocationStreet] ?? '',
country: data[Constants.dbFieldLocationCountry] ?? '',
administrativeArea: data[Constants.dbFieldLocationArea],
locality: data[Constants.dbFieldLocationLocality] ?? '',
subLocality: data[Constants.dbFieldLocationSubLocality],
postalCode: data[Constants.dbFieldLocationPostalCode],
latitude: (data[Constants.dbFieldLocationLatitude] as num?)?.toDouble(),
longitude: (data[Constants.dbFieldLocationLongitude] as num?)?.toDouble(),
);
}
// 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 21:55:17 +02:00
/// Returns: locality, country. In case of an error: latitude, longitude.
String toStringDegree() {
try {
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';
}
}
/// Returns: locality, country
2024-05-24 00:30:08 +02:00
@override
2024-05-09 21:55:17 +02:00
String toString() {
return '$locality, $country';
}
@override
int get hashCode => Object.hash(latitude, longitude);
@override
bool operator ==(Object other) =>
other is MyLocation &&
latitude == other.latitude &&
longitude == other.longitude;
}