我有一个用户模型,每个用户都在表中存储了纬度和经度。我想在给定的纬度和经度范围内获取30公里范围内的所有用户。使用表中的纬度和经度计算距离的任何插件。
id name latitude longitude
1 abc 43.56 56.34
2 xyz 46.34 57.87
3 mno 50.34 23.56
假设这是我的表值(它只是一个样本数据。)。我想从给定的高度(如(34.89,56.45))获取30km范围内的所有用户
有一个漂亮的Geokit宝石可以将这种方法添加到您的模型中:
Store.find(:all, :origin =>[37.792,-122.393], :within=>10)
甚至
Store.find(:all, :origin=>'100 Spear st, San Francisco, CA', :within=>10)
认为斯芬克斯可以根据与纬度和经度的距离进行搜索、排序和过滤:http://freelancing-god.github.com/ts/en/geosearching.html
使用Wicked发布的可移动类型脚本的帮助,我计算了2个点之间的距离(在具有lat、lng属性的活动记录中使用方法):
def calc_distance(origin)
radius = 6371
lat1 = to_rad(origin[0])
lat2 = to_rad(self.lat)
lon1 = to_rad(origin[1])
lon2 = to_rad(self.lng)
dLat = lat2-lat1
dLon = lon2-lon1
a = Math::sin(dLat/2) * Math::sin(dLat/2) +
Math::cos(lat1) * Math::cos(lat2) *
Math::sin(dLon/2) * Math::sin(dLon/2);
c = 2 * Math::atan2(Math::sqrt(a), Math::sqrt(1-a));
d = radius * c
end
def to_rad angle
angle * Math::PI / 180
end
Geokit非常健壮,但我只需要计算距离,加上Geokit需要非sqlite db
试试这个链接。对这种方法有详细的解释。
http://www.movable-type.co.uk/scripts/latlong.html
此外,我在网上找到了这个C#算法。我希望它能有所帮助。
class DistanceAlgorithm
{
const double PIx = 3.141592653589793;
const double RADIO = 6378.16;
/// <summary>
/// This class cannot be instantiated.
/// </summary>
private DistanceAlgorithm() { }
/// <summary>
/// Convert degrees to Radians
/// </summary>
/// <param name="x">Degrees</param>
/// <returns>The equivalent in radians</returns>
public static double Radians(double x)
{
return x * PIx / 180;
}
/// <summary>
/// Calculate the distance between two places.
/// </summary>
/// <param name="lon1"></param>
/// <param name="lat1"></param>
/// <param name="lon2"></param>
/// <param name="lat2"></param>
/// <returns></returns>
public static double DistanceBetweenPlaces(
double lon1,
double lat1,
double lon2,
double lat2)
{
double dlon = lon2 - lon1;
double dlat = lat2 - lat1;
double a = (Math.Sin(dlat / 2) * Math.Sin(dlat / 2)) + Math.Cos(lat1) * Math.Cos(lat2) * (Math.Sin(dlon / 2) * Math.Sin(dlon / 2));
double angle = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
return angle * RADIO;
}