Google Maps is used in many Android applications. While using Google Maps there are many modifications which you will get to see while using Maps in this apps. When we have used Google Maps in different apps such as OLA and Uber we will get to see lines and routes drawn on our Maps. In this article, we will take a look at drawing Polyline on Google Maps in Android.
What we are going to build in this article?
We will be building a simple application in which we will display our Map. On this map, we will display a polygon between three locations as Brisbane, Tamworth, and Newcastle. A sample image is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.

Step by Step Implementation
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.
Step 3: Adding Polyline on Google Maps in Android
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.graphics.Color;
import android.os.Bundle;
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.LatLng;
import com.google.android.gms.maps.model.PolylineOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
LatLng TamWorth = new LatLng(- 31.083332 , 150.916672 );
LatLng NewCastle = new LatLng(- 32.916668 , 151.750000 );
LatLng Brisbane = new LatLng(- 27.470125 , 153.021072 );
@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 );
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.addPolyline(( new PolylineOptions()).add(Brisbane, NewCastle, TamWorth, Brisbane).
width( 5 )
.color(Color.RED)
.geodesic( true ));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Brisbane, 13 ));
}
}
|
After adding this code. Now run your app and see the output of the app.
Output:
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.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
24 Jan, 2021
Like Article
Save Article