Asynchronous Account Initialization
Contents
The Mobile Maps SDK examples require that you first complete the initial project set-up.
Asynchronous Account Initialization with callback methods
activity_sample_basic_map.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent">
<com.trimblemaps.mapsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
SampleMapWithCallbackAuthorizationActivity.java
Before running the Java or Kotlin code, the theme needs to be set in the Theme.xml
file as shown below.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.AsyncAcctInit" parent="Theme.AppCompat" />
</resources>
package com.trimblemaps.navigationexamples;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.trimblemaps.account.LicensedFeature;
import com.trimblemaps.account.TrimbleMapsAccountManager;
import com.trimblemaps.account.models.TrimbleMapsAccount;
import com.trimblemaps.mapsdk.TrimbleMaps;
import com.trimblemaps.mapsdk.maps.MapView;
import com.trimblemaps.mapsdk.maps.OnMapReadyCallback;
import com.trimblemaps.mapsdk.maps.Style;
import com.trimblemaps.mapsdk.maps.TrimbleMapsMap;
public class SampleMapWithCallbackAuthorizationActivity extends AppCompatActivity {
private MapView mapView;
private TrimbleMapsMap map;
private final TrimbleMapsAccountManager.AccountInitializationListener onAccountInitializationCallback =
new TrimbleMapsAccountManager.AccountInitializationListener() {
@Override
public void onAccountStatusChanged(AccountStatus accountStatus) {
if (accountStatus == AccountStatus.LOADED) {
Handler mainHandler = new Handler(Looper.getMainLooper());
// Execute your code on the main thread
runOnUiThread(new Runnable() {
@Override
public void run() {
// Assuming TrimbleMaps is a class with a method getInstance()
TrimbleMaps.getInstance(getApplicationContext());
setContentView(R.layout.activity_sample_basic_map);
// Set up the MapView from the layout
mapView = findViewById(R.id.mapView);
// the onMapReadyCallback is fired when the map is ready to be worked with
mapView.getMapAsync(trimbleMapsMap -> {
// The TrimbleMapsMap object is created, now a style can be applied to render a map.
trimbleMapsMap.setStyle(Style.MOBILE_DAY);
});
}
});
}
}
@Override
public void onAccountStatusFailed(AccountStatus accountStatus, String s) {
// Handle failure case
}
@Override
public void onLicensingError(List<LicensedFeature> licensedFeatures) {
// Handle error case
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Authorize the api key for the session.
// .apiKey() requires your Trimble Maps API key
TrimbleMapsAccount trimbleMapsAccount = TrimbleMapsAccount.builder()
.apiKey("YOUR API KEY")
.addLicensedFeature(LicensedFeature.MAPS_SDK)
.build();
AccountInitializationOptions accountInitializationOptions = AccountInitializationOptions.builder()
.callback(onAccountInitializationCallback)
.build();
// Initialize the session with callback
TrimbleMapsAccountManager.initialize(trimbleMapsAccount, accountInitializationOptions);
}
/**
* Activity Overrides
*/
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
public void onStart() {
super.onStart();
if (mapView != null) {
mapView.onStart();
}
}
@Override
public void onResume() {
super.onResume();
if (mapView != null) {
mapView.onResume();
}
}
@Override
public void onStop() {
super.onStop();
if (mapView != null) {
mapView.onStop();
}
}
@Override
public void onPause() {
super.onPause();
if (mapView != null) {
mapView.onPause();
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (mapView != null) {
mapView.onDestroy();
}
}
}
package com.trimblemaps.navigationexamples
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.trimblemaps.account.AccountInitializationOptions
import com.trimblemaps.account.AccountStatus
import com.trimblemaps.account.LicensedFeature
import com.trimblemaps.account.TrimbleMapsAccountManager
import com.trimblemaps.account.models.TrimbleMapsAccount
import com.trimblemaps.mapsdk.TrimbleMaps
import com.trimblemaps.mapsdk.maps.MapView
import com.trimblemaps.mapsdk.maps.OnMapReadyCallback
import com.trimblemaps.mapsdk.maps.Style
import com.trimblemaps.mapsdk.maps.TrimbleMapsMap
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.util.concurrent.Executors
class SampleMapWithCallbackAuthorizationActivity : AppCompatActivity() {
private var mapView: MapView? = null
private var map: TrimbleMapsMap? = null
private val dispatcher: CoroutineDispatcher =
Executors.newFixedThreadPool(1).asCoroutineDispatcher()
private val onAccountInitializationCallback =
object : TrimbleMapsAccountManager.AccountInitializationListener {
override fun onAccountStatusChanged(accountStatus: AccountStatus?) {
if (accountStatus == AccountStatus.LOADED) {
MainScope().launch {
withContext(dispatcher) {
launch(Dispatchers.Main) {
TrimbleMaps.getInstance(applicationContext)
setContentView(R.layout.activity_sample_basic_map)
// Set up the MapView from the layout
mapView = findViewById(R.id.mapView)
// the onMapReadyCallback is fired when the map is ready to be worked with
mapView?.getMapAsync(OnMapReadyCallback { trimbleMapsMap ->
// The TrimbleMapsMap object is created, now a style can be applied to render a map.
trimbleMapsMap.setStyle(Style.MOBILE_DAY) {
// The style is loaded, you can add content to the map, move it etc.
}
})
}
}
}
}
}
override fun onAccountStatusFailed(
accountStatus: AccountStatus?,
errorMessage: String?
) {
if (accountStatus == AccountStatus.FAILED) {
// Handle failure case
}
}
override fun onLicensingError(licensedFeatures: MutableList<LicensedFeature>?) {
// Handle error case
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Authorize the api key for the session.
// .apiKey() requires your Trimble Maps API key
val trimbleMapsAccount = TrimbleMapsAccount.builder()
.apiKey("Your-API-key-here")
.addLicensedFeature(LicensedFeature.MAPS_SDK)
.build()
val accountInitializationOptions = AccountInitializationOptions.builder()
.callback(onAccountInitializationCallback)
.build()
// Initialize the session with callback
TrimbleMapsAccountManager.initialize(trimbleMapsAccount, accountInitializationOptions)
}
/**
* Activity Overrides
*/
override fun onStart() {
super.onStart()
mapView?.onStart()
}
override fun onResume() {
super.onResume()
mapView?.onResume()
}
override fun onPause() {
super.onPause()
mapView?.onPause()
}
override fun onStop() {
super.onStop()
mapView?.onStop()
}
override fun onLowMemory() {
super.onLowMemory()
mapView?.onLowMemory()
}
override fun onDestroy() {
super.onDestroy()
mapView?.onDestroy()
}
}