Thanks to pheelicks and Nikita Koksharov answers, I manage to turn on/off the rotation of a mapview according to the compass.
First you will need the two inner class of MapViewCompassDemo.java found at: Android_SDK_ Toolsadd-onsaddon-google_apis-google-#samplesMapsDemosrccomexampleandroidapisview
RotateView
SmoothCanvas
Extract the inner class RotateView to RotateView.java and add SmoothCanvas as a inner class of RotateView.java instead of MapViewCompassDemo.java
public class RotateView extends ViewGroup implements SensorListener {
...
static final class SmoothCanvas extends Canvas {
...
}//end SmoothCanvas
}//end RotateView
maplayout.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map_layout_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/rotating_view"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.google.android.maps.MapView
android:id="@+id/map_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:apiKey="##### YOUR MAP KEY HERE ######"
android:clickable="true" />
</LinearLayout>
<ToggleButton
android:id="@+id/button_compass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="onClick"
android:textOff="compass off"
android:textOn="compass on" />
</RelativeLayout>
The MapActivity
/**
* Example activity on how to display a google map view rotation with compass
* To make it work you need to add:
* - <uses-library android:name="com.google.android.maps" /> in the manifest.xml file
* - Your Android Maps API Key from https://developers.google.com/android/maps-api- signup
* - Set the project build target to "Google APIs"
* - Extract/Add the two inner classes RotateView and SmoothCanvas of MapViewCompassDemo.java found at:
* ..AndroidAndroid SDK Toolsadd-onsaddon-google_apis-google-#samplesMapsDemosrccomexampleandroidapisview
*
* @author hsigmond - touchboarder.com -
*
*/
public class MapViewRotationWithCompass extends MapActivity {
private MapView mMapView;
private MyLocationOverlay mMyLocationOverlay = null;
private boolean mModeCompass = false;
private SensorManager mSensorManager;
private LinearLayout mRotateViewContainer;
private RotateView mRotateView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maplayout);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mRotateViewContainer = (LinearLayout) findViewById(R.id.rotating_view);
mRotateView = new RotateView(this);
// Sign Up for the Android Maps API at:
// https://developers.google.com/android/maps-api-signup
// Add the Android Maps API key to the MapView in the maplayout.xml file
mMapView = (MapView) findViewById(R.id.map_view);
mMyLocationOverlay = new MyLocationOverlay(this, mMapView);
}
@SuppressWarnings("deprecation")
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_compass:
if (mMyLocationOverlay.isCompassEnabled()) {
mSensorManager.unregisterListener(mRotateView);
mRotateView.removeAllViews();
mRotateViewContainer.removeAllViews();
mRotateViewContainer.addView(mMapView);
mMyLocationOverlay.disableCompass();
mModeCompass = false;
} else {
mRotateViewContainer.removeAllViews();
mRotateView.removeAllViews();
mRotateView.addView(mMapView);
mRotateViewContainer.addView(mRotateView);
mMapView.setClickable(true);
mSensorManager.registerListener(mRotateView,
SensorManager.SENSOR_ORIENTATION,
SensorManager.SENSOR_DELAY_UI);
mMyLocationOverlay.enableCompass();
mModeCompass = true;
}
break;
}
}
@SuppressWarnings("deprecation")
@Override
public void onResume() {
super.onResume();
if (mModeCompass) {
mMyLocationOverlay.enableCompass();
mSensorManager.registerListener(mRotateView,
SensorManager.SENSOR_ORIENTATION,
SensorManager.SENSOR_DELAY_UI);
}
}
@Override
public void onPause() {
super.onPause();
mMyLocationOverlay.disableCompass();
}
@SuppressWarnings("deprecation")
@Override
protected void onStop() {
mSensorManager.unregisterListener(mRotateView);
super.onStop();
}
@Override
protected boolean isRouteDisplayed() {
return (false);// Don't display a route
}
}
Update:
Rotating Google MapView with Compass Example Project: https://www.dropbox.com/sh/c1encbc2lr63qd9/6C1C4hsrlT
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…