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″
3. Configure Android Application Project
4. Design Application Launcher Icon
5. Create a blank activity
6. Enter Main Activity Details
7. Add Google Play Services 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
15. Download Source Code


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
Thanks A LOT
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
Are you getting any error message in Logcat?
yes. it’s say null pointer exception
thanks george,, i love your tutorials
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
Very nice Example and very help fully…………..
Thank you so much for sharing to us
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.
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 ….
Thank you very much.
George, you are the best Android teacher in the world!