views:

2354

answers:

4

Hi,

i need to have an mapview on a tab. The attached example shows how to do this if using intents. Now i want to change the zoom level of the mapview(s). How can i do this, although i'm using intents (or is there completely different solution)?

Activity code:

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

Layout code:

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

+1  A: 

Zooming should be possible either by activating the Zoom controls of the MapView and let your user zoom-in or out or instead programmatically by using

mapView.getController().zoomIn();

or

mapView.getController().zoomOut();

or

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

Alternatively what you can do is to pass your intent extra data from outside. I'm just guessing now, but basically you could do something like

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

Within the onCreate(...) of your MyMapActivity you can then read the passed data like

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

Edit: To populate a tab content dynamically:

TabSpec tabSpec = tabHost.newTabSpec("Android X");
tabSpec.setContent(new TabContentFactory() { 
   public View createTabContent(String arg0) { 
       //return the view to add as content
       return theView; 
   }
});
Juri
+1  A: 

Try to use MapController:

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

See also Using Google Maps in Android

UPDATE
You create several instances of the same layout and there is an issue to get needed instance of MapView by id. Can you just use separate layouts for each tab? or create them dynamically?

Max Gontar
+1  A: 

@ juri: Can an intent also receive data outside of onCreate()? (for example to set the zoomlevel dynamically during runtime and not just at creation of the intent)

nr1
you should add such statements as a comment to my post. This is the way to interact on Stackoverflow. I updated my original post to show you how you can create the content of a tab dynamically without using Intents. In this way you should be able to create references to your MapView objects which will then permit you to do zooming etc. at runtime through appropriate calls.
Juri
A: 

I ran your code but it does´nt show the map?. Do you guess what happen? [email protected]

xavier