C# 如何在谷歌地图中根据给定的距离和坐标找到其他位置
我在谷歌地图上有很多位置。在数据库SQL Server中,我存储了它们的坐标。模型是这样的:C# 如何在谷歌地图中根据给定的距离和坐标找到其他位置,c#,linq,google-maps,google-maps-api-3,C#,Linq,Google Maps,Google Maps Api 3,我在谷歌地图上有很多位置。在数据库SQL Server中,我存储了它们的坐标。模型是这样的: public class Marker { public int Id { get; set; } public string Lat { get; set; } public string Long { get; set; } } 1坐标、经度和1半径(即100米)将提供给我,我应该从位置列表中找到该区域半径内的位置 如何通过坐标上的半径计算距离?我认为您需要对给定点的每个点使用哈
public class Marker
{
public int Id { get; set; }
public string Lat { get; set; }
public string Long { get; set; }
}
1坐标、经度和1半径(即100米)将提供给我,我应该从位置列表中找到该区域半径内的位置
如何通过坐标上的半径计算距离?我认为您需要对给定点的每个点使用哈弗公式,并将结果与半径进行比较。以下SQL查询用于计算坐标和表中坐标之间的距离 d=acos sinφ1.sinφ2+cosφ1.cosφ2.cosΔλ.R 查询使用 为MS SQL&C使用准备好的语句 试一试 因为我没有MS SQL&C,所以无法进行测试
PS使用3956英里6367公里我不确定您要的是什么。你想看看你的标记是否在另一个位置的给定距离内吗?是的,我需要计算给定坐标和其他坐标之间的距离,如果距离小于给定半径,则取该标记,过滤标记列表。为了解决这个问题,我需要用c中的meter来计算位置之间的距离。谢谢,我现在研究它好了,现在我正在尝试这个链接:然后我将比较结果。
"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"
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();
}
}