我正在编写一个 Android 应用程序,其中一个功能是 map 将根据指南针旋转(即,如果手机指向东方,则 map 将被定向,以便 map 的东侧位于顶部) .我发现以前的答案建议在 mapView 中编写 onDraw() 方法,但是,api 将方法更改为 final,因此它不能被覆盖。结果,我尝试像这样覆盖 dispatchDraw()
方法:
注意:
-compass 是一个 bool 值,如果为真,则旋转 View
-bearing 是一个浮点变量,具有 View 应旋转的度数
protected void dispatchDraw(Canvas canvas) {
canvas.save();
if (compass) {
final float w = this.getWidth();
final float h = this.getHeight();
final float scaleFactor = (float)(Math.sqrt(h * h + w * w) / Math.min(w, h));
final float centerX = w / 2.0f;
final float centerY = h / 2.0f;
canvas.rotate(bearing, centerX, centerY);
canvas.scale(scaleFactor, scaleFactor, centerX, centerY);
}
super.dispatchDraw(canvas);
canvas.restore();
}
最佳答案
感谢 pheelick 和 Nikita Koksharov 的回答,我设法根据指南针打开/关闭 map View 的旋转。
首先,您需要 MapViewCompassDemo.java 的 两个内部 类,该类位于:Android_SDK_ Tools\add-ons\addon-google_apis-google-#\samples\MapsDemo\src\com\example\android\apis\view\
RotateView
SmoothCanvas
提取内部类RotateView到RotateView.java并添加 SmoothCanvas作为RotateView.java的内部类而不是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>
map Activity
/**
* 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:
* ..\Android\Android SDK Tools\add-ons\addon-google_apis-google-#\samples\MapsDemo\src\com\example\android\apis\view\
*
* @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
}
}
更新: 使用 Compass 示例项目旋转 Google MapView:https://www.dropbox.com/sh/c1encbc2lr63qd9/6C1C4hsrlT
关于android - 在 Android 中旋转 MapView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1830391/