簡體   English   中英

如何在Google地圖中按給定距離和坐標查找其他位置

[英]How to find other locations by given distance and coordinate in Google Maps

我在Google地圖中有很多位置。 在數據庫(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 (ie 100 meter) ,我應該從位置列表中找到半徑范圍內的這個區域。

如何計算坐標上半徑的距離?

我認為你需要從給定點使用Haversine公式foreach點並將結果與​​radius進行比較。

以下SQL查詢使用球面余弦定律來計算坐標和表中坐標之間的距離。

d = acos(sin(φ1).sin(φ2)+ cos(φ1).cos(φ2).cos(Δλ))。R

該查詢使用SQL Math函數

"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和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#我無法測試

對於kms,PS使用3956英里6367英里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM