public
Created

  • Download Gist
gistfile1.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
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));
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.