Android 旋转 MapView

标签 android android-mapview rotation

我正在创建一个应用程序,它在 map View 上跟踪用户路径。例如,当用户左转时,我想要实现的目标是 map 应该以 map View 的头部始终指向向上的方式自行旋转。我希望这是有道理的。

我看到了 Romain Guy 先生的帖子,他说

I have done this in the past and it requires to create a custom ViewGroup that rotates the Canvas in the dispatchDraw() method. You also need to increase the size of the MapView (so that it draws enough pixels when rotated.) You will also need to rotate the touch events in dispatchTouchEvent(). Or if you use Android 3.0 you can simply call theMapView.rotate() :

有没有人遇到过与我的问题类似的解决方案?一个有效的例子会很好 :)

最佳答案

请记住,这不是一种理想的方法,因为 map 上的文本是静态图像,并且会随着 map 图 block 旋转(在某些时候它会上下颠倒)。

下面是一个示例,说明如何将 MapView 放入您自己的 Layout 小部件中并旋转它。我已经使用 OpenStreetMaps 完成了它,但它应该与 Google map 完全相同。

首先创建“旋转”Layout 小部件

package com.eli.util;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.widget.LinearLayout;

public class RotatingLinearLayout extends LinearLayout {

    private final int mDiagonal;
    private float mBearing;

    public RotatingLinearLayout(final Context pContext, final AttributeSet pAttrs) {
        super(pContext, pAttrs);
        final DisplayMetrics dm = pContext.getResources().getDisplayMetrics();
        mDiagonal = (int) Math.hypot(dm.widthPixels, dm.heightPixels);
    }

    public void setBearing(final float pBearing) {
        mBearing = pBearing;
    }

    @Override
    protected void dispatchDraw(final Canvas pCanvas) {
        pCanvas.rotate(-mBearing, getWidth() >> 1, getHeight() >> 1);
        super.dispatchDraw(pCanvas);
    }

    @Override
    protected void onMeasure(final int pWidthMeasureSpec,
            final int pHeightMeasureSpec) {
        final int widthMode = MeasureSpec.getMode(pWidthMeasureSpec);
        final int heightMode = MeasureSpec.getMode(pHeightMeasureSpec);
        super.onMeasure(MeasureSpec.makeMeasureSpec(mDiagonal, widthMode), MeasureSpec.makeMeasureSpec(mDiagonal, heightMode));
    }
}

layout.xml 中的 MapView 包围起来

<com.eli.util.RotatingLinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/rotating_layout">
    <org.osmdroid.views.MapView
        android:id="@+id/map_view"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:enabled="true"
        android:clickable="true"/>
</com.eli.util.RotatingLinearLayout>

现在,每次收到地理定位时,更新旋转布局的方位,它应该会旋转。

关于Android 旋转 MapView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6028999/

相关文章:

android - 如何像谷歌地图应用程序一样将谷歌标志移动到透明操作栏上方?安卓

iphone - 旋转时调整 iPhone 上的 View 大小

CSS3 旋转翻转卡片 - 闪烁?

java - Android:如何永久设置可见性

java - 如何在 WebView 中添加进度条/加载条

android - 如何在android中的 map 上添加动画图钉?

android - 在谷歌地图上标记当前位置

android - 钛 android 中的 tableview 子项中的标签文本未更新。但在 IOS 中有效

android - 使用 AWS cognito 注册 Android 应用程序

matrix - 从二维变换矩阵中获取旋转