Saturday, November 2, 2013

Google Map

In this tutorial, you will learn to create a Google Map app. The Google Map app will display the Google map and spot the current location of the device on the map by a red-filled circle. In case that the current location can not be retrieved at the time, you will not see the red-filled circle at the correct location. When the user touches any location on the map, the address of that location will be shown. The address to display will include latitude, longitude, street, city, and country. Without internet connection, the app is able to show only the latitude and longitude of the location. To get the full address as mentioned, you need to make sure the internet connection work properly.

To begin the GoogleMap app, you will create a new Android Project in Eclipse. The name of the project will be GMap.

Getting Google Map to work in your app requires many steps as shown below.

1. Download and install Google Play Service API. You can use the Android SDK Manager to install this API. You would get the google-play-service.jar file stored in the directory where you store the Android SDK. In my machine, this is the path of the jar file: D:\androidbundle\sdk\extras\google\google_play_services\libproject\google-play-services_lib\libs. To use the API in the GMap project, you need to add this jar file into the project build path (Project->Properties->Java Build Path->Libraries->Add External Jars...) of the Eclipse.

2. Get the SHA-1 fingerprint for your certificate. For Window 7 or Vista users, you can get the SHA-1 key by issuing the following command from the command prompt window. You will need to change the path of the keystore file. In my case, the keystore file is in the path C:\Users\Acer\.android.

keytool -list -v -alias androiddebugkey -keystore C:\Users\Acer\.android\debug.keystore -alias name: androiddebugkey -storepass android -keypass android

3. You would see the output similar to this:
Creation date: Sep 8, 2013
Entry type: PrivateKeyEntry
Certificate chain length: 1
Owner: CN=Android Debug, O=Android, C=US
Issuer: CN=Android Debug, O=Android, C=US
Serial number: 5883f7cc
Valid from: Sun Sep 08 14:28:53 ICT 2013 until: Tue Sep 01 14:28:53 ICT 2043
Certificate fingerprints:
MD5: 2A:9E:7C:7B:87:6C:5D:1F:B4:84:C0:84:BB:45:10:69
SHA1: D0:91:62:F8:32:45:B1:89:94:B3:79:B4:FD:DD:64:22:C9:72:B9:E3
SHA256: FF:4C:BF:52:EA:C1:0E:0D:FF:C0:8E:C3:2A:0D:41:ED:07:DA:3A:3D:40:
Signature algorithm name: SHA256withRSA
Version: 3
#1: ObjectId: Criticality=false
SubjectKeyIdentifier [
KeyIdentifier [
0000: 2F 69 DC 79 F8 A5 07 1A 10 61 EC BD 1D E0 58 AC /i.y.....a....X.
0010: 94 CB 18 C5 ....

4. Obtain API key from Google. To get the API key from Google, you can follow the steps below:
- Open Google Console then create a new project by clicking the Create... from the drop-down list.
- Select API Access from the active project you created. In the resulting page, click Create New Android Key....In the resulting page, you are required to enter the SHA-1 key, semi-colon, and the package name of the your project. See the picture below.

- You will get the API key similar to this AIzaSyB6aqPG9XhbPMGVzkoohdlgK2HzRHe85nA.
- Turn on Goole Map Android API v2 service. You will select Services and turn on Goole Map Android API v2.
- Register the API key to your GMap app. You will open the AndroidManifest.xml file of the app and just above </application> paste the following code.

- You will need to set some permissons and the use of OpenGL ES version 2 feature in the AndroidManifest file of your app.
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name=""/>
<!-- The following two permissions are not required to use
Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
android:required="true" />

This is the complete AndroidManifest.xml file.

AndroidManifest.xml file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    android:versionName="1.0" >

        android:targetSdkVersion="17" />
        android:required="true" />

  <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission     android:name=""/>
<!-- The following two permissions are not required to use
     Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

        android:theme="@style/AppTheme" >
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />


Now you are ready to add a map to the GMap app. You will copy and paste (override) the following code to the activity_main.xml file.

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android=""

In the MainActivity class, you will need more code to show the map, apply the settings to map, identify the current location, and display the address of the location when the user touches that location. Here is the content of the MainActivity class.

MainActivity. java file

package com.example.gmap;
import java.util.List;
import java.util.Locale;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;


public class MainActivity extends FragmentActivity{
  private GoogleMap map;
  private LocationManager locationManager;
  private Location mylocation;
  protected void onCreate(Bundle savedInstanceState) {

  protected void onStart() {

  public void setupMap(){
  SupportMapFragment mf=(SupportMapFragment)getSupportFragmentManager().findFragmentById(;  


  public void appMapSettings(){
 //enable map click
 map.setOnMapClickListener(new MapClick());
 //specify the type of map
 //enable my location on the map
 //enable button for my location
 UiSettings uis=map.getUiSettings();


  public void spotCurrentLocation(Location location){
     double lat,lng;
// Instantiates a new CircleOptions object and defines the center and radius
 CircleOptions circleOptions = new CircleOptions();
 if(location==null) {//default center

 //center based on the current location LatLng(lat,lng));
 circleOptions.radius(1000); // In meters
 Circle circle = map.addCircle(circleOptions);
 //set target location
 CameraUpdate center=CameraUpdateFactory.newLatLng(new LatLng(lat,lng));
         CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
         //set zoom level
  class MapClick implements OnMapClickListener{

 public void onMapClick(LatLng coor){




  public void doInBackground(LatLng coordinate){
 final LatLng coor=coordinate;
 Handler handler=new Handler(); Runnable(){
 public void run(){

  public void showAddress(double lat,double lng){

 Geocoder geocoder =new Geocoder(getBaseContext(), Locale.getDefault());
 List<Address> addresses = null;
 String addressText="";
 int count=0;
 try {    
 addresses = geocoder.getFromLocation(lat, lng, 1);
 addresses = geocoder.getFromLocation(lat, lng, 1);
 } catch (Exception e1) {Log.e(this.toString(),"Error...");}

 if (addresses != null && addresses.size() > 0) {
// Get the first address
Address address = addresses.get(0);
//get street, city, and country
addressText =address.getMaxAddressLineIndex()>0?address.getAddressLine(0)+", ":"null, ";
addressText+=address.getLocality()+", "+address.getCountryName();


 Toast.makeText(getBaseContext(), "Address:("+lat+","+lng+") "+addressText, Toast.LENGTH_SHORT).show();

  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(, menu);
    return true;

  public void determineLocation() {
String location_context = Context.LOCATION_SERVICE;
//Create locationManager object from the Android system location service
locationManager = (LocationManager)getSystemService(location_context);
//retrieve the available location providers
  List<String> providers = locationManager.getProviders(true);
  for (String provider : providers) {
  locationManager.requestLocationUpdates(provider, 1000, 0, new LocationListener() {
  public void onLocationChanged(Location location) {
  //spot the current update location
  public void onProviderDisabled(String provider){}
  public void onProviderEnabled(String provider){}
  public void onStatusChanged(String provider, int status, Bundle extras){}
  //get the current device' location from the provider
  mylocation= locationManager.getLastKnownLocation(provider);


In the onCreate method, the determineLocation method is called to detect the current location of the device. The getSystemService method returns the LocationManager object. This object will be used to get the information about location providers. Each provider represents a different technology used to determine the current locaiton. A location on the device changes when the device moves. So to get the current updated location from Android, you will need to invoke the requestLocationUpdates method of the LocationManager class. The getLastKnownLocation method will be used to get the current locaiton of the device.

In the onStart method, the setupMap, appMapSettings, and spotCurrentLocation methods are invoked. The setupMap method will show the map. The appMapSettings method specifies settings for the map. You will read he comments in code to get the idea about each setting. The spotCurrentLocation will spot the current location by a red-filled circle. The circle will cover 1000 meters around the current location.

The showAddress method will be called each time the user touched the map. The getFromLocation method of the Geocoder class is used to get the address of the current location. This method return a string that contain street, city, and country of the current location.

Download the apk file of the GMap app.


  1. Vương Lâm cầm lấy quần áo, hỏi:

    - Ta nghỉ ngơi ở đâu?

    Thanh niên mắt cũng không mở, không chút để ý nói

    : - Đi hướng Bắc, tự nhiên sẽ nhìn đến một loạt phòng ốc, đem thẻ bài đưa cho đệ tử nơi đó, liền an bài phòng cho ngươi.

    Vương Lâm ghi tạc trong lòng, xoay người hướng bắc đi đến, đợi hắn đi rồi, thanh niên mở to mắt, miệt thị lẩm bẩm:
    học kế toán thực hành
    forum rao vặt cattleya
    học kế toán tổng hợp
    chung cư eco-green-city
    học kế toán thực hành
    học kế toán tại bắc ninh
    dịch vụ kế toán trọn gói
    chung cư hà nội
    dịch vụ làm báo cáo tài chính
    kế toán cho quản lý
    khoá học kế toán thuế
    keny idol
    trung tâm kế toán tại long biên
    trung tâm kế toán tại hải phòng

    - Lại có thể dựa vào tự sát mới gia nhập vào đây, thật sự là một phế vật.

    Đi ở bên trong Hằng Nhạc Phái, Vương Lâm dọc theo đường đi nhìn thấy phần lớn đệ tử đều là mặc áo xám, một đám bộ dạng hấp tấp, sắc mặt lãnh đạm, có một số trong tay còn cầm công cụ lao động, vẻ mặt lộ chút mệt mỏi.

    Vẫn hướng bắc đi được hồi lâu, rốt cục nhìn đến một loạt phòng ốc không lớn, ở đây áo xám đệ tử phải so với nơi khác nhiều hơn không ít, nhưng vẫn như cũ vẫn cứ làm việc của bọn họ, rất ít nói chuyện với nhau.

    Sau khi đem thẻ bài giao cho hoàng y đệ tử phụ trách nơi đây, đối phương nói cũng chưa từng nói một câu, không kiên nhẫn chỉ một căn phòng.

    Vương Lâm cũng đã quen với biểu tình lãnh đạm của mọi người ở đây, đi đến phòng ở, đẩy cửa đi vào nhìn thấy, căn phòng không lớn, hai chiếc giường gỗ, một bàn, quét dọn rất sạch sẽ, mức độ mới cũ cùng trong nhà không sai biệt lắm.

    Hắn chọn lấy một giường gỗ thoạt nhìn không người sử dụng, đem hành lý để lên, lúc này mới nằm ở trên giường trong lòng suy nghĩ rất nhiều, tuy rằng cuối cùng vào được Hằng Nhạc Phái này, nhưng cũng không phải như hắn tưởng tượng như vậy có thể tu luyện tiên thuật, lúc trước nghe ý tứ của thanh niên hoàng y kia, công tác của mình là nấu nước.

  2. There are lots of information about hadoop have spread around the web, but this is a unique one according to me. The strategy you have updated here will make me to get to the next level in big data. Thanks for sharing this. Hadoop Training in Chennai | Big Data Training in Chennai

  3. This blog is having a wonderful talk. The technology are discussed and provide a great knowledge to all. This helps to learn more details about technology. All this

    details are important for this technology. Thank you for this blog.
    Hadoop Training in Chennai

  4. Great article. Glad to find your blog. Thanks for sharing.

    digital marketing institute

  5. hi welcome to all.its really informative blog.try to implement more set of ideas through it.
    apachespark training

  6. Glad to have visit in this blog, i enjoyed reading this blogs.

    Keep updating more:)

    Oracle Forms & Report in Chennai

  7. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing.

    Informatica Training | Hadoop Training in Chennai

  8. Very good knowledge and very good information. The simplest method to do this process. This is very useful for many peoples.Nice it. We'll have to share it amazing posting.I like that your moderate useful article.I read all your blog is humbled excellent blogger commenting.
    Digital Marketing Course in Chennai | Best Digital Marketing Training Institute | SEO Training Chennai

  9. Thanks for posting useful information.You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...Really it was an awesome article...very interesting to read..
    please sharing like this information......
    Android training in chennai
    Ios training in chennai

  10. You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...

    Mobile App Development Company
    Mobile App Development Company
    Mobile app Development Companies

  11. Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care and we take your comments to heart.As always, we appreciate your confidence and trust in us
    Java Training in Chennai

  12. Being new to the blogging world I feel like there is still so much to learn. Your tips helped to clarify a few things for me as well as giving..
    Android App Development Company

  13. great and nice blog thanks sharing..I just want to say that all the information you have given here is awesome...Thank you very much for this one.
    Web Design Development Company
    Web design Company in Chennai
    Web development Company in Chennai

  14. I am expecting more interesting topics from you. And this was nice content and definitely it will be useful for many people.
    Fitness SMS
    Salon SMS
    Investor Relation SMS

  15. it is really amazing...thanks for sharing....provide more useful information...
    Mobile app development company