Skip to content

Commit

Permalink
More fixes for synchronizing compass with map. Removed SensorEventLis…
Browse files Browse the repository at this point in the history
…tener interface.
  • Loading branch information
zinfin committed Jun 15, 2016
1 parent e3debdc commit d241e2d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 113 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,3 @@ Icon?
ehthumbs.db
Thumbs.db

maps-app/src/main/res/values/app_settings.xml
82 changes: 40 additions & 42 deletions maps-app/src/main/java/com/esri/android/mapsapp/MapFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,7 @@
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.Map;
import com.esri.arcgisruntime.mapping.Viewpoint;
import com.esri.arcgisruntime.mapping.view.DefaultMapViewOnTouchListener;
import com.esri.arcgisruntime.mapping.view.Graphic;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.mapping.view.LocationDisplay;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.mapping.view.WrapAroundMode;
import com.esri.arcgisruntime.mapping.view.*;
import com.esri.arcgisruntime.portal.Portal;
import com.esri.arcgisruntime.portal.PortalItem;
import com.esri.arcgisruntime.security.AuthenticationManager;
Expand Down Expand Up @@ -268,6 +263,12 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
// add graphics layer
addGraphicLayers();

// synchronize the compass icon as the map changes
mMapView.addVisibleAreaChangedListener(new VisibleAreaChangedListener() {
@Override public void visibleAreaChanged(VisibleAreaChangedEvent visibleAreaChangedEvent) {
mCompass.setRotationAngle(((MapView)visibleAreaChangedEvent.getSource()).getMapRotation());
}
});
}
}

Expand All @@ -285,39 +286,39 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
private void setClickListenerForFloatingActionButton(final MapView mapView) {
final FloatingActionButton fab = (FloatingActionButton) mMapContainer.findViewById(R.id.fab);
fab.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mLocationDisplay = mapView.getLocationDisplay();
// Pan to location
mLocationDisplay.setAutoPanMode(LocationDisplay.AutoPanMode.DEFAULT);


final String currentMode = mLocationDisplay.getAutoPanMode().name();
// Toggle compass mode
if (!mIsInCompassMode){
fab.setImageResource(R.drawable.ic_action_compass_mode);
mCompass.start();
mCompass.setVisibility(View.VISIBLE);
mLocationDisplay.setAutoPanMode(LocationDisplay.AutoPanMode.COMPASS);
mIsInCompassMode = true;

} else {

fab.setImageResource(android.R.drawable.ic_menu_mylocation);
mCompass.setRotationAngle(0);
final ListenableFuture<Boolean> setViewpointListener = mMapView.setViewpointRotationAsync(0);
try {
setViewpointListener.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
mLocationDisplay.setAutoPanMode(LocationDisplay.AutoPanMode.OFF);
mIsInCompassMode = false;
}
}

});
}
@Override
public void onClick(View v) {
mLocationDisplay = mapView.getLocationDisplay();
// Pan to location
mLocationDisplay.setAutoPanMode(LocationDisplay.AutoPanMode.DEFAULT);

final String currentMode = mLocationDisplay.getAutoPanMode().name();
// Toggle compass mode
if (!mIsInCompassMode) {
fab.setImageResource(R.drawable.ic_action_compass_mode);
mCompass.setVisibility(View.VISIBLE);
mLocationDisplay.setAutoPanMode(LocationDisplay.AutoPanMode.COMPASS);
mIsInCompassMode = true;

} else {
// turn of the pan mode before setting compass back to 0
mLocationDisplay.setAutoPanMode(LocationDisplay.AutoPanMode.OFF);
mIsInCompassMode = false;
fab.setImageResource(android.R.drawable.ic_menu_mylocation);
final ListenableFuture<Boolean> setViewpointListener = mMapView.setViewpointRotationAsync(0);
setViewpointListener.addDoneListener(new Runnable() {
@Override public void run() {
try {
setViewpointListener.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
});
}
}
});
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Expand Down Expand Up @@ -365,9 +366,6 @@ public void onResume() {
if (mIsInCompassMode) {
mMapView.getLocationDisplay().startAsync();
}
if (mCompass != null){
mCompass.start();
}

}
}
Expand Down
72 changes: 2 additions & 70 deletions maps-app/src/main/java/com/esri/android/mapsapp/tools/Compass.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,12 @@
* listeners.
*
*/
public class Compass extends View implements SensorEventListener {
public class Compass extends View {

// Handles the sensors
public final SensorManager sensorManager;
private final Paint mPaint;
private final Bitmap mBitmap;
private final Matrix mMatrix;
// Sensors for accelerometer and magnetometer
private final Sensor gSensor;
private final Sensor mSensor;
// Used for orientation of the compass
private final float[] mGravity = new float[3];
private final float[] mGeomagnetic = new float[3];
// To send and receive notification from the sensors.
public SensorEventListener sensorEventListener;
private float mAngle = 0;

public Compass(Context context) {
Expand All @@ -68,24 +59,6 @@ public Compass(Context context) {
mMatrix = new Matrix();

mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.eaf_compass);

sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
gSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
}

public void start() {
// A copy of instance which is used to restart the sensors
sensorEventListener = this;

// Enable the sensors
sensorManager.registerListener(this, gSensor, SensorManager.SENSOR_DELAY_GAME);
sensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_GAME);
}

public void stop() {
// Disable the sensors
sensorManager.unregisterListener(this);
}

/**
Expand All @@ -94,7 +67,7 @@ public void stop() {
*/
public void setRotationAngle(double angle) {
// Save the new rotation angle.
mAngle = (float) MapFragment.mMapView.getMapRotation();
mAngle = (float) angle;
// Force the compass to re-paint itself.
postInvalidate();
}
Expand All @@ -104,7 +77,6 @@ public void setRotationAngle(double angle) {
*/
@Override
protected void onDraw(Canvas canvas) {

// Reset the matrix to default values.
mMatrix.reset();

Expand All @@ -116,45 +88,5 @@ protected void onDraw(Canvas canvas) {
canvas.drawBitmap(mBitmap, mMatrix, mPaint);

super.onDraw(canvas);

}

@Override
public void onSensorChanged(SensorEvent event) {
final float alpha = 0.97f;

synchronized (this) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

mGravity[0] = alpha * mGravity[0] + (1 - alpha) * event.values[0];
mGravity[1] = alpha * mGravity[1] + (1 - alpha) * event.values[1];
mGravity[2] = alpha * mGravity[2] + (1 - alpha) * event.values[2];
}

if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {

mGeomagnetic[0] = alpha * mGeomagnetic[0] + (1 - alpha) * event.values[0];
mGeomagnetic[1] = alpha * mGeomagnetic[1] + (1 - alpha) * event.values[1];
mGeomagnetic[2] = alpha * mGeomagnetic[2] + (1 - alpha) * event.values[2];

}

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);
float azimuth = (float) Math.toDegrees(orientation[0]);
azimuth = (azimuth + 360) % 360;

setRotationAngle(-azimuth);
}
}
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
7 changes: 7 additions & 0 deletions maps-app/src/main/res/values-v21/app_settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<!-- TODO: add your OAuth Client ID here-->
<string name="client_id">STV3sRWfJQxqvspH</string>

</resources>

0 comments on commit d241e2d

Please sign in to comment.