View video tutorial
ANDROID Google Map
ANDROID
Google Maps helps the user get geolocation information and it increases the productivity of an application.
Android Google Map
➔ Implementing Google Maps in an Android app requires setting up a Google Cloud project to obtain an API key.
➔ The process of implementing the Google Maps API in an application involves some required dependencies, permissions, and coding.
- Login to Google account. Or try https://accounts.google.com/ in your browser.
- Create Google Cloud project. Google Cloud project Or try https://console.cloud.google.com/ in your browser.
- Create API key.
- Enable Maps SDK for Android.
- Add library dependencies to the project in build.gradle (module:app).
- Use permissions in the AndroidManifest.xml file.
- Use the API key as meta-data in the AndroidManifest.xml file.
- Create a fragment element to hold the Google Map.
- Add the necessary code to implement GoogleMaps.
➔ The Google Maps API in an Android app requires an active internet connection to download and display map tiles, render maps, and access real-time data.
➔ Libraries dependencies build.gradle (module:app) file
dependencies {
//other dependencies here..
implementation("com.google.android.gms:play-services-maps:19.0.0")
implementation("com.google.maps.android:maps-ktx:5.1.1")
}
Libraries dependencies
➔ Use permissions in the AndroidManifest.xml file
<!-- Other permissions go here.. -->
<uses-permission android:name="android.permission.INTERNET" />
Permissions in the AndroidManifest.xml
➔ Use the API key as meta-data in the AndroidManifest.xml file
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="" />
Use the API key in the AndroidManifest.xml file
➔ Create a fragment element in Activity Layout activity_main.xml file
<fragment
android:id="@+id/mymap"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
➔ Layout activity_main.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/mymap"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
➔ Activity class MainAcitivy.java file
package com.example.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
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 MainActivity extends AppCompatActivity implements OnMapReadyCallback{
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.mymap);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng myloc = new LatLng(20.5, 78.96);
mMap.addMarker(new MarkerOptions().position(myloc).title("Marker in My Location"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(myloc));
}
}
Google Map display on android device.