magnetic declination android studio code example

Example 1: magnetic declination android studio

public void setLocation(Location location)
  {
    GeomagneticField geomagneticField = new GeomagneticField(
      (float) location.getLatitude(),
      (float) location.getLongitude(),
      (float) location.getAltitude(),
      System.currentTimeMillis());
    declination = (float) Math.toRadians(geomagneticField.getDeclination());
  }
}

Example 2: magnetic declination android studio

private void updateSensorValues(SensorEvent event) {
  if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
    setAcclValues(event);
  } else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
    setmagValues(event);
  }
  SensorManager.getRotationMatrix(rotation, null, grav, mag);
  SensorManager.getOrientation(rotation, orientation);
  double floatBearing = orientation[0];
  floatBearing = Math.toDegrees(floatBearing); 
  if (magField != null)
    floatBearing += magField.getDeclination();
  if (floatBearing < 0)
    floatBearing += 360;
  setBearing(floatBearing);
  setRotationInDegrees(floatBearing);
}

Tags:

Misc Example