3

我需要在选项卡上有一个地图视图。附加的示例显示了如何在使用意图时执行此操作。现在我想更改地图视图的缩放级别。尽管我使用的是意图(或者有完全不同的解决方案),但我该怎么做?

活动代码:

public class TabMapsExample extends TabActivity {    
    TabHost mTabHost;        
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);  
        Context ctx = this.getApplicationContext();         
        //tab 1
        mTabHost = getTabHost();
        TabSpec tabSpec1 = mTabHost.newTabSpec("tab_test1");
        tabSpec1.setIndicator("Map1");
        Intent i1 = new Intent(ctx, MapTabView.class);
        tabSpec1.setContent(i1);
        mTabHost.addTab(tabSpec1);          
        //tab2
        mTabHost = getTabHost();
        TabSpec tabSpec2 = mTabHost.newTabSpec("tab_test1");
        tabSpec2.setIndicator("Map2");
        Intent i2 = new Intent(ctx, MapTabView.class);
        tabSpec2.setContent(i2);
        mTabHost.addTab(tabSpec2);          
        //tab 3
        mTabHost = getTabHost();
        TabSpec tabSpec3 = mTabHost.newTabSpec("tab_test1");
        tabSpec3.setIndicator("Map");
        Intent i3 = new Intent(ctx, MapTabView.class);
        tabSpec3.setContent(i3);
        mTabHost.addTab(tabSpec3);    
    }
}

布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/maptablayout" android:orientation="vertical"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="fill_parent" android:layout_width="fill_parent"
        android:background="#000000" android:orientation="vertical">
        <TextView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent" android:layout_height="50px"
            android:id="@+id/textview" />
        <com.google.android.maps.MapView
            android:id="@+id/mapview" android:layout_width="fill_parent"
            android:layout_height="fill_parent" android:clickable="true"
            android:apiKey="" />
    </LinearLayout>
</RelativeLayout>

谢谢

4

3 回答 3

1

应该可以通过激活 MapView 的缩放控件并让您的用户放大或缩小,或者通过使用以编程方式进行缩放

mapView.getController().zoomIn();

或者

mapView.getController().zoomOut();

或者

mapView.getController().setZoom(...);

或者,您可以做的是从外部传递您的意图额外数据。我现在只是在猜测,但基本上你可以做类似的事情

Intent intent = new Intent(this, MyMapActivity.class); //this is just one way of specifying it
intent.putExtra(MyMapActivity.ZOOM_LEVEL_CONSTANT, 10);
startActivity(intent);

在 MyMapActivity 的 onCreate(...) 中,您可以读取传递的数据,例如

Bundle retrievedData = this.getIntent().getExtras();
if (retrievedData != null) {
   int zoomLevel = retrievedData.getInt(ZOOM_LEVEL_CONSTANT);
   mapView.getController.setZoom(zoomLevel);
}

编辑: 动态填充选项卡内容:

TabSpec tabSpec = tabHost.newTabSpec("Android X");
tabSpec.setContent(new TabContentFactory() { 
   public View createTabContent(String arg0) { 
       //return the view to add as content
       return theView; 
   }
});
于 2010-01-08T11:50:59.453 回答
1

尝试使用MapController

    MapView mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);
    MapController mapController = mapView.getController();
    mapController.setZoom(10);

另请参阅在 Android 中使用 Google 地图

更新
您创建了相同布局的多个实例,并且通过 id 获取所需的 MapView 实例存在问题。您可以为每个选项卡使用单独的布局吗?或动态创建它们?

于 2010-01-08T11:51:39.170 回答
1

@ juri:intent 也可以在 onCreate() 之外接收数据吗?(例如,在运行时动态设置缩放级别,而不仅仅是在创建意图时)

于 2010-01-08T16:46:22.703 回答