2015-07-13 114 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