Created
November 23, 2016 00:33
-
-
Save ZahidRasheed/c18d4d60866f0dc4f1bcabfdeeae770e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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