2015-07-13 83 views
-1

首先,我甚至不知道这是做的最好的方法,但是......测量距离和lattitude

我已经积累的经度和lattitude分表的所有拉链代码在美国。我想要做的是允许用户选择一个邮政编码,以英里数(5,10,20,40等等)选择一个半径,并且该应用将列出该半径内的所有用户。

它显然不需要非常准确,但它必须接近。我一直在寻找其他方法来做到这一点,但我很难过,我找不到一个使用long/lat做的好例子。

如果我能在C#中得到最好的效果。我不擅长Java,但如果绝对必要,我可能会混淆它。

编辑:

我的坐标是这样的:

CountryCode Zipcode Place StateCode Latitude Longitude 
US   95219 Stockton  CA  38.01 -121.3698 
US   95220 Acampo  CA  38.2004 -121.2186 
US   95227 Clements  CA  38.1929 -121.0811 
US   95230 Farmington CA  37.9945 -120.7926 
US   95231 French Camp CA  37.878 -121.2827 
US   95234 Holt   CA  37.9344 -121.4261 
US   95236 Linden  CA  38.032 -121.0493 

这个问题是不是一个重复的,链接的问题是一个电话。

+0

请注意,如果您可以将纬度/经度转换为东/北纬度,则这种做法微不足道。 –

+0

@oppassum - 这是一款手机,我认为它内置了GeoCoordinates功能? –

+0

也许我的理解不正确,但是您的问题是找到,比方说,与所选位置相关的所有坐标都以某个半径表示? – nelek

回答

2

以下代码会生成此Wgs84Point实例与其他实例之间的距离。假设球形地球完美,并且不考虑地球的不规则形状,距离就是给定的。

public class Wgs84Point 
{ 
    const double MaxDegreesLongitude = 180; 
    const double MinDegreesLongitude = -180; 
    const double MaxDegreesLatitude = 90; 
    const double MinDegreesLatitude = -90; 

    readonly double _longitude; 
    readonly double _latitude; 

    public double Latitude { get { return _latitude; } } 

    public double Longitude { get { return _longitude; } } 

    public Wgs84Point(double longitude, double latitude) 
    { 
     if (longitude > MaxDegreesLongitude || longitude < MinDegreesLongitude) 
      throw new ArgumentException("longitude"); 

     if (latitude > MaxDegreesLatitude || latitude < MinDegreesLatitude) 
      throw new ArgumentException("latitude"); 

     _longitude = longitude; 
     _latitude = latitude; 
    } 

    public Distance DistanceTo(Wgs84Point that) 
    { 
     if (that == null) 
      throw new ArgumentNullException("that"); 

     if (this == that) 
      return Distance.Zero; 

     var dLat = DegreesToRadians(Latitude - that.Latitude); 
     var dLon = DegreesToRadians(Longitude - that.Longitude); 
     var a = Math.Sin(dLat/2) * Math.Sin(dLat/2) + 
      Math.Cos(DegreesToRadians(Latitude)) * 
      Math.Cos(DegreesToRadians(that.Latitude)) * 
      Math.Sin(dLon/2) * Math.Sin(dLon/2); 
     var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a)); 
     var d = Distance.RadiusOfEarth.ToDouble() * c; 
     return new Distance(d); 
    } 

    static double DegreesToRadians(double degrees) 
    { 
     return degrees * (Math.PI/180); 
    } 
} 
+0

这是什么语法?假设,在我的表格中,我添加到了上面的编辑中,我想查找从Stockton到Holt的距离。我会用什么语法? –

+0

表?你问题的[原始版本]没有提到持久性机制。您将不得不使用EF,DataReader或DataSet构造C#类,并使用上述代码。或者,查找上述代码的SQL实现。 – Matt