最近的GPS坐标的基础上的距离给定点 [英] Nearest GPS coordinate based on distance from a given point

查看:201
本文介绍了最近的GPS坐标的基础上的距离给定点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MySQL服务器数据库的GPS位置的列表。用户将进入应用程序中的GPS坐标,他应该得到的最接近的GPS坐标。

I have a list of GPS locations in a MySQL server database. The user will be entering a GPS coordinate in the application and he should get the nearest GPS coordinate.

我不介意的距离计算的依据是乌鸦的飞行或其他任何东西。它应该是足够快,以搜寻GPS位置十万。

I don't mind the distance calculation is based on "crow's flight" or anything else. It should be fast enough to search thousands of GPS locations.

我preFER的解决方案,否则我将努力让逻辑和应用自己。

I prefer solution in C#, else I will try to get the logic and apply myself.

推荐答案

有对MySQL的一个问题在<一个纬度/长距离搜索href="http://stackoverflow.com/questions/951923/need-help-optimizing-a-lat-lon-geo-search-for-mysql">Need帮助优化纬度/经度地理搜索MySQL的

There's one question on MySQL lat/long distance search in Need help optimizing a lat/Lon geo search for mysql

有关C#的距离计算,大多数网站使用半正矢公式 - 这里有一个C#实现 - <一个href="http://www.storm-consultancy.com/blog/development/$c$c-snippets/the-haversine-formula-in-c-and-sql/">http://www.storm-consultancy.com/blog/development/$c$c-snippets/the-haversine-formula-in-c-and-sql/ - 这也有一个SQL(MS SQL)实施过

For C# distance calculation, most sites use the Haversine formula - here's a C# implementation - http://www.storm-consultancy.com/blog/development/code-snippets/the-haversine-formula-in-c-and-sql/ - this also has a SQL (MS SQL) implementation too.

/// <summary>
/// Returns the distance in miles or kilometers of any two
/// latitude / longitude points.
/// </summary>
/// <param name="pos1">Location 1</param>
/// <param name="pos2">Location 2</param>
/// <param name="unit">Miles or Kilometers</param>
/// <returns>Distance in the requested unit</returns>
public double HaversineDistance(LatLng pos1, LatLng pos2, DistanceUnit unit)
{
    double R = (unit == DistanceUnit.Miles) ? 3960 : 6371;
    var lat = (pos2.Latitude - pos1.Latitude).ToRadians();
    var lng = (pos2.Longitude - pos1.Longitude).ToRadians();
    var h1 = Math.Sin(lat / 2) * Math.Sin(lat / 2) +
                  Math.Cos(pos1.Latitude.ToRadians()) * Math.Cos(pos2.Latitude.ToRadians()) *
                  Math.Sin(lng / 2) * Math.Sin(lng / 2);
    var h2 = 2 * Math.Asin(Math.Min(1, Math.Sqrt(h1)));
    return R * h2;
}

public enum DistanceUnit { Miles, Kilometers };

对于大多数查询......你可能确定拆分C#和SQL之间的工作

For most queries... you are probably OK splitting the work between C# and SQL

  • 使用MySQL来选择关闭纬度/经度点,例如:说得清纬度和液化天然气都在1.0的目标
  • 然后用C#来计算更精确的距离,并选择最佳。

如果您正在使用MS SQL 2008,然后我会建议使用MS SQL地理类型,因为这些具有内置的优化索引和计算功能 - 我看到的MySQL也有一些扩展 - <一个href="http://dev.mysql.com/tech-resources/articles/4.1/gis-with-mysql.html">http://dev.mysql.com/tech-resources/articles/4.1/gis-with-mysql.html - 但我已经对这些没有经验

If you were using MS SQL 2008 then I'd recommend using the MS SQL geography types as these have built-in optimised indexing and calculation features - I see that MySQL also has some extensions - http://dev.mysql.com/tech-resources/articles/4.1/gis-with-mysql.html - but I've no experience with these.

这篇关于最近的GPS坐标的基础上的距离给定点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆