2014-01-09 34 views
0

我開發了一個使用phonegap的混合應用程序。如何在手機中獲得準確的方向?

在phonegap API和本地代碼中使用指南針功能。

存在問題。

我的測試設備(Galaxy Note 1,GalaxyS3等...)不表示方向相同。

我PhoneGap的來源是...

document.addEventListener("deviceready", onDeviceReady, false); 
function onDeviceReady() { 
     startWatch(); 
    } 

function startWatch() { 

     // Update compass every 3 seconds 
     var options = { frequency: 1000 }; 

     watchID = navigator.compass.watchHeading(onSuccess, onError, options); 
    } 

function onSuccess(heading) { 
     var element = document.getElementById('heading'); 
     var deg = heading.magneticHeading; 
     element.innerHTML = 'Heading: ' + deg + '<br>';   
    } 

參考PhoneGap的API。

我的祖國源...

float[] mGravity; 
float[] mGeomagnetic; 

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); 
      azimut = (float)Math.toDegrees(orientation[0]); 

      //for (-180 to 180), (-10 to 350) 
      if(azimut<0) 
       azimut = azimut+360;       Log.d("azimut",""+azimut); 

      // orientation contains: azimut, pitch and roll 
      Matrix matrix=new Matrix(); 
      imgView.setScaleType(ScaleType.MATRIX); //required 
      matrix.postRotate(360-azimut+angle, imgView.getWidth()/2,  imgView.getHeight()/2); 
      imgView.setImageMatrix(matrix); 

     } 
    } 
} 

參考谷歌開發者。

爲什麼我的設備無法指示相同的方向?

+0

您是否在應用程序之外測試了設備上的指南針?指南針可能需要重新校準。 –

+0

當然。我被下載了其他指南針應用程序。 但這些都是一樣的情況。 如何重新校準我的設備? 我不知道。 – whdals0

回答

相關問題