Android 旋转 MapView [英] Android Rotating MapView

查看:32
本文介绍了Android 旋转 MapView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应用程序,它在地图视图上跟踪用户路径.当用户左转时我想实现什么,例如地图应该以这样的方式自行旋转,使得地图视图的头部应该始终指向向上方向.我希望这是有道理的.

I am creating an application which trakcs down users path on a mapview. What I want to achieve when user takes a left turn for instance map should rotate it self in such a way that the head of the mapview should always point to upward direction. I hope that makes sense.

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

I came across Mr. Romain Guy's post an he says

我过去做过这件事需要创建自定义 ViewGroup旋转画布dispatchDraw() 方法.你还需要增加 MapView 的大小(以便它绘制足够的像素时旋转.)您还需要旋转中的触摸事件dispatchTouchEvent().或者如果你使用Android 3.0 你可以简单的调用theMapView.rotate() :

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() :

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

Does anyone came accross some solution similiar to my question ? A working example would be excellent :)

推荐答案

请记住,这不是理想的方法,因为地图上的文本是静态图像并且会随着地图图块旋转(在某些时候会倒过来).

Keep in mind that it's not an ideal approach, because the text on the map is a static image and will rotate along with the map tiles (at some point it will be upside down).

以下是如何将 MapView 放入您自己的 Layout 小部件并旋转它的示例.我已经使用 OpenStreetMaps 完成了这项工作,但对于 Google 地图来说应该完全相同.

Here's an example of how to put the MapView into your own Layout widget and rotate it. I've done it with the OpenStreetMaps, but it should be quite the same for Google Maps.

首先创建旋转"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

<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>

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

Now, every time you receive a geo fix, update the bearing of the rotating layout and it should turn.

这篇关于Android 旋转 MapView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆