Download Code

Android Google Map

In this tutorial you will learn how to develop android google map in android application.

Google Map

You can learn more about Google Map in detail from Google Official Documentation or Android Official Documentation.

Alright let’s start.Create android application project.

First you will download Google_Play_Sevices library.Go to Window > Android SDK Manager and hit install.

android google map,map,maps,google maps,google map,google maps android,google maps in android,google map android tutorial,map android,fragment,supportmapfragment android,supportmapfragment,map android tutorial code,map android code,google map api key,how to generate map api key for android,google play services,fragment,support map fragment

Now import into workspace from your SDK directory.

You will generate Google Map API key which you will use to load maps.For that purpose go to Google Developer Console and Sign In.

Create new project by clicking API Project option on right top corner.

android google map,map,maps,google maps,google map,google maps android,google maps in android,google map android tutorial,map android,fragment,supportmapfragment android,supportmapfragment,map android tutorial code,map android code,google map api key,how to generate map api key for android,google play services,fragment,support map fragment

After creating project it will take you to the api’s screen.Now you will see Google Maps Android API Under Google Maps API.Click on Google Maps Android API and enable it.

When you enabled Android Maps API then generate API Key.Go to Credentials in the left hand side Navigation.

When you click Credentials it will show you a window to generate key.Create Credentials > API Key.Click API Key.

android google map,map,maps,google maps,google map,google maps android,google maps in android,google map android tutorial,map android,fragment,supportmapfragment android,supportmapfragment,map android tutorial code,map android code,google map api key,how to generate map api key for android,google play services,fragment,support map fragment

New window will appear select Android Key.

android google map,map,maps,google maps,google map,google maps android,google maps in android,google map android tutorial,map android,fragment,supportmapfragment android,supportmapfragment,map android tutorial code,map android code,google map api key,how to generate map api key for android,google play services,fragment,support map fragment

Enter project name,package name( android project package) don’t forget this API key will run with that package which you will enter here.Next is SHA-1.To generate SHA-1 follow the steps.

Open your Command Prompt and locate this path using Command Prompt.

Path ( C:\Program Files\Java\jdk1.8.0_45\bin).

After paste this keytool query.

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

Hit enter.You will find SHA-1 copy and paste in project fingerprint and hit create.Copy that api key and keep it safe.

Go to activity_main.xml in your project and add Fragment.

android google map,map,maps,google maps,google map,google maps android,google maps in android,google map android tutorial,map android,fragment,supportmapfragment android,supportmapfragment,map android tutorial code,map android code,google map api key,how to generate map api key for android,google play services,fragment,support map fragment

Next you need to import google_play_service_lib into the project.Right click on project > Properties > Android > import.

android google map,map,maps,google maps,google map,google maps android,google maps in android,google map android tutorial,map android,fragment,supportmapfragment android,supportmapfragment,map android tutorial code,map android code,google map api key,how to generate map api key for android,google play services,fragment,support map fragment

Now go to your MainActivity.java class.Do the Following steps.

  • Create variable of Google Map class.
  • Create a method name initializeMap().
  • Check if googleMap is null else refer the fragment in xml.
  • Set Latitude and Longitude.
  • Set MarkerOption with Latitude,Longitude and Title.
  • Set camera position to that particular co-ordinates.
  • And add marker to google map.
package developer.codecmap;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;


public class MainActivity extends Activity {
	// Google Map
	private GoogleMap googleMap;

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

		try {
			// Loading map
			initilizeMap();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * function to load map. If map is not created it will create it for you
	 * */
	private void initilizeMap() {
		if (googleMap == null) {
			googleMap = ((MapFragment) getFragmentManager().findFragmentById(
					R.id.map)).getMap();
			// latitude and longitude
			double latitude = 33.601091 ;
			double longitude = 73.048278;

			// create marker
			MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Android Devevlopers ");

			CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(latitude, longitude)).zoom(12).build();

			googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));


			// adding marker
			googleMap.addMarker(marker);
			// 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();
		initilizeMap();
	}

}

Next go to your Android Manifest.xml file.Add permission and map api key.

android google map,map,maps,google maps,google map,google maps android,google maps in android,google map android tutorial,map android,fragment,supportmapfragment android,supportmapfragment,map android tutorial code,map android code,google map api key,how to generate map api key for android,google play services,fragment,support map fragment

Now run your android application project.Screen shows android google map example.

android google map,map,maps,google maps,google map,google maps android,google maps in android,google map android tutorial,map android,fragment,supportmapfragment android,supportmapfragment,map android tutorial code,map android code,google map api key,how to generate map api key for android,google play services,fragment,support map fragment

That’s it for this tutorial.

Thanks.