Coords.fromMap constructor

Coords.fromMap(
  1. JsonMap map, {
  2. bool strict = true,
})

Creates Coords from a map.

Throws InvalidCoordsException if latitude or longitude are missing and strict is true (default). If strict is false, defaults to 0.0.

Implementation

factory Coords.fromMap(JsonMap map, {bool strict = true}) {
  final lat = map['latitude'];
  final lng = map['longitude'];

  if (strict && (lat == null || lng == null)) {
    throw InvalidCoordsException(
        'Missing required coordinates: latitude=${lat != null}, longitude=${lng != null}');
  }

  final latitude = (lat as num?)?.toDouble() ?? 0.0;
  final longitude = (lng as num?)?.toDouble() ?? 0.0;

  // Validate ranges
  if (latitude < -90 || latitude > 90) {
    throw InvalidCoordsException(
        'Latitude must be between -90 and 90, got: $latitude');
  }
  if (longitude < -180 || longitude > 180) {
    throw InvalidCoordsException(
        'Longitude must be between -180 and 180, got: $longitude');
  }

  return Coords(
    latitude: latitude,
    longitude: longitude,
    accuracy: (map['accuracy'] as num?)?.toDouble() ?? 0.0,
    speed: (map['speed'] as num?)?.toDouble(),
    heading: (map['heading'] as num?)?.toDouble(),
    altitude: (map['altitude'] as num?)?.toDouble(),
  );
}