Skip to content

Instantly share code, notes, and snippets.

@mlc
Created February 1, 2013 16:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlc/4692530 to your computer and use it in GitHub Desktop.
Save mlc/4692530 to your computer and use it in GitHub Desktop.
import com.google.android.gms.maps.model.LatLng;
import com.google.common.collect.Lists;
import java.util.List;
import static java.lang.Math.*;
public class CircleMaker {
private static final double EARTH_RADIUS = 6371000.0; /* meters */
private static final int CIRCLE_PRECISION = 48;
public static List<LatLng> circle(LatLng center, double radiusMeters) {
List<LatLng> points = Lists.newArrayListWithCapacity(CIRCLE_PRECISION);
final double lat1 = degToRad(center.latitude);
final double lon1 = degToRad(center.longitude);
final double r = radiusMeters / EARTH_RADIUS;
final double sinlat1 = sin(lat1);
final double coslat1 = cos(lat1);
final double sinr = sin(r);
final double cosr = cos(r);
for (int i = 0; i < CIRCLE_PRECISION; ++i) {
final double θ = -i * (2*PI / (double)CIRCLE_PRECISION);
final double lat2 = asin(sinlat1 * cosr + coslat1 * sinr * cos(θ));
final double lon2 = lon1 + atan2(sin(θ)*sinr*coslat1, cosr - sinlat1*sin(lat2));
points.add(radToDeg(lat2, lon2));
}
return points;
}
private static double degToRad(double degrees) {
return degrees * (PI / 180.0);
}
private static double radToDeg(double radians) {
return radians * (180.0 / PI);
}
private static LatLng radToDeg(double lat, double lng) {
return new LatLng(radToDeg(lat), radToDeg(lng));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment