Android - API Google Maps - Using MapType

I think the MapType is certainly the most fascinating feature of GoogleMaps and also one of the most easy to understand and make alive in an Android application.

With one click it's possible to change the display of the map such as hybrid, none, normal, satellite or terrain.

And this is exactly what we are going to see in this Android Tutorial 5.

Explanation

The setMapType() method is the key of this feature.

Indeed, we have just to pass the map type as parameter to change the display.

I chose to have a minSdkVersion equals to 12 because the Google Maps Android API requires API level 12 or higher for the support of MapFragment objects.

Of course, we will need to understand how to create and manage the Google Maps API v2.

And last but not least, you can download this tutorial as an application, directly from the Google Play store.

Code

MainActivity.java

package com.badprog.android.tutorial.api2.tuto_5_badprogtutorialmaptype;

import android.app.Activity;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;

public class MainActivity extends Activity {

    private GoogleMap        gm;
    private RelativeLayout     mainLayout;
    final float             _X = 250;
    final float             _Y = 90;

    private int[]    arrayType    = {
            GoogleMap.MAP_TYPE_HYBRID,
            GoogleMap.MAP_TYPE_NONE,
            GoogleMap.MAP_TYPE_NORMAL,
            GoogleMap.MAP_TYPE_SATELLITE,
            GoogleMap.MAP_TYPE_TERRAIN
    };
    
    private CharSequence[] arrayText    = {
            "Hyb",
            "Non",
            "Nor",
            "Sat",
            "Ter",
    };
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        badprogSetLayout();            // set main layout
        badprogHandleMapType();     // handle MapType settings
        badprogManagerButton();        // manage buttons
    }
    
    /**
     * Set main layout
     */
    public void badprogSetLayout() {
        this.mainLayout = (RelativeLayout)findViewById(R.id.main_layout); // match layout
    }
    
    /**
     * Manage buttons
     */
    public void badprogManagerButton() {
        int i = 0;
        
        while (i < arrayType.length) {
            badprogCreatingButtonType(arrayType[i], arrayText[i], _X, _Y * (i));
            ++i;
        }
    }
    
    /**
     * Creating a Button with its corresponding type
     */
    public Button badprogCreatingButtonType(final int mapType, CharSequence text,
                                            final float x, final float y) {
        Button b = new Button(this);
        
        b.setX(badprogGetScreenWidth() - x);
        b.setY(y);
        b.setText(text);
        this.mainLayout.addView(b);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gm.setMapType(mapType);
            }
        });
        return b;
    }
    
    /**
     * Retrieve screen's width
     */
    public int badprogGetScreenWidth() {
        Display d = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        
        d.getSize(size);
        int width = size.x;
        return width;
    }
    
    /**
     * Handle MapType settings
     */
    public void badprogHandleMapType() {
        gm = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:id="@+id/main_layout"
    >
    
    <fragment
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.MapFragment"/>
    
</RelativeLayout>

 

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Refresh Type the characters you see in this picture. Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.