Open In App

How to Add Images, Markers in Google Map in Android?

Last Updated : 14 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Google Maps is a mapping service developed by Google. Google Maps offers satellite imagery, real time-time traffic conditions(Google Traffic) street route planning for traveling by foot, car, or public transportation. The service is available on the web and as a mobile application for IOS and Android devices. Google Maps uses a combination of satellite imagery and geographic information system(GIS) data to provide accurate and up-to-date maps around the world.

Types of google maps:

  • Google Maps web service
  • Google Maps mobile app
  • Google Maps Go
  • Google Earth
  • Google Street View
  • Google Maps Platform

Markers

A marker is a graphical icon that represents a location on the map. Markers are commonly used to highlight points of landmarks or other locations.

Step-by-Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2:

Add the SHA key to your cloud console.

 

Step 3:

Copy the API key and paste it into the manifest folder.

 

 

Step 4:

Add this code to your maps activity xml file. We have used fragments in this project.

XML




<?xml version="1.0" encoding="utf-8"?>
<fragment
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/map"
    tools:context=".MapsActivity"
    android:name="com.google.android.gms.maps.SupportMapFragment"/>


Step 5:

Add this code to your main activity. In this, we have used Bitmap for storing images. And many map inbuilt functions for implementing makers and other things to it.

Java




package com.shruti.googlemaps;
  
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;
  
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
  
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.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.GroundOverlayOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolygonOptions;
import com.shruti.googlemaps.databinding.ActivityMapsBinding;
  
import java.io.IOException;
import java.util.ArrayList;
  
public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback{
  
    private GoogleMap mMap;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
  
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().
                findFragmentById(R.id.map);
  
        mapFragment.getMapAsync(this::onMapReady);
    }
  
    @Override
    public void onMapReady(GoogleMap googleMap) {
            mMap = googleMap;
            LatLng alatLng = new LatLng(30.7333,76.7794);
            MarkerOptions markerOptions = new MarkerOptions().position(alatLng).title("Chandigarh");
            mMap.addMarker(markerOptions);
            mMap.moveCamera(CameraUpdateFactory.newLatLng(alatLng));
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(alatLng,16f));            
             
            mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
                @Override
                public void onMapClick(@NonNull LatLng latLng) {
                    MarkerOptions markerOptions = new MarkerOptions().position(latLng).title("");
                    mMap.addMarker(markerOptions);
                    Geocoder geocoder = new Geocoder(MapsActivity.this);
                    try {
                       ArrayList<Address> arrAdr = (ArrayList<Address>) geocoder.getFromLocation(latLng.latitude,latLng.longitude,1);
                        Log.d("Addr",arrAdr.get(0).getAddressLine(0));
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
        
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);          
  
            // Adding any image
            mMap.addGroundOverlay(new GroundOverlayOptions()
                    .position(alatLng,1000f,1000f).image(BitmapDescriptorFactory
                            .fromResource(R.drawable.gfg))
                            .clickable(true));
    }
}


Output:

Note: Only use maps empty activity to implement google maps to it.

Points to Remember:

  1. Add the right SHA key to your cloud console (if the SHA key is not right the map will not show)
  2. Add the right API key to your manifest folder so the connection which is established is correct .


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads