Android device (GPS)

Using Location.getBearing(); I seem to accidentally change bearings.

Aka, I can slowly turn the device, and he does not notice, he just selects his random bearings.

I know that the device works, because the "You are here" icon in the "Maps" application on the tablet slowly rotates as the device rotates.

Is there any other right way to get a bearing? I am using GPS. Perhaps there is a better way to determine which direction you are facing.

+6
source share
3 answers

Try to get the bearing from the accelerometer sensor and magnetic field sensor (G-).

Here is the tutorial: http://android-coding.blogspot.co.at/2012/03/create-our-android-compass.html

+3
source

Location.getBearing() returns the bearing calculated for you by GPS satellites. This is not a real-time representation of the title of your device. The Google Maps app uses the device built into the G-sensors to get the direction you are in.

+3
source

Following herom's recommendations using the link: http://android-coding.blogspot.co.at/2012/03/create-our-android-compass.html

I expanded my class to implement the sensor: extends Activity implements SensorEventListener

And implemented as proposed, but changed it to take into account the orientation of the screen.

Here is the code I went with:

 @Override public void onSensorChanged(SensorEvent event) { switch(event.sensor.getType()){ case Sensor.TYPE_ACCELEROMETER: for(int i =0; i < 3; i++){ valuesAccelerometer[i] = event.values[i]; } break; case Sensor.TYPE_MAGNETIC_FIELD: for(int i =0; i < 3; i++){ valuesMagneticField[i] = event.values[i]; } break; } boolean success = SensorManager.getRotationMatrix( matrixR, matrixI, valuesAccelerometer, valuesMagneticField); if(success){ SensorManager.getOrientation(matrixR, matrixValues); double azimuth = Math.toDegrees(matrixValues[0]); //double pitch = Math.toDegrees(matrixValues[1]); //double roll = Math.toDegrees(matrixValues[2]); WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE); Display mDisplay = mWindowManager.getDefaultDisplay(); Float degToAdd = 0f; if(mDisplay.getRotation() == Surface.ROTATION_0) degToAdd = 0.0f; if(mDisplay.getRotation() == Surface.ROTATION_90) degToAdd = 90.0f; if(mDisplay.getRotation() == Surface.ROTATION_180) degToAdd = 180.0f; if(mDisplay.getRotation() == Surface.ROTATION_270) degToAdd = 270.0f; mapView.setFacingDirection((float) (azimuth + degToAdd)); //DEGREES NOT RADIANS } } 
0
source

Source: https://habr.com/ru/post/919680/


All Articles