Google Map Tutorial in Android Studio [Step by Step] - Android Hire
Coding Tutorials

Google Map Tutorial in Android Studio [Step by Step]

Vidhi Markhedkar
Google Maps is one of the many applications bundled with the Android platform. In Addition, to simply using the Maps application, you can also embed it into your own applications and make it do some very cool things. This section describes how to use Google Maps in your Android applications and programmatically perform the following:
  • Displaying Google Map.
  • Change the type of Google Maps.
  • Add markers to Google Maps.

1. Creating the Project in Android Studio

To get started, we need to create an Android project and select Google maps activity.

Google Map Activity

2. Steps for getting the Google Map API key:

Step 1: Copy the URL from the google_map_api.xml file to generate a Google map API key.

Step 2: When we paste the URL in the browser, it will open the page. Then click CONTINUE => Create API key. The API Key will be generated.

Step 3: Copy this API key to the element in the google_maps_api.xml file

3. Example to Access Google Maps in Android Studio:

We need to modify AndroidManifest.xml file by adding some user permission like:

1)INTERNET: To determine if we are connected to the internet or not.
2)ACCESS_FINE_LOCATION: To determine a user's location using GPS.
3)ACCESS_COARSE_LOCATION: To determine user"s location using Wi-Fi and mobile data.

AndroidManifext.xml


      
    
    
     
   
   
   
   
    
   
  
   
  
activity_maps.xml

We are going to implement the android MapView in a Fragment and subsequently add the Fragment to the MainActivity class.

MapsActivity.java

In MapsActivity.java class to get the Google map object, we need to implement the OnMapReadyCallback interface and override the onMapReady() callback method, these are called when the map is ready to be used.

Changing the Map Type: There are four different types of map Normal, Hybrid, Satellite, and terrain. We can use them as below:

MAP_TYPE_NORMAL : The map includes a road map with street names and labels.

MAP_TYPE_SATELLITE: This type of map has a Satellite View without street names and labels.

MAP_TYPE_TERRAIN: The map includes colors, lines, and labels. Some roads and labels are also visible.

MAP_TYPE_HYBRID : This map type is the combination of a satellite View Area and Normal mode displaying satellite images of an area with all labels.

googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN)

Zoom Controls: We can control Zooming gestures on the map by using built-in setZoomControlsEnabled() function. These can be enabled by calling:

googleMap.getUiSettings().setZoomGesturesEnabled(true); // false to disable
Add Marker on Map: We can place a marker to display location on the map by addMarker() method.
LatLng delhi= new LatLng(28.7,77.1);
mMap.addMarker(new MarkerOptions().position(delhi).title("Marker in Delhi"));


MapsActivity.java

package com.example.dell.displayinggooglemaps;
import android.support.v4.app.FragmentActivity; import android.os.Bundle; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.MarkerOptions; public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { private GoogleMap mMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps); // Obtain the SupportMapFragment and get notified when the map is ready to be used. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); } @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; LatLng delhi = new LatLng(28.7,77.1); mMap.addMarker(new MarkerOptions().position(delhi).title("Marker in Delhi")); mMap.moveCamera(CameraUpdateFactory.newLatLng(delhi)); } }
build.gradle

In the app build.gradle file, we will add a Map dependency library. The modified version of the file is as shown:

apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
defaultConfig {
applicationId "com.example.dell.displayinggooglemaps"
minSdkVersion 19
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.google.android.gms:play-services-maps:11.0.0'
testCompile 'junit:junit:4.12'
}

That’s all for accessing google maps in an android tutorial. Now if you run this demo, you will get the output as shown above.

Vidhi Markhedkar's profile picture

Vidhi Markhedkar

Android developer and writer with a passion for creating innovative and user-friendly apps.

Related Posts

7 Top Samsung Galaxy Ring Alternatives for 2025

7 Top Samsung Galaxy Ring Alternatives for 2025

Tired of waiting for the Samsung Galaxy Ring to hit the market? You’re not alone. While the buzz around Samsung’s smart ring continues, a plethora of impressive alternatives is already available. These devices deliver advanced health tracking, stylish designs, and unique features that cater to various lifestyles. The current lineup of smart rings caters to […]

What Is Quiet Mode on Instagram and How to Activate It

What Is Quiet Mode on Instagram and How to Activate It

Ever wondered what Quiet Mode on Instagram is all about? This simple yet powerful Instagram feature helps you take a break from the constant buzz of notifications and focus on what truly matters. Whether you’re striving for better work-life balance, dedicating time to studying, or simply trying to disconnect from social media distractions, Quiet Mode […]

How to Make a Bed in Minecraft (Step-by-Step Guide)

How to Make a Bed in Minecraft (Step-by-Step Guide)

A bed in Minecraft is very important. It lets you skip the night and set your spawn point, so if you die, you will return to your bed instead of the original world spawn. This guide will show you how to gather materials, craft a bed, and set your spawn point. We’ll also show you how to customize your bed, build bunk […]

10 Best MMORPG Games For Android

10 Best MMORPG Games For Android

Not too long ago, MMORPG games and powerful gaming consoles were mostly exclusive to PCs. They required high-end graphics and systems to deliver their immersive gameplay. But times have changed. With the rise of gaming-oriented smartphones, you can now enjoy PC-like gaming experiences on your Android device. Thanks to these technological advancements and faster internet […]

Roblox: Fruit Battlegrounds codes (January 2025)

Roblox: Fruit Battlegrounds codes (January 2025)

Fruit Battlegrounds codes are all about getting more gems and help you to shoot up your rank in this One Piece anime-inspired game. This Fruit Battlegrounds was made by Popo developer. It is an action-packed game where players battle it out using unique fruit-based abilities. With constant updates, new fruits, and exciting challenges, it’s a fruity frenzy you won’t […]

Roblox: Ultimate Football Codes (January 2025)

Roblox: Ultimate Football Codes (January 2025)

Want to get some extra items for Ultimate Football in Roblox? You’ve come to the right place! Here’s the latest list of codes to help you score touchdowns and look stylish on the field. These codes offer free rewards such as coins and cosmetics to enhance your gameplay. What are Ultimate Football Codes? Ultimate Football […]

Roblox: Da Hood Codes (January 2025)

Roblox: Da Hood Codes (January 2025)

Are you a fan of Roblox games, in this article we will look at the Roblox Da Hood Codes for December 2024 that will help you unlock exclusive items, improve your gameplay and dominate the streets of Da Hood. You can feel that the game is inspired by the Grand Theft Auto series and there […]