计算一个位置到许多其他位置之间的接近度

时间:2022-11-15 20:01:20

I have a database with a list of latitude and longitude

我有一个包含纬度和经度列表的数据库

-DeviceName
-Latitude
-Longitude

Given my current device's latitude and longitude, I want to get all devices within the database list in distance/proximity of X kilometres.

鉴于我当前设备的纬度和经度,我想让数据库列表中的所有设备距离/接近X公里。

How do I calculate the proximity of my location vs other locations?

如何计算我的位置与其他位置的距离?

2 个解决方案

#1


0  

I think you want to have a peep at this amazing presentation. it will tell you how to use (and for bonus points explains!) the haversine formula to calcuate distances on the surface of the earth accounting for curviture and how to avoid some common mistakes in your database queries etc. His dataset is pretty much exactly what yours is - item, longitude and latitude.

我想你想看看这个惊人的演示。它将告诉你如何使用(以及奖励积分解释!)的半径公式来计算地球表面的距离,考虑到曲率以及如何避免数据库查询中的一些常见错误等。他的数据集几乎就是什么你的是 - 项目,经度和纬度。

#2


0  

If you're after the raw code, this should help you:

如果您正在使用原始代码,这可以帮助您:

private static Double rad2deg(Double rad) {
    return (rad / Math.PI * 180.0);
}

private static Double deg2rad(Double deg) {
    return (deg * Math.PI / 180.0);
}

private const Double kEarthRadiusKms = 6376.5;

private static Double CalculateDistance(Double latitude1, Double longitude1, Double latitude2, Double longitude2) {
    double theta = longitude1 - longitude2;
    double dist = Math.Sin(deg2rad(latitude1)) * Math.Sin(deg2rad(latitude2)) + Math.Cos(deg2rad(latitude1)) * Math.Cos(deg2rad(latitude2)) * Math.Cos(deg2rad(theta));
    dist = Math.Acos(dist);
    dist = rad2deg(dist);
    dist = dist * 60 * 1.1515;
    dist = dist * 1.609344;
    return (dist);
}

#1


0  

I think you want to have a peep at this amazing presentation. it will tell you how to use (and for bonus points explains!) the haversine formula to calcuate distances on the surface of the earth accounting for curviture and how to avoid some common mistakes in your database queries etc. His dataset is pretty much exactly what yours is - item, longitude and latitude.

我想你想看看这个惊人的演示。它将告诉你如何使用(以及奖励积分解释!)的半径公式来计算地球表面的距离,考虑到曲率以及如何避免数据库查询中的一些常见错误等。他的数据集几乎就是什么你的是 - 项目,经度和纬度。

#2


0  

If you're after the raw code, this should help you:

如果您正在使用原始代码,这可以帮助您:

private static Double rad2deg(Double rad) {
    return (rad / Math.PI * 180.0);
}

private static Double deg2rad(Double deg) {
    return (deg * Math.PI / 180.0);
}

private const Double kEarthRadiusKms = 6376.5;

private static Double CalculateDistance(Double latitude1, Double longitude1, Double latitude2, Double longitude2) {
    double theta = longitude1 - longitude2;
    double dist = Math.Sin(deg2rad(latitude1)) * Math.Sin(deg2rad(latitude2)) + Math.Cos(deg2rad(latitude1)) * Math.Cos(deg2rad(latitude2)) * Math.Cos(deg2rad(theta));
    dist = Math.Acos(dist);
    dist = rad2deg(dist);
    dist = dist * 60 * 1.1515;
    dist = dist * 1.609344;
    return (dist);
}