C# - LINQ - shortest distance by GPS latitude and longitude

前端 未结 3 1546
甜味超标
甜味超标 2021-02-14 16:38

I have database and in it I have class hotel with gps coordinates. I want to get closest places to coordinates which I choose.

I think It should look like this (I found

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-14 17:29

    I have had a reasonable about of success using this implementation of the Haversine Distance formula

    var startPoint = new { Latitude = 1.123, Longitude = 12.3 };
    
    var closest = entities.Something.OrderBy(x => 12742 * SqlFunctions.Asin(SqlFunctions.SquareRoot(SqlFunctions.Sin(((SqlFunctions.Pi() / 180) * (x.Latitude - startPoint.Latitude)) / 2) * SqlFunctions.Sin(((SqlFunctions.Pi() / 180) * (x.Latitude - startPoint.Latitude)) / 2) +
                                        SqlFunctions.Cos((SqlFunctions.Pi() / 180) * startPoint.Latitude) * SqlFunctions.Cos((SqlFunctions.Pi() / 180) * (x.Latitude)) *
                                        SqlFunctions.Sin(((SqlFunctions.Pi() / 180) * (x.Longitude - startPoint.Longitude)) / 2) * SqlFunctions.Sin(((SqlFunctions.Pi() / 180) * (x.Longitude - startPoint.Longitude)) / 2)))).Take(5);
    

提交回复
热议问题