Google Map Android API V2 – Switching between Normal View, Satellite View and Terrain View

January 11, 2013
By

In this article, we will develop an Android Google Map V2 application which facilitates users to switch between different views ( Normal View, Satellite View and Terrain View ) by tapping corresponding radio buttons.

This application is an upgraded version of the application discussed in the article titled “Android Google Map – Switching between Map view and Satellite view” where Google Map Android API V1 is used.

For developing this application, we are using Eclipse 4.2.1 with ADT plugin ( 21.0.0 ) and Android SDK ( 21.0.0 ) and testing in a real device with Android 2.3.6  ( GingerBread ).


1. Download and configure Google Play Services Library in Eclipse

Please follow the given below link to setup Google Play Service library in Eclipse.

http://developer.android.com/google/play-services/setup.html


2. Create a new Android Application Project namely “LocationGoogleMapViewsV2″

Create new Android application project

Figure 1 : Create new Android application project


3. Configure Android Application Project

Configure new Android application project

Figure 2 : Configure new Android application project


4. Design Application Launcher Icon

Design application launcher icon

Figure 3 : Design application launcher icon


5. Create a blank activity

Create a blank activity

Figure 4 : Create a blank activity


6. Enter Main Activity Details

Enter Main Activity Details

Figure 5 : Enter Main Activity Details


7. Add Google Play Services Library to this project

Add Google Play Service Library to this project

Figure 6 : Add Google Play Service Library to this project


8. Get the API key for Google Maps Android API v2

We need to get an API key from Google to use Google Maps in Android application. Please follow the given below link to get the API key for Google Maps Android API v2.

https://developers.google.com/maps/documentation/android/start


9. Add Android Support library to this project

By default, Android support library (android-support-v4.jar ) is added to this project by Eclipse IDE to the directory libs. If it is not added, we can do it manually by doing the following steps :

  • Open Project Explorer by Clicking “Window -> Show View -> Project Explorer”
  • Right click this project
  • Then from popup window, Click “Android Tools -> Add Support Library “

10. Update the file AndroidManfiest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.wptrafficanalyzer.locationgooglemapviewsv2"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <permission
        android:name="in.wptrafficanalyzer.locationgooglemapviewsv2.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>

    <uses-permission android:name="in.wptrafficanalyzer.locationgooglemapviewsv2.permission.MAPS_RECEIVE"/>

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="in.wptrafficanalyzer.locationgooglemapviewsv2.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="YOUR_API_KEY"/>

    </application>
</manifest>


11. Update the layout file res/layout/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"
    tools:context=".MainActivity" >

    <RadioGroup
        android:id="@+id/rg_views"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/rb_normal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/str_rb_normal"
            android:checked="true" />

        <RadioButton
            android:id="@+id/rb_satellite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/str_rb_satellite" />

        <RadioButton
            android:id="@+id/rb_terrain"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/str_rb_terrain" />

    </RadioGroup>

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/rg_views"
        class="com.google.android.gms.maps.SupportMapFragment"/>

</RelativeLayout>


12. Update the file res/values/strings.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">LocationGoogleMapViewsV2</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="str_rb_normal">Normal</string>
    <string name="str_rb_satellite">Satellite</string>
    <string name="str_rb_terrain">Terrain</string>
</resources>


13. Update the file src/in/wptrafficanalyzer/locationgooglemapviewsv2/MainActivity.java


package in.wptrafficanalyzer.locationgooglemapviewsv2;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;

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

public class MainActivity extends FragmentActivity {

    GoogleMap googleMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        googleMap = mapFragment.getMap();

        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

        RadioGroup rgViews = (RadioGroup) findViewById(R.id.rg_views);

        rgViews.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId == R.id.rb_normal){
                    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                }else if(checkedId == R.id.rb_satellite){
                    googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
                }else if(checkedId == R.id.rb_terrain){
                    googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
                }
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}


14. Screenshots of the application in execution

Google Map Android API V2 in Normal View

Figure 7 : Google Map Android API V2 – Normal View

Google Map Android API V2 - Satellite View

Figure 8 : Google Map Android API V2 – Satellite View

Google Map Android API V2 - Satellite View

Figure 9 : Google Map Android API V2 – Satellite View


15. Download Source Code


How to hire me?

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to info@wptrafficanalyzer.in.

My other blogs
store4js.blogspot.com


Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :



Tags: , , , , ,

12 Responses to Google Map Android API V2 – Switching between Normal View, Satellite View and Terrain View

  1. bouaoud on April 15, 2013 at 6:01 pm

    Thanks A LOT :)

  2. Wedar on April 22, 2013 at 3:47 pm

    Hai thank for the tutorial. but i get a problem :
    when i add googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    the app crash but when i delete it it works but the map won’t change to satelite or terrain view. sorry for bad english. thanks. hope i get the answer

    • george on April 22, 2013 at 5:18 pm

      Are you getting any error message in Logcat?

      • Wedar on April 23, 2013 at 8:12 am

        yes. it’s say null pointer exception

  3. andry yudha on May 9, 2013 at 11:49 am

    thanks george,, i love your tutorials :D

  4. ChulbulPandey on June 14, 2013 at 12:34 pm

    Like always, again such a nice tutorial, easy to learn and use, but this time i need your help, In same tutorial i wanna show 3 custom Locations, using Latitude and Longitude, Cities like: New Delhi, Mumbai and Bangalore, i hope you will add this functionality ASAP.

    Thanks in Advance

  5. Sanjay on June 26, 2013 at 5:40 pm

    Very nice Example and very help fully…………..

  6. agilene on July 2, 2013 at 2:19 pm

    Thank you so much for sharing to us :D

  7. Ashwini Saraf on September 4, 2013 at 3:08 pm

    09-04 14:47:18.053: E/AndroidRuntime(11779): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{in.wptrafficanalyzer.locationgooglemapviewsv2/in.wptrafficanalyzer.locationgooglemapviewsv2.MainActivity}: java.lang.ClassNotFoundException: in.wptrafficanalyzer.locationgooglemapviewsv2.MainActivity in loader dalvik.system.PathClassLoader[/data/app/in.wptrafficanalyzer.locationgooglemapviewsv2-1.apk]
    plz give reply …its urgen. :(

  8. Jalpa Patel on December 11, 2013 at 4:07 pm

    your code is working awesome..but the only prob is it is not showing compass in my map…can you plz help me in that ….

  9. Min Naing Oo on October 5, 2014 at 10:10 pm

    Thank you very much.

  10. Meow on February 16, 2016 at 8:37 pm

    George, you are the best Android teacher in the world!

Leave a Reply

Your email address will not be published. Required fields are marked *

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner