2010-01-08 67 views
2

我需要在选项卡上有一个mapview。 attached example显示如果使用意图如何做到这一点。 现在我想更改mapview(s)的缩放级别。 我如何做到这一点,虽然我使用的意图(或有完全 不同的解决方案)?Android MapView + Tab示例

活动代码:

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> 

THX

+0

理论上,您现在可以通过您在意图上添加的数据来设置缩放级别,如我在后面的文章中所述。应该管用。 – Juri 2010-01-08 12:31:43

回答

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

内的onCreate(...)你MyMapActivity的你就可以读取传递的数据像

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

尝试使用MapController

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

又见Using Google Maps in Android

UPDATE
创建相同的布局的几个实例,存在这样的问题通过ID来获得MapView类的需要实例。你能为每个标签使用不同的布局吗?或动态创建它们?

1

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

+0

你应该添加这样的语句作为我的文章的评论。这是在Stackoverflow上进行交互的方式。我更新了我的原始帖子,向您展示了如何在不使用Intents的情况下动态创建标签的内容。通过这种方式,您应该能够创建对MapView对象的引用,然后允许您在运行时通过适当的调用进行缩放等操作。 – Juri 2010-01-08 18:53:53