【发布时间】:2015-04-24 20:58:04
【问题描述】:
如何使用纬度/经度坐标找到半径 (X) 英里内的所有值(在本例中为机构)?
代码:
public class GeoGen {
static final GeoPosition USER_POSITION = new GeoPosition(39.410868, -107.102182);
public static void main(String[] args) {
new Establishment("Fries Electronics", randomLocation(USER_POSITION, 40)).print();
new Establishment("Walmart Supercenter", randomLocation(USER_POSITION, 40)).print();
new Establishment("Target", randomLocation(USER_POSITION, 40)).print();
new Establishment("Krogers", randomLocation(USER_POSITION, 40)).print();
new Establishment("McDonalds", randomLocation(USER_POSITION, 40)).print();
}
public static GeoPosition randomLocation(GeoPosition location, double radius) {
Random random = new Random();
// Convert radius from miles to meters
double meters = radius * 1609.34;
// Convert radius from meters to degrees
double radiusInDegrees = meters / 111000f;
double u = random.nextDouble();
double v = random.nextDouble();
double w = radiusInDegrees * Math.sqrt(u);
double t = 2 * Math.PI * v;
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(location.latitude());
double foundLongitude = new_x + location.longitude();
double foundLatitude = y + location.latitude();
return new GeoPosition(foundLongitude, foundLatitude);
}
public static double distanceBetween(GeoPosition a, GeoPosition b) {
double longDif = a.longitude() - b.longitude();
double distance =
Math.sin(deg2rad(a.latitude()))
*
Math.sin(deg2rad(b.latitude()))
+
Math.cos(deg2rad(a.latitude()))
*
Math.cos(deg2rad(b.latitude()))
*
Math.cos(deg2rad(longDif));
distance = Math.acos(distance);
distance = rad2deg(distance);
distance = distance * 60 * 1.1515; // Convert to meters
distance = distance * 0.8684; // Convert to miles.
return distance;
}
private static double rad2deg(double rad) {
return (rad * 180.0 / Math.PI);
}
private static double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
}
/**
* A class representing an establishment in the world.
*
* @author Christian
*/
class Establishment {
public static Map<GeoPosition, String> establishments = new HashMap<>();
private final String name;
private final GeoPosition geoPosition;
public Establishment(String name, GeoPosition geoPosition) {
this.name = name;
this.geoPosition = geoPosition;
establishments.put(geoPosition, name);
}
public void print() {
System.out.print("Establishment("+name+") was created approx ");
System.out.printf("%.2f", GeoGen.distanceBetween(geoPosition, GeoGen.USER_POSITION));
System.out.print(" miles from specified Lat/Long \n");
}
public final String name() { return name; }
public final GeoPosition position() { return geoPosition; }
}
/**
* A class representing a geographical location using latitude/longitude.
*
* @author Christian
*/
class GeoPosition {
private final double longitude;
private final double latitude;
public GeoPosition(double longitude, double latitude) {
this.longitude = longitude;
this.latitude = latitude;
}
public double longitude() { return longitude; }
public double latitude() { return latitude; }
}
运行此应用程序每次都会产生不同的结果,因为它是随机的,但这里是使用 GeoGen#distanceBetween(GeoPosition, GeoPosition) 方法的示例。
Establishment(Fries Electronics) was created approx 19.26 miles from specified Lat/Long
Establishment(Walmart Supercenter) was created approx 9.79 miles from specified Lat/Long
Establishment(Target) was created approx 28.83 miles from specified Lat/Long
Establishment(Krogers) was created approx 10.61 miles from specified Lat/Long
Establishment(McDonalds) was created approx 3.37 miles from specified Lat/Long
但是,我想弄清楚的是如何让所有场所都在 X 英里范围内。比如这样的
GeoGen#FindEstablishmentsWithinRadius(GeoPosition, Double) returns List<Establishment>
这将返回指定 GeoPosition X 英里范围内的机构列表。
【问题讨论】: