Android Display Google Map in Android Device?

GETTING KEY FROM GOOGLE CONSOLE

Before we create Application Generate API key in  Google API Console .

1. Now open Google API Console
2. Select Services on left side and turn on Google Maps Android API v2

3. Now select API Access on left side and on the right side click on Create new Android key…
4.  I have given like below
BE:03:E1:44:39:7B:E8:17:02:9F:7F:B7:98:82:EA:DF:84:D0:FB:6A;com.example.name
And note down the API key which required later in our project.

After that we can write code like this.

MAINACTIVITY.JAVA


  1. public class MainActivity extends FragmentActivity {
    // Google Map
    private GoogleMap googleMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    try {
    // Loading map
    initializeMap();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    /**
     * function to load map. If map is not created it will create it for you
     * */
    private void initializeMap() {
    if (googleMap == null) {
    googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    // check if map is created successfully or not
    if (googleMap == null) {
    Toast.makeText(getApplicationContext(),"Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
    }
    }
    }

    @Override
    protected void onResume() {
    super.onResume();
    initializeMap();
    }
    }
XML.JAVA

  1. <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

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

    </RelativeLayout>

Finally we add Manifest.xml file

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

        <permission
            android:name="com.googlemaps.nag.permission.MAPS_RECEIVE"
            android:protectionLevel="signature" />

        <uses-permission android:name="com.googlemaps.nag.permission.MAPS_RECEIVE" />

        <uses-sdk
            android:minSdkVersion="12"
            android:targetSdkVersion="18" />

        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

        <!-- Required to show current location -->
        <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="com.googlemaps.nag.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" />
            <meta-data
                android:name="com.google.android.gms.version"
                android:value="4323000" />  // depending on the version value 
        </application>

    </manifest>


OUTPUT





No comments:

Select DateRange UsingRangePicker.

  /* * This Method is for select range from picker. * */ private fun selectDateRangeUsingRangePicker () { pageNumber = 1 val displ...