mockLocation function

Map<String, double> mockLocation([
  1. double? latitude,
  2. double? longitude,
  3. int? radius
])

Generate random latitude longitude in radius from centerLat to centerLon, inclusive. If any argument is null, random location will be returned.

Implementation

Map<String, double> mockLocation(
    [double? latitude, double? longitude, int? radius]) {
  Map<String, double> location = Map();

  if (latitude == null || longitude == null || radius == null) {
    location['lat'] = random.nextDouble() * 90 * (random.nextBool() ? 1 : -1);
    location['lon'] = random.nextDouble() * 180 * (random.nextBool() ? 1 : -1);
  } else {
    // Convert radius from meters to degrees
    double radiusInDegrees = radius / 111000.0;

    double u = random.nextDouble();
    double v = random.nextDouble();
    double w = radiusInDegrees * sqrt(u);
    double t = 2 * pi * v;
    double x = w * cos(t);
    double y = w * sin(t);

    // Adjust the x-coordinate for the shrinking of the east-west distances
    location['lon'] = x / cos(_toRadians(latitude)) + longitude;
    location['lat'] = y + latitude;
  }

  return location;
}