How to Add Custom Marker to Google Maps in Android? - GeeksforGeeks

24 Jan, 2021
Google Maps is used in many apps for displaying a location and indicating a specific location on a map. We have seen the use of maps in many apps that provides services such as Ola, Uber, and many more. In these apps, you will get to see How we can add Custom Marker to Google Maps in Android. 
We will be building a simple application in which we will display a map and on that map, we will be displaying a custom marker on our app. Below is the screenshot in which we will get to see what we are going to do in this project. Note that we are going to implement this project using the Java language. 
Step 1: Create a New Project
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. Make sure to select Maps Activity while creating a new Project.
Step 2: Generating an API key for using Google Maps
To generate the API key for Maps you may refer to How to Generate API Key for Using Google Maps in Android. After generating your API key for Google Maps. We have to add this key to our Project. For adding this key in our app navigate to the values folder > google_maps_api.xml file and at line 23 you have to add your API key in the place of YOUR_API_KEY. After adding this now we are ready for adding the custom marker to our app. After adding the API key, you can run your app and you will get to see a default marker on the Sydney location with the default marker. 
Step 3: Adding a custom marker in Google Maps
For adding a custom marker to Google Maps navigate to the app > res > drawable > Right-Click on it > New > Vector Assets and select the icon which we have to show on your Map. You can change the color according to our requirements. After creating this icon now we will move towards adding this marker to our Map. 
Step 4: Working with the MapsActivity.java file
Go to the MapsActivity.java file and refer to the following code. Below is the code for the MapsActivity.java file. Comments are added inside the code to understand the code in more detail.
Java
import android.content.Context;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.drawable.Drawable;import android.os.Bundle;  import androidx.core.content.ContextCompat;import androidx.fragment.app.FragmentActivity;  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.BitmapDescriptor;import com.google.android.gms.maps.model.BitmapDescriptorFactory;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);    }      /**     * Manipulates the map once available.     * This callback is triggered when the map is ready to be used.     * This is where we can add markers or lines, add listeners or move the camera. In this case,     * we just add a marker near Sydney, Australia.     * If Google Play services is not installed on the device, the user will be prompted to install     * it inside the SupportMapFragment. This method will only be triggered once the user has     * installed Google Play services and returned to the app.     */    @Override    public void onMapReady(GoogleMap googleMap) {        mMap = googleMap;          // Add a marker in Sydney and move the camera        LatLng sydney = new LatLng(-34, 151);        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")                // below line is use to add custom marker on our map.                .icon(BitmapFromVector(getApplicationContext(), R.drawable.ic_flag)));        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));    }      private BitmapDescriptor BitmapFromVector(Context context, int vectorResId) {        // below line is use to generate a drawable.         Drawable vectorDrawable = ContextCompat.getDrawable(context, vectorResId);                  // below line is use to set bounds to our vector drawable.         vectorDrawable.setBounds(0, 0, vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight());                  // below line is use to create a bitmap for our        // drawable which we have added.        Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);                  // below line is use to add bitmap in our canvas.         Canvas canvas = new Canvas(bitmap);                  // below line is use to draw our         // vector drawable in canvas.         vectorDrawable.draw(canvas);                  // after generating our bitmap we are returning our bitmap.         return BitmapDescriptorFactory.fromBitmap(bitmap);    }}
Now run your app and see the output of the app. 
Note: In the Google Developer Console (https://console.developers.google.com), ensure that the “Google Maps Android API v2” is enabled. And also ensure that your API Key exists.
android
Technical Scripter 2020
Android
Java
Technical Scripter
Java
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Create and Add Data to SQLite Database in Android?
Resource Raw Folder in Android Studio
Broadcast Receiver in Android With Example
Services in Android with Example
Android RecyclerView in Kotlin
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples