In this article, we will develop a Google Map Android API V2 application with a circle at the touched position where the circle is filled with a semi transparent color.
This application is developed in Eclipse (4.2.1) with ADT plugin (21.1.0) and Android SDK (21.1.0) and tested in a real Android device (Android 2.3.6 - GingerBread).
1. Create a new Android application project namely “SemiTransparentDemo”
2. Configure the project
3. Design application launcher icon
4. Create a blank activity
5. Enter MainActivity details
6. Download and configure Google Play Services Library in Eclipse
Google Map for Android is now integrated with Google Play Services. So we need to set up Google Play Service Library for developing Google Map application in Android.
Please follow the given below link to setup Google Play Service library in Eclipse.
http://developer.android.com/google/play-services/setup.html
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 menu, Click “Android Tools -> Add Support Library “
10. 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" > <fragment android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" class="com.google.android.gms.maps.SupportMapFragment" /> </RelativeLayout>
11. Update the class “MainActivity” in the file src/in/wptrafficanalyzer/semitransparentdemo/MainActivity.java
package in.wptrafficanalyzer.semitransparentdemo; import android.app.Dialog; import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.view.Menu; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GooglePlayServicesUtil; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.GoogleMap.OnMapClickListener; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.CircleOptions; import com.google.android.gms.maps.model.LatLng; public class MainActivity extends FragmentActivity { GoogleMap googleMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Getting Google Play availability status int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext()); // Showing status if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available int requestCode = 10; Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode); dialog.show(); }else { // Google Play Services are available // Getting reference to the SupportMapFragment of activity_main.xml SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); // Getting GoogleMap object from the fragment googleMap = fm.getMap(); // Enabling MyLocation button for the Google Map googleMap.setMyLocationEnabled(true); googleMap.setOnMapClickListener(new OnMapClickListener() { @Override public void onMapClick(LatLng point) { // Removes the existing marker from the Google Map googleMap.clear(); // Instantiating CircleOptions to draw a circle around the marker CircleOptions circleOptions = new CircleOptions(); // Specifying the center of the circle circleOptions.center(point); // Radius of the circle circleOptions.radius(50); // Border color of the circle circleOptions.strokeColor(Color.BLACK); // Fill color of the circle // 0x represents, this is an hexadecimal code // 55 represents percentage of transparency. For 100% transparency, specify 00. // For 0% transparency ( ie, opaque ) , specify ff // The remaining 6 characters(00ff00) specify the fill color circleOptions.fillColor(0x5500ff00); // Border width of the circle circleOptions.strokeWidth(2); // Adding the circle to the GoogleMap googleMap.addCircle(circleOptions); } }); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
12. Update the file AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="in.wptrafficanalyzer.semitransparentdemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <permission android:name="in.wptrafficanalyzer.semitransparentdemo.permission.MAPS_RECEIVE" android:protectionLevel="signature"/> <uses-permission android:name="in.wptrafficanalyzer.semitransparentdemo.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.semitransparentdemo.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_ANDROID_API_KEY"/> </application> </manifest>
13. Screenshot of the application
14. Download the 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
Nice tutorial sir,, I am always waiting your tutorial everytime..
Sir, I want to ask you to give a tutorial on “sending data between activity but by using a spinner”. I’m very curious about it. :-p
Thanks before..
It’s so wonderful. Thanks for your shared! Hope that you share more document like that.
Hi,
Thank you for the suggestion
I’m having some issues (With debug signing, i think).
I exported the Apk building a new Keystore called debug.keystore in /a/personal/directory, following the instructions of developer.android.com/[..]/app-signing.html.
So i got a new APIKEY pushing on Google Console (Maps V2, sure) the SHA1 from: keytool -list -v -keystore /a/personal/directory/debug.keystore -alias androiddebugkey -storepass android -keypass android
Adding ‘;my.awesome.package’.
I placed the APIKEY and run the app on my smartphone (Android 2.3.7) and.. snap, this error: “Failed to load map. Could not contact Google servers.”
Any suggests?
This error may be because of the following reasons :
=> Incorrect API key in AndroidManifest.xml
=> No internet connection in the device
I want the circle to be draggable ,how to do it?
In this code circle is not draggable,to achieve it which method should b used
Yes i got that right,
1 more thing when i try to zoom the circle to include more locations,the map also gets zoomed but i want only the circle should be zoomed.
Thanks
Thanks for the tutorial much appreciated
svp tu peut me l’aide pour creer une application android pour éviter la congestion routière