如何使用纬度/经度坐标在半径(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英里内的机构列表。
答案 0 :(得分:3)
你已经有了计算距离的方法吗?只需循环通过场所,检查它们是否在半径范围内。
将所有机构添加到列表中,然后您可以使用流,例如:
public List<Establishment> findEstablishmentsWithinRadius(List<Establishment> list, GeoPosition position, double radius) {
return list.stream().filter(e -> distanceBetween(position, e.position()) <= radius).collect(Collectors.toList());
}
答案 1 :(得分:1)
嗯,对我来说似乎相当微不足道。假设您有一个已创建的所有场所的列表,并且您已经拥有距离功能。剩下的只是一个函数,给定一个中心和半径,迭代所有机构,计算每个机构之间的距离,并收集那些低于半径的结果。完成。我想这回答了你的问题。
现在,如果你正在看性能并且这个方法不能说服你(比如,你有太多的机构来对它们进行线性传递),你可能想要先使用一些近似值并预先过滤合理的集合,然后选择是否足够,或者您仍然想要计算此子集内的距离。为此,您可能需要阅读Geohashing,Quad-Trees,R-Trees和/或KD-trees ...或其中的一些变体: - )