Last active
May 9, 2017 07:15
-
-
Save talhahasanzia/b93858872b9e8e4965aec6458d9dc82c to your computer and use it in GitHub Desktop.
Java direction to specific place compass.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public void onSensorChanged( SensorEvent event ) | |
{ | |
if ( currentLocation != null ) | |
{ | |
// get accelerometer data | |
if ( event.sensor.getType() == Sensor.TYPE_ACCELEROMETER ) | |
{ | |
// we need to use a low pass filter to make data smoothed | |
// smoothed = LowPassFilter.filter(event.values, gravity); | |
gravity[ 0 ] = event.values[ 0 ]; | |
gravity[ 1 ] = event.values[ 1 ]; | |
gravity[ 2 ] = event.values[ 2 ]; | |
String dir = "" + Math.round( new GreatCircleBearing().calculateBearing( currentLocation.getLatitude(), currentLocation.getLongitude(), Fixed_Coordinates_Latitude, Fixed_Coordinates_Longitude ) ); | |
String data = "The direction of Specific place is " + dir + "° from North."; | |
mDirection.setText( data ); | |
} | |
else if ( event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD ) | |
{ | |
// smoothed = LowPassFilter.filter(event.values, geomagnetic); | |
geomagnetic[ 0 ] = event.values[ 0 ]; | |
geomagnetic[ 1 ] = event.values[ 1 ]; | |
geomagnetic[ 2 ] = event.values[ 2 ]; | |
String dir = "" + Math.round( new GreatCircleBearing().calculateBearing( currentLocation.getLatitude(), currentLocation.getLongitude(), Fixed_Coordinates_Latitude, Fixed_Coordinates_Longitude ) ); | |
String data = "The direction of Specific place is " + dir + "° from North."; | |
mDirection.setText( data ); | |
} | |
// get rotation matrix to get gravity and magnetic data | |
SensorManager.getRotationMatrix( rotation, null, gravity, geomagnetic ); | |
// get bearing to target | |
SensorManager.getOrientation( rotation, orientation ); | |
// east degrees of true North | |
bearing = orientation[ 0 ]; | |
// convert from radians to degrees | |
bearing = Math.toDegrees( bearing ); | |
// fix difference between true North and magnetical North | |
if ( geomagneticField != null ) | |
{ | |
bearing += geomagneticField.getDeclination(); | |
} | |
// bearing must be in 0-360 | |
if ( bearing < 0 ) | |
{ | |
bearing += 360; | |
} | |
// Whole compass (N-E-W-S) | |
imageViewObjectAnimator = ObjectAnimator.ofFloat( compassImageView, | |
"rotation", compassImageView.getRotation(), (float) ( 360 - bearing ) ); | |
imageViewObjectAnimator.setDuration( 210 ); | |
imageViewObjectAnimator.start(); | |
// pointer to specific LatLng | |
double direction = ( new GreatCircleBearing().calculateBearing( currentLocation.getLatitude(), currentLocation.getLongitude(), Fixed_Coordinates_Latitude, Fixed_Coordinates_Longitude ) ); | |
int rotation = (int) ( 360 - bearing + direction ); | |
directionObjectAnimator = ObjectAnimator.ofFloat( directionTo, | |
"rotation", directionTo.getRotation(), (float) ( rotation ) ); | |
directionObjectAnimator.setDuration( 210 ); | |
directionObjectAnimator.start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment