2015-08-14 24 views
0

在我的Android應用程序中,我必須使用我當前的標題(使用加速度計和磁力計)和當前方位到targetLocationlocation.bearingTo(targetLocation))。爲什麼我的heading值在查看targetLocation時不同於bearingValue?

我已經知道使用加速度計和磁力計來計算當前航向從磁北向0°開始,當前軸承從地理北向開始。所以我想通了,我必須添加到headingValue,取決於我目前的位置,一個稱爲赤緯的值。

例如,我從google-maps中獲取某個GPS點,並在應用程序中添加此點作爲位置點。開始使用前,請在測量設備(如無限符號)之前將設備移動到空中,然後將設備放在我面前,並將其對準目標方向。所以我注意到標題!=軸承。任何人都可以向我解釋錯誤?假設我嘗試了50到3米之間的不同距離,並且我的設備已經正確校準。下面是源代碼的重要的方法:

@Override 
public void onSensorChanged(SensorEvent event) { 

     if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) 
      mGravity = event.values.clone(); 
     if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) 
      mGeomagnetic = event.values.clone(); 
     if (mGravity != null && mGeomagnetic != null) { 
      float R[] = new float[9]; 
      float I[] = new float[9]; 
      boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic); 
      if (success) { 
       float orientation[] = new float[3]; 
       SensorManager.getOrientation(R, orientation); 

       double tempAzimuth = Math.toDegrees(orientation[0]); // orientation contains: azimut, pitch and roll 

       if(tempAzimuth < 0){ 
        currentHeading = tempAzimuth + 360; 
       } else { 
        currentHeading = tempAzimuth; 
       } 
       TVheadingMag.setText(""+String.format("%.2f",currentHeading)+"°"); 
      } 
     } 

    } 

@Override 
public void onLocationChanged(Location location) { 

    //Declination http://stackoverflow.com/questions/4308262/calculate-compass-bearing-heading-to-location-in-android 
    geoField = new GeomagneticField(
      Double.valueOf(location.getLatitude()).floatValue(), 
      Double.valueOf(location.getLongitude()).floatValue(), 
      Double.valueOf(location.getAltitude()).floatValue(), 
      System.currentTimeMillis()); 

    if(location.bearingTo(this.target)<0){ 
     currentBearing = location.bearingTo(this.target)+360; 
    } else { 
     currentBearing = location.bearingTo(this.target); 
    } 

    headingWithDeclination = currentHeading; 
    headingWithDeclination += geoField.getDeclination(); 


    currentDistance = location.distanceTo(this.target); 

    TVheading.setText(""+String.format("%.2f", headingWithDeclination)+"°"); 
    TVheadingMag.setText(""+String.format("%.2f",currentHeading)+"°"); 

    TVbearing.setText(""+String.format("%.2f",currentBearing)+"°"); 
    TVgps.setText(""+ String.format("%.6f",location.getLatitude()) + " " + String.format("%.6f",location.getLongitude())); 

} 

UPDATE

圖片:https://pl.vc/1r6ap

橙色標記的位置是targetLocation。 這兩個位置都前往targetLocation。 你能同意,這些結果是安靜正確顯示?

在創作這張照片的過程中,我注意到這兩個白色標記都不等於我站在的位置。這似乎是壞的GPS數據是因爲這個問題的原因,不是嗎?

+0

「heading!= bearing」。您可以請這麼友好地發佈差異值嗎?這是一定程度的十度完全不同(10或更多?)? – AlexWien

回答

0

標題是你看的方向,例如坦克在哪個方向射擊,而方向是這輛車的方向。所以這應該回答爲什麼軸承不是前進。 他們有不同的名字和含義,他們不同,他們不能期望提供相同的價值。

更多細節

您可以將北(軸承=北),但看NE。 (標題)

  • 全球定位系統提供的軸承(或場(在地面上))時,車輛移動(altough部分API錯誤地稱之爲標題)的方向

  • 羅盤(=磁力計)開的方向你把設備=(標題)

當你計算定義的兩個位置之間的軸承,如緯度,經度座標,因爲你在做targetLocation (location.bearingTo(targetLocation)).那麼這是軸承!這不是標題!

既不指南針也不是加速度計將提供一個體面的標題值。 一些Android設備在他們的magnetomter非常錯誤的(我看見+ -20度相比+/- 2度我的iPhone中,一定要使用傳統的高品質的指南針作爲參考) 的iOS設備顯示+中的標題以及/ - 經過良好校準後的2個增量,(不僅在操作系統要求校準時,您必須在每次查看decice值之前進行校準)。移動
GPS時>10公里(H delives GOOT軸承結果,但不前進。

磁力可以校準,即使通過某種程度的關閉。 並且通常偏角大於誤差小。 赤緯幾乎沒什麼在歐洲,3個輩分極北(歐洲),只有少數地方有一個高赤緯> 6-7°(北阿拉斯加)

更新您在您的圖形進一步explantion:

你已經把兩點距離只有15米,而GPS不會比3-6m更敏捷。所以想象起點或終點的6m偏移量:a = 6m,b = 15的這樣一個三角形的角度爲atan2(6/15.0)= 21°。因此,只有位置不準確,纔會有21°的偏移。然而,仍然認爲在兩個地點之間通過視線指南針和方位的差異。

+0

Thx爲您的答案。我已經知道航向和方位的區別。更好的主題將是爲什麼我的headingValue如此不同於bearingValue而在targetLocation上查看? – ryoga86

+0

我告訴過你1)單單GPS的偏移就會導致至少20°的情況。使用更高的距離,看一個距離1km的目標,可以說是你能看到的最大的塔或山頂。 2)物鏡運行不正常,從recta,silva或suunto購買指南針。當你看目標時,看看它告訴了什麼,或者你使用另一個運行良好的應用程序,並看看他們顯示的標題。 – AlexWien

+0

進一步只標記你真實站立的地方,用Google googl earth測量方位角。對於這種措施,我們總是有很高的距離。 – AlexWien

相關問題