74 lines
2.2 KiB
Dart
74 lines
2.2 KiB
Dart
import 'place_gate.dart';
|
|
|
|
class PlaceModel {
|
|
final String id;
|
|
final String name;
|
|
final String category;
|
|
final double latitude;
|
|
final double longitude;
|
|
final double elevationMeters; // GPS Altitude AMSL in meters (defaults to 0.0)
|
|
final double? distanceKm;
|
|
final String? address;
|
|
final List<PlaceGate> gates;
|
|
|
|
PlaceModel({
|
|
required this.id,
|
|
required this.name,
|
|
required this.category,
|
|
required this.latitude,
|
|
required this.longitude,
|
|
this.elevationMeters = 0.0,
|
|
this.distanceKm,
|
|
this.address,
|
|
this.gates = const [],
|
|
});
|
|
|
|
bool get hasGates => gates.isNotEmpty;
|
|
|
|
factory PlaceModel.fromJson(Map<String, dynamic> json) {
|
|
final rawElev = json['elevation_meters'] ?? json['elevation'] ?? json['altitude'] ?? 0.0;
|
|
double elev = 0.0;
|
|
if (rawElev is num) {
|
|
elev = rawElev.toDouble();
|
|
} else if (rawElev != null) {
|
|
elev = double.tryParse(rawElev.toString()) ?? 0.0;
|
|
}
|
|
|
|
final rawGates = json['gates'];
|
|
List<PlaceGate> parsedGates = [];
|
|
if (rawGates is List) {
|
|
parsedGates = rawGates
|
|
.map((g) => PlaceGate.fromJson(Map<String, dynamic>.from(g)))
|
|
.toList();
|
|
}
|
|
|
|
return PlaceModel(
|
|
id: json['id']?.toString() ?? '',
|
|
name: json['name']?.toString() ?? '',
|
|
category: json['category']?.toString() ?? 'other',
|
|
latitude: double.tryParse(json['latitude']?.toString() ?? json['lat']?.toString() ?? '0') ?? 0.0,
|
|
longitude: double.tryParse(json['longitude']?.toString() ?? json['lng']?.toString() ?? '0') ?? 0.0,
|
|
elevationMeters: elev,
|
|
distanceKm: json['distanceKm'] != null
|
|
? (json['distanceKm'] as num).toDouble()
|
|
: null,
|
|
address: json['address']?.toString() ?? json['neighborhood']?.toString(),
|
|
gates: parsedGates,
|
|
);
|
|
}
|
|
|
|
Map<String, dynamic> toJson() {
|
|
return {
|
|
'id': id,
|
|
'name': name,
|
|
'category': category,
|
|
'latitude': latitude,
|
|
'longitude': longitude,
|
|
'elevation_meters': elevationMeters,
|
|
'altitude': elevationMeters,
|
|
if (address != null) 'address': address,
|
|
if (gates.isNotEmpty) 'gates': gates.map((g) => g.toJson()).toList(),
|
|
};
|
|
}
|
|
}
|