Saturday, 10 October 2015

Google Map integration

In this post we will be having a look on how to integrate Google Maps in android. Here in this post, Google Map is integrating using dynamic fragment.


First Create project on https://console.developers.google.com/ and create a Android key using your SHA1 and Keystore (whether debug or release).

See Below Screenshots as reference for creating a Google web console project. And get your Map API key.










1. MapActivity

import android.location.Geocoder;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;

public class MapActivity extends FragmentActivity {
    private GoogleMap googleMap;
    private SupportMapFragment fragment;

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

    private void setupDynamicMapFragment() {
        FragmentManager fm = this.getSupportFragmentManager();
        fragment = (SupportMapFragment) fm.findFragmentById(R.id.map_container);
        if (fragment == null) {
            fragment = SupportMapFragment.newInstance();
            fm.beginTransaction().replace(R.id.map_container, fragment).commit();
        }
    }

    /*** function to load map. If map is not created it will create it for you     */   
 private void initializeMap() {
        LatLng location = null;
        if (googleMap == null) {
            googleMap = fragment.getMap();
            if (googleMap != null) {
                    location = new LatLng(30.7500,76.7800);
                if (location != null) {
                    CameraPosition cameraPosition = new CameraPosition.Builder().target(
                            location).zoom(12).build();
                    googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
                }
            }
        }
    }

//initializing Google Maps on resume in order to let the SupportMapfragment get initialized 
 @Override    protected void onResume() {
        super.onResume();
        initializeMap();
    }
}

2. activity_map.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"
    android:id="@+id/map_container"
    tools:context=".MapActivity">

    <!-- The map fragments will go here -->
</RelativeLayout>


3. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest 
  xmlns:android="http://schemas.android.com/apk/res/android"    
  package="com.Googlemapdemo" >
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <!--  The ACCESS_COARSE/FINE_LOCATION 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" />

    <!-- Required OpenGL ES 2.0. for Maps V2 -->    
<uses-feature        
 android:glEsVersion="0x00020000"
 android:required="true" />

  <application
    android:allowBackup="true"  
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" >
     <activity        
     android:name=".MapActivity"
     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="@string/google_maps_key" />

 </application>

</manifest>


4. strings.xml


<resources>
    <string name="app_name">MapDemo</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="google_maps_key">YOUR MAP KEY</string>
</resources>

5. build.gradle
apply plugin: 'com.android.application'
android {
    compileSdkVersion 23 
    buildToolsVersion "23.0.0 rc2"
    defaultConfig {
        applicationId "com.mapdemo"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'com.google.android.gms:play-services:7.5.0'}


Output

No comments:

Post a Comment