LatLng.fromSexagesimal constructor

LatLng.fromSexagesimal(
  1. String str
)

Converts sexagesimal string into a lat/long value

final LatLng p1 = new LatLng.fromSexagesimal('''51° 31' 10.11" N, 19° 22' 32.00" W''');
print("${p1.latitude}, ${p1.longitude}");
// Shows:
51.519475, -19.37555556

Implementation

factory LatLng.fromSexagesimal(final String str) {
  double _latitude = 0.0;
  double _longitude = 0.0;
  // try format '''47° 09' 53.57" N, 8° 32' 09.04" E'''
  var splits = str.split(',');
  if (splits.length != 2) {
    // try format '''N 47°08'52.57" E 8°32'09.04"'''
    splits = str.split('E');
    if (splits.length != 2) {
      // try format '''N 47°08'52.57" W 8°32'09.04"'''
      splits = str.split('W');
      if (splits.length != 2) {
        throw 'Unsupported sexagesimal format: $str';
      }
    }
  }
  _latitude = sexagesimal2decimal(splits[0]);
  _longitude = sexagesimal2decimal(splits[1]);
  if (str.contains('S')) {
    _latitude = -_latitude;
  }
  if (str.contains('W')) {
    _longitude = -_longitude;
  }
  return LatLng(_latitude, _longitude);
}