问题
Given the input, center latitude, center longitude and radius in kilometers, I want to get the coordinates for the rectangle that contains this circle (northeast and southwest lat/lng).
Should I write the method myself? Even though I'm afraid not to account for some things as my math is rusty. Or can I find a ready implementation for java? I have google maps sdk in my project but I couldn't find anything useful there.
回答1:
I suppose your square radius is much smaller than the earth's radius (6371 km) so that you can safely ignore the earth's curvature.
Then the math is quite easy:
// center of square
double latitudeCenter = ...; // in degrees
double longitudeCenter = ...; // in degrees
double radius = ...; // in km
double RADIUS_EARTH = 6371; // in km
// north-east corner of square
double latitudeNE = latitudeCenter + Math.toDegrees(radius / RADIUS_EARTH);
double longitudeNE = longitudeCenter + Math.toDegrees(radius / RADIUS_EARTH / Math.cos(Math.toRadians(latitudeCenter)));
// south-west corner of square
double latitudeSW = latitudeCenter - Math.toDegrees(radius / RADIUS_EARTH);
double longitudeSW = longitudeCenter - Math.toDegrees(radius / RADIUS_EARTH / Math.cos(Math.toRadians(latitudeCenter)));
Example:
Center(lat,lon) at 48.00,11.00 and radius 10 km
will give NE-corner(lat,lon) at 48.09,11.13 and SW-corner(lat,lon) at 47.91,10.87.
And here is how to do it with LatLng
and Bounds of the google-maps-services-java API:
public static final double RADIUS_EARTH = 6371;
public static Bounds boundsOfCircle(LatLng center, double radius) {
Bounds bounds = new Bounds();
double deltaLat = Math.toDegrees(radius / RADIUS_EARTH);
double deltaLng = Math.toDegrees(radius / RADIUS_EARTH / Math.cos(Math.toRadians(center.lat)));
bounds.northeast = new LatLng(center.lat + deltaLat, center.lng + deltaLng);
bounds.southwest = new LatLng(center.lat - deltaLat, center.lng - deltaLng);
return bounds;
}
来源:https://stackoverflow.com/questions/48440092/geo-circle-to-rectangle-coordinates