I am developing an application with Google Maps API v2. I have built a custom Location source to provide location Updates to the Map and also some functions to follow the user, so f.i. when the user presses the "follow" button Aggiorna[=update]BearingAuto and AggiornaPosAuto became true:
@Override
public void OnBearingChanged(float bearing) {
if (AggiornaBearingAuto)
{
mMap.moveCamera(CameraUpdateFactory.newCameraPosition(CameraPosition.builder(mMap.getCameraPosition()).bearing(bearing).build()));
}
}
@Override
public void OnLocationChanged(Location location) {
if (AggiornaPosAuto)
{
CameraPosition Att = mMap.getCameraPosition();
mMap.moveCamera(CameraUpdateFactory.newCameraPosition(
CameraPosition.builder().bearing(Att.bearing).
target(new LatLng(location.getLatitude(), location.getLongitude()))
.tilt(Att.tilt).zoom(Att.zoom).build()
));
}
}
and those functions update the CameraPosition, according to the values provided from my class.
Now, in that class the method which provides Bearing updates is the following:
@Override
public void onSensorChanged( SensorEvent sensorEvent ) {
float[] inR = new float[16];
float[] I = new float[16];
float[] orientVals = new float[3];
double azimuth = 0;
double pitch = 0;
double roll = 0;
// Gets the value of the sensor that has been changed
switch (sensorEvent.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
gravity = sensorEvent.values.clone();
break;
case Sensor.TYPE_MAGNETIC_FIELD:
geomag = sensorEvent.values.clone();
break;
}
// If gravity and geomag have values then find rotation matrix
if (gravity != null && geomag != null) {
// checks that the rotation matrix is found
boolean success = SensorManager.getRotationMatrix(inR, I, gravity, geomag);
if (success) {
SensorManager.getOrientation(inR, orientVals);
azimuth = Math.toDegrees(orientVals[0]);
pitch = Math.toDegrees(orientVals[1]);
roll = Math.toDegrees(orientVals[2]);
}
}
//finally, call OnBeraingChange in Listener
LocListener.OnBearingChanged((float) azimuth);
}
Ok. So the problem is: the maps moves sometimes rendundantly quickly enough to get annoying, like if the Sensor is providing -4, 0, +4, 0, +6, -5, etc. continuously. This make impossible to deal with this functionality. How could Google Maps make the Map rotation so smooth and absolutely stabilized? They implemented a kind of inertia, but how?? Have someone an idea to implement such a function?
I have managed the problem by implementing a Kalman filter.