如何在谷歌地图中通过给定的距离和坐标找到其他位置

本文关键字:坐标 距离 位置 其他 谷歌地图 | 更新日期: 2023-09-27 18:30:12

我在谷歌地图中有很多位置。在数据库(SQL Server)中,我存储了它们的坐标。型号如下:

public class Marker
{
   public int Id { get; set; }
   public string Lat { get; set; }
   public string Long { get; set; }
}

1 coordinate(latitude and longitude)1 radius (i.e 100 meter)会给我,我应该从位置列表中找到半径内这个区域的位置。

如何通过坐标上的半径计算距离?

如何在谷歌地图中通过给定的距离和坐标找到其他位置

我认为您需要对给定点的每个点使用Haversine公式,并将结果与半径进行比较。

以下SQL查询使用余弦球面定律来计算坐标与表中坐标之间的距离。

d=acos(sin(φ1).sin(φ2)+cos(φ1

查询使用SQL数学函数

"SELECT Id,Lat,Long,(6367 * acos( cos( radians(center_lat) )
  * cos( radians( Lat ) ) * cos( radians( Long ) - radians(center_lng) )
  + sin( radians(center_lat) ) * sin( radians( Lat ) ) ) ) AS distance FROM table 
  HAVING distance < radius ORDER BY distance ASC LIMIT 0 , 20"

使用MS SQL的prepared语句&C#。

尝试

private static void SqlCommandPrepareEx(string connectionString)
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        SqlCommand command = new SqlCommand(null, connection);
        // Create and prepare an SQL statement.
        command.CommandText =
        "SELECT  Id, Lat, Long, (6367 * acos( cos( radians(@center_lat) ) * cos( radians( Lat ) ) * cos( radians( Long ) - radians(@center_lng) ) + sin( radians(@center_lat) ) * sin( radians( Lat ) ) ) ) AS distance FROM table HAVING distance < @radius ORDER BY distance ASC LIMIT 0 , 20");
        SqlParameter center_lat = new SqlParameter("@center_lat", SqlDbType.Decimal);
        center_lat.Precision = 10;
        center_lat.Scale = 6;
        center_lat.Value = YOURVALUEHERE;//latitude of centre
        command.Parameters.Add(center_lat);
        SqlParameter center_lng = new SqlParameter("@center_lng", SqlDbType.Decimal);
        center_lng.Precision = 10;
        center_lng.Scale = 6;
        center_lng.Value = YOURVALUEHERE;//longitude of centre
        command.Parameters.Add(center_lng);
        SqlParameter radius = new SqlParameter("@radius", SqlDbType.Int,3);
        radius.Value = YOURVALUEHERE;
        command.Parameters.Add(radius);//Radius in km
        // Call Prepare after setting the Commandtext and Parameters.
        command.Prepare();
        command.ExecuteNonQuery();

    }
}

由于我没有MS SQL&C#我无法测试

PS使用3956英里6367公里