Tuesday, 1 September 2015

Android Programming Tutorial 47 : GoogleMaps Example In Android

Following are The Permissions need to Run Google Maps Application:
Step 1
Now we can get the app ready to use mapping functions by adding the appropriate information to the Project Manifest file. Open it and switch to the XML tab to edit the code. First let's add the Google Maps API key inside the application element, using the following syntax:



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

Include your own API key as the value attribute.
Step 2
Now we need to add some permissions to the Manifest. Inside the Manifest element but outside the application element, add the following, amending it to include your own package name:





<permission                                                                              
    android:name="your.package.name.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />                                    
<uses-permission                                                                        android:name="your.package.name.permission.MAPS_RECEIVE"/>

The Maps API is associated with lots of permissions, the need for which of course depends on your own app. The Developer Guide recommends adding the following to any app using Maps:







<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="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" />

Finally, we need to add a feature element for OpenGL ES version 2:




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


Application: 


Create Android Application:

  • File >> New >> Android Application
  • Enter Project name: GoogleMapsApp
  • Package: com.ambilpursunil.newapp
  • Keep other default selections, click Next until you reach Finish

                                   

     1.Simply Copy and Past the Code which is display below in the  MainActivity.java 

    package com.ambilpursunil.newapp;

    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;

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


                public class MainActivity extends Activity {
                GoogleMap map;

                @SuppressLint("NewApi")
                @Override
                protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                map =((MapFragment)                                                                                                 getFragmentManager().findFragmentById(R.id.map)).getMap();
                //map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
               //map.setMapType(GoogleMap.MAP_TYPE_NONE);
               // map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
               // map.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.main, menu);
               return true;
               }
               @Override
              public boolean onOptionsItemSelected(MenuItem item) {
              // Handle action bar item clicks here. The action bar will
              // automatically handle clicks on the Home/Up button, so long
              // as you specify a parent activity in AndroidManifest.xml.
              int id = item.getItemId();
              if (id == R.id.action_settings) {
              return true;
              }
              return super.onOptionsItemSelected(item);
        }
    }


    2.Simple Copy and Past the below code:  actvitiy_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        android:id="@+id/LinearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <fragment 
              android:id="@+id/map"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:name="com.google.android.gms.maps.MapFragment"
              />
    </LinearLayout>

    3.Copy and past the code for manifest.xml

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

        <uses-sdk
            android:minSdkVersion="15"
            android:targetSdkVersion="21" />

        <uses-feature
            android:glEsVersion="0x00020000"
            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="com.google.android.providers.gsf.permission.READ_GSERVICES" />
        <!--
         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" />

        <permission
            android:name="com.ambilpursunil.newapp.permission.MAPS_RECEIVE"
            android:protectionLevel="signature" />

        <uses-permission android:name="com.ambilpursunil.newapp.permission.MAPS_RECEIVE" />

        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".MainActivity"
                android:configChanges="keyboardHidden|orientation"
                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.gms.version"
                android:value="@integer/google_play_services_version" />
            <meta-data
                android:name="com.google.android.maps.v2.API_KEY"
                android:value="Place Your API KEY" />
        </application>

    </manifest>

    4.Right click on the project and Run As-->Android Application


    OutPut:

    When our application launch Map will be appear on the screeen check the output 

               
                                                  

    UnComment the below code and see the output   

               map.setMapType(GoogleMap.MAP_TYPE_NONE);
               map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
               map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);

     In the MainActivity.java Run and Observe the output.

    Please Send Your Comments To ambilpura.sunil@gmail.com








    Stay Tune For Next Tutorial... Progressive Bar In Android:




    Android Programming Tutorial 46 : GoogleMaps Guide In Android

    Google Maps In Android


    Integrating GoogleMaps in Android application to know location on the map, to know the specific place on the earth, different routes on the earth.

    Step by Step to Integrate Google Maps in Android:

    Step 1:

      Install relavent package to as shown below



    Step 2:

    Importing the google_play_services_lib in to your eclipse workspace like the below.

    File>>New>>Import>>General>>Existing Project into Workspace>>Browse the location where you store the bundle>>E:\Softwares\Android Bundle\adt-bundle-windows-x86_64-20140702\sdk\extras\google\google_play_services\libproject and click on finish button.




    The google_play_service will be imported into you eclipse.

    Now add the google_play_service to our project like the below

    Right click on the project and choose preferences and click on the Android from left side then click on the add option as the below figure.



    After clickong on the add choose the google_play service and click on ok;


    Now click on the apply option and ok.




    Step 3:

    Register you Project name into Google Console Api and generate the API KEY from google how to do that follow these steps.

    Goto to google and type Google Console Api and select the first link like the below 


    choose the create project and give the name over there has the below.



    Once the projec created the it will open new widget, click on the API and auths options and again click on api option .
                                        

    enable the Google API v2 Key as the below.




    Now Click on the credentials option below the API and auth option it will open another window and choose Android Key option click on ok



    Once you click on the Android Key option it will ask you enter package and SHA1 fingerprint as shown below , 



    Package means the package of your eclipse Project and for SHA1 fingerprint.

    In Eclipse choose window option>>preferences>>Android(left side)>>build option. once you click on the build option on the right side you will find the MD5 fingerprint and SHA1 Fingerprint in the Edit Text.

    Copy the SHA1 fingerprint and past as the below.


    OR



     click on the create option.




    The Red circle is the API KEY.

    Step 4:

    To Generate SHA1 Fingerprint manually through Command Prompt follow the steps.

    Open command prompt>>Start button>>Run>>type cmd.


    now type the following keytool command in the cmd as the below

    Run keytool using the following command:
    keytool -list -v -keystore [STORE FILENAME] -alias [KEY NAME] -storepass [STORE PASSWORD] -keypass [KEY PASSWORD

    Example:

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

    You can get the keystore from you eclipse
    window>>preferences>>build>>you will find Default debug keystore:copy the that and past in the place of  yellow color place. and click on the enter

    You will find the like the below


    Please Send Your Comments To ambilpura.sunil@gmail.com






    Stay Tune For Next Tutorial...  Google Maps Example In Android: