在Android中旋转MapView [英] Rotate MapView in Android

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

问题描述

我正在写一个Android应用程序,其中一个功能是地图将根据指南针旋转(即,如果手机指向东,地图将面向地图的东边在顶部) 。以前的答案建议在mapView中编写onDraw()方法,但是,api将方法更改为final,因此不能被覆盖。因此,我试图覆盖 dispatchDraw()方法,如下所示:



注意:



-compass是一个布尔值,如果为true,则旋转视图



-bearing是一个浮点数变量,视图应该旋转

  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();
}


解决方案

感谢pheelicks和Nikita Koksharov答案是,我根据指南针打开/关闭地图视图的旋转。



首先,您将需要两个内部 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扩展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 / rotate_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 =#####你的地图钥匙这里######
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_centerHorizo​​ntal =true
android:onClick =onClick
android:textOff =compass off
android:textOn =指南针/>

< / RelativeLayout>

MapActivity

 code> / ** 
*如何使用指南针显示谷歌地图视图旋转的示例活动
*为了使其工作,您需要添加:
* - < uses-图书馆android:name =com.google.android.maps/>在manifest.xml文件
* - 您的Android Maps API密钥从https://developers.google.com/android/maps-api- signup
* - 将项目构建目标设置为Google API
* - 提取/添加MapViewCompassDemo.java的两个内部类RotateView和SmoothCanvas:
* ..\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扩展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);

//注册Android Maps API,网址为:
// https://developers.google.com/android/maps-api-signup
//添加在Mapslayout.xml文件中的MapView的Android Maps API键
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); //不显示路线
}

}

更新:
旋转Google MapView with Compass示例项目: https://www.dropbox.com/sh/c1encbc2lr63qd9/6C1C4hsrlT


I am writing an Android app where one of the features is that the map will rotate according to the compass (i.e. if the phone is pointing east, the map will be oriented so that the east side of the map is on top). Previous answers that I have found suggested over writing the onDraw() method in mapView, however, the api changed the method to final so it cannot be overwritten. As a result I have tried to overwrite the dispatchDraw() method like so:

Note:

-compass is a boolean that if true, rotate the view

-bearing is a float variable that has the degrees that the view should rotate

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();
}

解决方案

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_ Tools\add-ons\addon-google_apis-google-#\samples\MapsDemo\src\com\example\android\apis\view\

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: 
 * ..\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
}

}

Update: Rotating Google MapView with Compass Example Project: https://www.dropbox.com/sh/c1encbc2lr63qd9/6C1C4hsrlT

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

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