import 'package:cloud_firestore/cloud_firestore.dart'; import '../constants.dart'; import '../utils/math.dart'; 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 data = doc.data() as Map; 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], longitude: data[Constants.dbFieldLocationLongitude], ); } // convert to a map Map toMap() { return { Constants.dbFieldLocationStreet: street, Constants.dbFieldLocationCountry: country, Constants.dbFieldLocationArea: administrativeArea, Constants.dbFieldLocationLocality: locality, Constants.dbFieldLocationSubLocality: subLocality, Constants.dbFieldLocationPostalCode: postalCode, Constants.dbFieldLocationLatitude: latitude, Constants.dbFieldLocationLongitude: longitude, }; } /// 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); return '$latResult, $longResult'; } catch (e) { // on error return origin values return '$latitude, $longitude'; } } /// Returns the location formatted as 'locality, country' /// if both values are present or an empty string if both values are empty. @override String toString() { if (locality.isNotEmpty && country.isNotEmpty) { return '$locality, $country'; } else if (country.isNotEmpty) { return country; } else if (locality.isNotEmpty) { return locality; } return ''; } @override int get hashCode => Object.hash(latitude, longitude); @override bool operator ==(Object other) => other is MyLocation && latitude == other.latitude && longitude == other.longitude; }