Skip to content

Instantly share code, notes, and snippets.

@ZahidRasheed
Created November 23, 2016 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZahidRasheed/c18d4d60866f0dc4f1bcabfdeeae770e to your computer and use it in GitHub Desktop.
Save ZahidRasheed/c18d4d60866f0dc4f1bcabfdeeae770e to your computer and use it in GitHub Desktop.
public final class TravelToolsUtils {
/**
* Generate mock stop locations, given current location of user.
*/
public static CoordinatesEntity getCoordinatesEntity(double x0, double y0, double radius) {
Random random = new Random();
// Convert radius from meters to degrees
double w = (radius / 111000f) * Math.sqrt(random.nextDouble());
double t = 2 * Math.PI * random.nextDouble();
double x = w * Math.cos(t);
double y = w * Math.sin(t);
// Adjust the x-coordinate for the shrinking of the east-west distances
double new_x = x / Math.cos(y0);
double foundLongitude = new_x + x0;
double foundLatitude = y + y0;
return CoordinatesEntity.create(String.valueOf(foundLatitude),
String.valueOf(foundLongitude));
}
/**
* Given LatLngBounds, get the radius.
*/
public static double getBoundsRadius(@NonNull LatLngBounds bounds) {
LatLng center = bounds.getCenter();
LatLng ne = bounds.northeast;
double degreesToRadians = 57.2958;
double ne_lat = ne.latitude / degreesToRadians;
double ne_lng = ne.longitude / degreesToRadians;
double c_lat = center.latitude / degreesToRadians;
double c_lng = center.longitude / degreesToRadians;
// distance = circle radius from center to Northeast corner of bounds
double r_km = 6378.8 * Math.acos(
Math.sin(c_lat) * Math.sin(ne_lat) +
Math.cos(c_lat) * Math.cos(ne_lat) * Math.cos(ne_lng - c_lng));
return r_km * 1000;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment