How to Integrate Ola Maps in an Android Application: A Complete Guide

The Good Coding
3 min readJul 21, 2024

--

Ola Maps Integration In Android

Integrating Ola Maps in an Android Application

In this guide, we will walk you through the process of integrating Ola Maps into an Android application. By the end of this tutorial, you will have a working map in your app. Let’s get started!

Prerequisites

Before we begin, ensure you have the following:

Step-by-Step Guide

1. Set Up Your Project

Create a New Project

  • Open Android Studio and create a new project with an empty activity. Name your project whatever you want.

Add the AAR File

  • Download the Ola Maps AAR file from Ola Maps SDK.
  • Place the downloaded AAR file in the libs folder within your app directory.

Configure build.gradle Dependencies

  • Navigate to your project’s settings.gradle file and add the flat directory configuration:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()

// add this
flatDir {
dirs 'libs'
}
}
}
  • Then, open build.gradle (Module: app) and add the dependencies, including the AAR file and other required libraries:
dependencies {
implementation files('libs/olamaps.aar') // Path to the AAR file or Use Absolute Path

// Required for Ola-MapsSdk
implementation 'com.moengage:moe-android-sdk:12.6.01'
implementation 'org.maplibre.gl:android-sdk:10.2.0'
implementation 'org.maplibre.gl:android-sdk-directions-models:5.9.0'
implementation 'org.maplibre.gl:android-sdk-services:5.9.0'
implementation 'org.maplibre.gl:android-sdk-turf:5.9.0'
implementation 'org.maplibre.gl:android-plugin-markerview-v9:1.0.0'
implementation 'org.maplibre.gl:android-plugin-annotation-v9:1.0.0'


implementation "androidx.lifecycle:lifecycle-extensions:2.0.0"
implementation "androidx.lifecycle:lifecycle-compiler:2.0.0"


}
  • After updating the dependencies, click Sync Now in the bar that appears in Android Studio to sync your project with the new dependencies

2. Configure Permissions

Modify AndroidManifest.xml

  • Add the necessary permissions to your AndroidManifest.xml file:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

3. Initialize Ola Maps

Update MainActivity.java

  • Open MainActivity.java and import the required classes. Then, set up OlaMapView and configure the map. Here’s the code:
public class MainActivity extends AppCompatActivity implements MapStatusCallback {

private OlaMapView olaMapView;
MapStatusCallback mapStatusCallback;

OlaMapsConfig olaMapsConfig;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

olaMapView = findViewById(R.id.olaMapView);

OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new AccessTokenInterceptor())
.build();

// Initialize OlaMapView
olaMapView.onCreate(savedInstanceState);

olaMapView.initialize(
mapStatusCallback = this,
olaMapsConfig = new OlaMapsConfig.Builder()
.setApplicationContext(getApplicationContext())
.setClientId(i"YOUR CLIENT ID")
.setMapBaseUrl("https://api.olamaps.io")
.setInterceptor(new AccessTokenInterceptor())
.setMinZoomLevel(3.0)
.setMaxZoomLevel(21.0)
.setZoomLevel(1.0)
.build()
);
}

@Override
public void onMapReady() {
}

@Override
public void onMapLoadFailed(String s) {

}

@Override
protected void onStop() {
super.onStop();
olaMapView.onStop();
}

@Override
protected void onDestroy() {
olaMapView.onDestroy();
super.onDestroy();
}
}

4. Implement Access Token Interceptor

Create AccessTokenInterceptor.java

  • Implement the interceptor to include the API key in your requests:
public class AccessTokenInterceptor implements Interceptor {
private static final String API_KEY_PARAM = "api_key";
private static final String API_KEY_VALUE = "Replace with your actual API key";

@Override
public Response intercept(Chain chain) throws IOException {
Request originalRequest = chain.request();

// Modify the URL to include the API key as a query parameter
HttpUrl urlWithApiKey = originalRequest.url().newBuilder()
.addQueryParameter(API_KEY_PARAM, API_KEY_VALUE)
.build();

Request newRequest = originalRequest.newBuilder()
.url(urlWithApiKey)
.build();

return chain.proceed(newRequest);
}
}

5. Configure Layout

Update activity_main.xml

  • Add the OlaMapView to your layout XML file:
<com.ola.maps.navigation.v5.navigation.OlaMapView
android:id="@+id/olaMapView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

6. Run Your Application

Build and Run

  • Connect an Android device or start an emulator and run your application. You should see the Ola Map with a marker at the specified location.

Conclusion

You’ve now integrated Ola Maps into your Android application! You can further customize your map with additional features such as markers, overlays, and custom navigation controls.

For more details on how to add markers and point locations on Ola Maps, check out our dedicated guide here: How to Add Markers and Point Locations on Ola Maps for Android.

If you have any questions or need further assistance, feel free to leave a comment below or reach out through the support channels.

Thanks for reading! If you found this guide helpful, please give it a thumbs up, share it with others, and don’t forget to follow us for more tutorials and updates.

--

--