Skip to content

Instantly share code, notes, and snippets.

@synw
Created March 22, 2020 03:25
Show Gist options
  • Save synw/847eb9051bb002d17c55ef0bec9a5cc0 to your computer and use it in GitHub Desktop.
Save synw/847eb9051bb002d17c55ef0bec9a5cc0 to your computer and use it in GitHub Desktop.
/// The main point class
class LatLng {
/// Main contructor, no validation
const LatLng(this.latitude, this.longitude);
/// Contructor with coordinates validation
LatLng.validate(this.latitude, this.longitude) {
if (latitude > 90.0) {
throw WrongCoordinateException("The latitude can not exceed 90.0");
}
if (latitude < -90.0) {
throw WrongCoordinateException(
"The latitude can not be lower than -90.0");
}
if (longitude > 180.0) {
throw WrongCoordinateException("The longitude can not exceed 180.0");
}
if (longitude < -180.0) {
throw WrongCoordinateException(
"The longitude can not be lower than -180.0");
}
}
/// The latitude
final double latitude;
/// The longitude
final double longitude;
@override
String toString() => "$latitude / $longitude";
@override
int get hashCode => latitude.hashCode + longitude.hashCode;
@override
bool operator ==(final Object other) =>
other is LatLng &&
latitude == other.latitude &&
longitude == other.longitude;
}
/// An exception for an error on a coordinate
class WrongCoordinateException implements Exception {
/// Provide a message
WrongCoordinateException(this.message);
/// The error message
final String message;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment