Cordova 如何在phonegap中获得准确的方向?
我用phonegap开发了一个混合应用程序 在phonegap api和本机代码中使用compass函数 有一个问题 我的测试设备Galaxy Note 1、GalaxyS3等。。。它们不表示相同的方向 我的phonegap来源是Cordova 如何在phonegap中获得准确的方向?,cordova,compass-geolocation,heading,Cordova,Compass Geolocation,Heading,我用phonegap开发了一个混合应用程序 在phonegap api和本机代码中使用compass函数 有一个问题 我的测试设备Galaxy Note 1、GalaxyS3等。。。它们不表示相同的方向 我的phonegap来源是 document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { startWatch(); } function start
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>';
}
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);
}
}
}
参考phonegap api
我的原始资料是
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>';
}
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);
}
}
}
参考谷歌开发者
为什么我的设备不能指示相同的方向?考虑到你在评论中说这是你的指南针而不是你的代码,答案是重新校准你的指南针。请参阅链接说明:
您是否在应用程序之外的设备上测试了指南针?指南针可能需要重新校准。当然可以。我下载了其他指南针应用程序。但情况是一样的。如何重新校准我的设备?我不知道。