0

我有一個應用程序,我想要做一些自定義指南針功能假設我有位置A和位置B我標記我的位置(位置A),然後我移動一些距離我的指南針應指向定位問題的地方,我可以移動我正在引用qiblacompass的功能,但它不是指向位置,當我移動到這裏其他位置是我的代碼如何獲取角度使用兩個經度和緯度

public static double getDirectionFromNorth(double degLatitude, 
       double degLongitude) { 


final double direction_LATITUDE = Math.toRadians(angle_lat); 
double direction_LONGITUDE = Math.toRadians(angle_lng); 

      double latitude2 = Math.toRadians(degLatitude); 
      double longitude = Math.toRadians(degLongitude); 

      double soorat = Math.sin(direction_LONGITUDE - longitude); 
      double makhraj = Math.cos(latitude2) * Math.tan(direction_LATITUDE) 
        - Math.sin(latitude2) * Math.cos(direction_LONGITUDE - longitude); 
      double returnValue = Math.toDegrees(Math.atan(soorat/makhraj)); 

      if (latitude2 > direction_LATITUDE) { 
       if ((longitude > direction_LONGITUDE || longitude < (Math 
         .toRadians(-180d) + direction_LONGITUDE)) 
         && (returnValue > 0 && returnValue <= 90)) { 

        returnValue += 180; 

       } else if (!(longitude > direction_LONGITUDE || longitude < (Math 
         .toRadians(-180d) + direction_LONGITUDE)) 
         && (returnValue > -90 && returnValue < 0)) { 

        returnValue += 180; 

       } 

      } 
      if (latitude2 < direction_LATITUDE) { 

       if ((longitude > direction_LONGITUDE || longitude < (Math 
         .toRadians(-180d) + direction_LONGITUDE)) 
         && (returnValue > 0 && returnValue < 90)) { 

        returnValue += 180; 

       } 
       if (!(longitude > direction_LONGITUDE || longitude < (Math 
         .toRadians(-180d) + direction_LONGITUDE)) 
         && (returnValue > -90 && returnValue <= 0)) { 

        returnValue += 180; 
       } 

      } 
      // *** 
      return returnValue; 
     } 

其中angle_lat和angle_lng是我的位置的緯度,經度和degLitudeitude是那些當我移動它從我的GPS獲取lat,lng

任何人請告訴我wha t在這段代碼中是錯誤的還是我用其他方法來完成我的任務?

+0

這可以幫助你https://software.intel.com/zh-cn/blogs/2012/11/30/calculating-a-bearing-between-points位置感知應用程序 – 2014-12-02 10:51:04

+0

如果您希望您的指南針指向位置A,那麼位置B與此有何關係? – greenapps 2014-12-02 10:51:16

+0

locationB是當我移動然後它得到經緯度 – hena12 2014-12-02 11:09:43

回答

0

使用此網頁來檢查你的代碼: http://www.movable-type.co.uk/scripts/latlong.html

然後,當你知道你正確地計算兩者之間的方向緯度,多頭 你就會意識到,這是不夠的速度行走。

只要駕駛一輛汽車或一列火車,遠高於10公里/小時,您的算法probabyl將工作。 要計算行人步行方向,需要更先進的算法,考慮過去的更多位置,例如30米。

但作爲第一槍,爲什麼不簡單地採取Location屬性提供的方向: 它被稱爲標題或課程。考慮到這一點,兩個經緯度之間的方向更加精確。 (因爲GPS芯片內部,有更多信息可用)