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:




    No comments:

    Post a Comment