ElevationData.fromJson constructor
Construct Elevation data from JSON as per the schema in the api documentation.
Bad code because of inconsistency of Elevation data in OpenRouteService.
The json should have keys 'timestamp', 'attribution', 'version', 'elevation' and 'geometry' which corresponds to a Map further containing keys 'coordinates' and 'type'.
Implementation
factory ElevationData.fromJson(Map<String, dynamic> json) => ElevationData(
coordinates: json['geometry'] is Map<String, dynamic> &&
json['geometry']['coordinates'] is List<dynamic> &&
(json['geometry']['coordinates'] as List<dynamic>).first
is List<dynamic>
? (json['geometry']['coordinates'] as List<dynamic>)
.map<ORSCoordinate>(
(dynamic coordinate) => ORSCoordinate.fromList(coordinate),
)
.toList()
: <ORSCoordinate>[
ORSCoordinate(
latitude: json['geometry'] is List
? (json['geometry']?[1] as double)
: (json['geometry']?['coordinates']?[1]! as double),
longitude: json['geometry'] is List
? (json['geometry']?[0] as double)
: (json['geometry']?['coordinates']?[0]! as double),
altitude: json['geometry'] is List
? (json['geometry']?[2] as double)
: (json['geometry']?['coordinates']?[2]! as num)
.toDouble(),
),
],
timestamp: json['timestamp']! as int,
attribution: json['attribution'] as String,
type: (json['geometry'] is List)
? 'point'
: (json['geometry']?['type'] ?? 'point') as String,
version: json['version'] as String,
);