我試圖找到目的地點,給定起點lat/long,距離爲&。下面這個網站的計算器給了我想要的結果。計算終點給定距離,方位,起點
http://www.movable-type.co.uk/scripts/latlong.html
當我試圖通過代碼來實現一樣的,我沒有得到正確的結果。
下面是我的代碼 -
private GLatLng pointRadialDistance(double lat1, double lon1,
double radianBearing, double radialDistance)
{
double rEarth = 6371.01;
lat1 = DegreeToRadian(lat1);
lon1 = DegreeToRadian(lon1);
radianBearing = DegreeToRadian(radianBearing);
radialDistance = radialDistance/rEarth;
double lat = Math.Asin(Math.Sin(lat1) * Math.Cos(radialDistance) + Math.Cos(lat1)
* Math.Sin(radialDistance) * Math.Cos(radianBearing));
double lon;
if (Math.Cos(lat) == 0)
{ // Endpoint a pole
lon = lon1;
}
else
{
lon = ((lon1 - Math.Asin(Math.Sin(radianBearing) * Math.Sin(radialDistance)/Math.Cos(lat))
+ Math.PI) % (2 * Math.PI)) - Math.PI;
}
lat = RadianToDegree(lat);
lon = RadianToDegree(lon);
GLatLng newLatLng = new GLatLng(lat, lon);
return newLatLng;
}
public double Bearing(double lat1, double long1, double lat2, double long2)
{
//Convert input values to radians
lat1 = DegreeToRadian(lat1);
long1 = DegreeToRadian(long1);
lat2 = DegreeToRadian(lat2);
long2 = DegreeToRadian(long2);
double deltaLong = long2 - long1;
double y = Math.Sin(deltaLong) * Math.Cos(lat2);
double x = Math.Cos(lat1) * Math.Sin(lat2) -
Math.Sin(lat1) * Math.Cos(lat2) * Math.Cos(deltaLong);
double bearing = Math.Atan2(y, x);
return bearing;
}
public double DegreeToRadian(double angle)
{
return Math.PI * angle/180.0;
}
public double RadianToDegree(double angle)
{
return 180.0 * angle/Math.PI;
}
在主程序中,調用子程序如下 -
double bearing = Bearing(-41.294444, 174.814444, -40.90521, 175.6604);
GLatLng endLatLng = pointRadialDistance(-41.294444, 174.814444, bearing, 80);
我得到以下結果 -
Bearing=1.02749621782165
endLatLng=-40.5751022737927,174.797458881699
答案我預計是-40.939722,175.646389
(從上面的網站鏈接)。
任何人都可以提示我在代碼中犯了什麼錯誤嗎?
首先,軸承返回的結果應轉換爲度數。 – 2011-06-02 04:03:32