With the Mobile Maps SDK, you can embed interactive, highly customizable maps into your Android mobile applications. Our maps are built on commercial vehicle-specific data from Trimble Maps, which allows you to plan and visualize safe, legal, and efficient routes for all types of vehicles, around the world. With features ranging from traffic data to weather alerts and road conditions, our Maps SDK is ideal for any application that demands maps for work.
This guide is intended to get you on your way to incorporating Trimble’s mapping library into your mobile project. It covers the key concepts needed to install, display, and customize maps. You can also view sample projects and code in our public GitHub repository.
Installation
In order to use the Maps SDK, you will need to add it as a dependency to your project. The library can be accessed from a Maven repository. To add the dependency, configure your build to download the Maps SDK:
Open your project in Android Studio
Open up your project-levelsettings.gradle file (settings.gradle.kts file in Kotlin)
If there is already a dependencyResolutionManagement block in the build.gradle file, copy the Trimble Maps Repository in there:
You can use the Manifest merge feature to reduce the need to include any SDK requirements in your application’s manifest file. You’ll need to add either the Fine or Coarse location permission to your manifest if you plan to display a user’s location on the map or get the user’s location information, and the Background location permission to continue it while the app is in the background. The Permissions should be added in AndroidManifest.xml under the manifests folder.
The user’s location permission should be checked during runtime using the PermissionsManager.
importandroid.os.Bundle;importandroidx.annotation.NonNull;importandroidx.appcompat.app.AppCompatActivity;importcom.trimblemaps.account.LicensedFeature;importcom.trimblemaps.account.TrimbleMapsAccountManager;importcom.trimblemaps.account.models.TrimbleMapsAccount;importcom.trimblemaps.android.core.permissions.PermissionsListener;importcom.trimblemaps.android.core.permissions.PermissionsManager;importcom.trimblemaps.mapsdk.TrimbleMaps;importcom.trimblemaps.mapsdk.maps.MapView;importcom.trimblemaps.mapsdk.maps.OnMapReadyCallback;importcom.trimblemaps.mapsdk.maps.Style;importcom.trimblemaps.mapsdk.maps.TrimbleMapsMap;importorg.jetbrains.annotations.NotNull;importjava.util.List;importkotlin.collections.ArraysKt;importkotlin.jvm.internal.Intrinsics;publicfinalclassExampleextendsAppCompatActivity{privatefinalPermissionsManagerpermissionsManager=newPermissionsManager(null);privatefinalPermissionsListenerpermissionsListener=newPermissionsListener(){@OverridepublicvoidonExplanationNeeded(List<String>permissionsToExplain){}@OverridepublicvoidonPermissionResult(booleangranted){if(granted&&PermissionsManager.isAndroidElevenPlus()&&!PermissionsManager.areLocationPermissionsGranted(Example.this)){permissionsManager.requestBackgroundLocationPermissions(Example.this);}}};protectedvoidonCreate(@NullableBundlesavedInstanceState){super.onCreate(savedInstanceState);this.setContentView(R.layout.activity_example);TrimbleMapsAccountaccount=TrimbleMapsAccount.builder()// See next section for more info about Authentication.apiKey("Your-API-key-here").addLicensedFeature(LicensedFeature.MAPS_SDK).build();TrimbleMapsAccountManager.initialize(account);TrimbleMapsAccountManager.awaitInitialization();TrimbleMaps.getInstance(this.getApplicationContext());if((!PermissionsManager.isAndroidElevenPlus()||ContextCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_BACKGROUND_LOCATION)!=0)&&ContextCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_FINE_LOCATION)!=0){this.permissionsManager.setListener(permissionsListener);this.permissionsManager.requestLocationPermissions(this);}else{// Start activity}}publicvoidonRequestPermissionsResult(intrequestCode,@NotNullString[]permissions,@NotNullint[]grantResults){Intrinsics.checkNotNullParameter(permissions,"permissions");Intrinsics.checkNotNullParameter(grantResults,"grantResults");super.onRequestPermissionsResult(requestCode,permissions,grantResults);if(PermissionsManager.isAndroidElevenPlus()){if(ArraysKt.contains(permissions,android.Manifest.permission.ACCESS_FINE_LOCATION)&&!PermissionsManager.areLocationPermissionsGranted(Example.this)){permissionsManager.requestBackgroundLocationPermissions(Example.this);}elseif(ArraysKt.contains(permissions,android.Manifest.permission.ACCESS_BACKGROUND_LOCATION)){// Start activity}}elseif(permissions.length!=0&&ArraysKt.contains(permissions,android.Manifest.permission.ACCESS_FINE_LOCATION)){// Start activity}}
importandroid.app.Activityimportandroid.os.Bundleimportandroidx.core.content.ContextCompatimportcom.trimblemaps.account.LicensedFeatureimportcom.trimblemaps.account.TrimbleMapsAccountManagerimportcom.trimblemaps.account.models.TrimbleMapsAccountimportcom.trimblemaps.android.core.permissions.PermissionsListenerimportcom.trimblemaps.android.core.permissions.PermissionsManagerimportcom.trimblemaps.mapsdk.TrimbleMapsimportcom.trimblemaps.mapsdk.maps.MapViewimportcom.trimblemaps.mapsdk.maps.OnMapReadyCallbackimportcom.trimblemaps.mapsdk.maps.StyleclassExample:AppCompatActivity(){privatevalpermissionsManager=PermissionsManager(null)privatevalpermissionsListener=object:PermissionsListener{overridefunonExplanationNeeded(permissionsToExplain:MutableList<String>?){}overridefunonPermissionResult(granted:Boolean){if(granted&&PermissionsManager.isAndroidElevenPlus()&&!PermissionsManager.areLocationPermissionsGranted(this@Example)){permissionsManager.requestBackgroundLocationPermissions(this@Example)}}}overridefunonCreate(savedInstanceState:Bundle?){super.onCreate(savedInstanceState)setContentView(R.layout.activity_example)valaccount=TrimbleMapsAccount.builder()// See next section for more info about Authentication
.apiKey("Your-API-key-here").addLicensedFeature(LicensedFeature.MAPS_SDK).build()TrimbleMapsAccountManager.initialize(account)TrimbleMapsAccountManager.awaitInitialization()TrimbleMaps.getInstance(applicationContext)if((PermissionsManager.isAndroidElevenPlus()&&ContextCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_BACKGROUND_LOCATION)==0)||ContextCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_FINE_LOCATION)==0){// Start activity
}else{permissionsManager.listener=permissionsListenerpermissionsManager.requestLocationPermissions(this)}}overridefunonRequestPermissionsResult(requestCode:Int,permissions:Array<String>,grantResults:IntArray){super.onRequestPermissionsResult(requestCode,permissions,grantResults);permissionsManager.onRequestPermissionsResult(requestCode,permissions,grantResults)if(PermissionsManager.isAndroidElevenPlus()){if(permissions.isNotEmpty()&&permissions.contains(android.Manifest.permission.ACCESS_BACKGROUND_LOCATION)){// Start activity
}}elseif(permissions.isNotEmpty()&&permissions.contains(android.Manifest.permission.ACCESS_FINE_LOCATION)){// Start activity
}}}
In order to use the APIs and SDK, you will need to authenticate your API key first. If you do not have an API key, you can request one. Authentication must be done prior to attempting to render a MapView or using the APIs. An example can be found below:
// Authorize the API key for the session.// .apiKey() requires your Trimble Maps API keyTrimbleMapsAccounttrimbleMapsAccount=TrimbleMapsAccount.builder().apiKey("Your-API-key-here").addLicensedFeature(LicensedFeature.MAPS_SDK).build();// Initialize the sessionTrimbleMapsAccountManager.initialize(trimbleMapsAccount);TrimbleMapsAccountManager.awaitInitialization();
// Authorize the API key for the session.
// .apiKey() requires your Trimble Maps API key
valtrimbleMapsAccount=TrimbleMapsAccount.builder().apiKey(getString("Your-API-key-here")).addLicensedFeature(LicensedFeature.MAPS_SDK).build()// Initialize the session
TrimbleMapsAccountManager.initialize(trimbleMapsAccount)TrimbleMapsAccountManager.awaitInitialization()
Once authentication is complete, you can start to use the SDK. To see authentication in conjunction with displaying a map, see Displaying a basic map.
Connecting to a proxy server (optional)
Prior to initializing the session, you can create a connection to a proxy server and pass it into the initialize function. The code below demonstrates how to set up a proxy.
To add a map to your application, the Maps SDK provides the MapView class. This provides out-of-the-box capability to display a map with street names, road information etc. Additionally, the map can be styled, moved, rotated etc.
Interaction with the map itself is done through the TrimbleMapsMap object. This can be retrieved using the getMapAsync() method on the MapView.