2 · truemetrics module
Option 2 · The last meter module in your app
An Android library that shows a stop’s last meter inside your courier app, with point-to-point navigation off or on.
The truemetrics last meter module (io.truemetrics:navigation) is the screen of the
truemetrics standalone navigation app as an Android library: the map, the last
meter, the routing and the best practices, inside your own app. One stop at a time.
Point-to-point navigation can be off or on, with one switch, routing:
Navigation off routing = false (default) | Navigation on routing = true | |
|---|---|---|
| For | Apps that navigate with something else (Google Maps, their own) | Apps that want navigation inside, made for the last meter |
| The map shows | Parking, entrance, walk path, building, and the courier’s position as a dot, always in one frame | The route to the waypoint and into the parking, turn-by-turn, then parking, entrance, walk path, building |
| Route, guidance, rerouting | No | Yes (OpenStreetMap, routed by truemetrics) |
| Foreground service | No | While driving on the device’s position, so the drive goes on with the phone locked or another app in front |
| Screen kept on | No | While navigating |
| Ends | With Back | On arrival (if configured) or with Back |
Without last-meter data for a stop, the map falls back to the destination and highlights the building at it.
Requirements
- Android app in Kotlin or Java,
minSdk24 or higher,compileSdk37, Kotlin 2.3 or newer (the library bringskotlin-stdlib2.4). - The screen that shows the module is a
FragmentActivity(anAppCompatActivityis one). - An API key from truemetrics, enabled for the module.
- Last-meter data: the truemetrics SDK recording in your courier app with
address_idin the metadata (see Integrate with an AI agent).
Flutter or React Native apps: show the module from a small native Android part
(NavigationContract, see step 4), or use the standalone app,
which needs no native code at all.
Integration
1. Add the dependency
The repository is the one the truemetrics SDK comes from; the current version comes from your truemetrics contact.
dependencyResolutionManagement {
repositories {
google()
mavenCentral()
maven { url = uri("https://github.com/TRUE-Metrics-io/truemetrics_android_SDK_p_maven/raw/main") }
}
}dependencies {
implementation("io.truemetrics:navigation:<version>")
}The library’s manifest merges into your app: INTERNET, ACCESS_NETWORK_STATE,
ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION, FOREGROUND_SERVICE,
FOREGROUND_SERVICE_LOCATION, POST_NOTIFICATIONS, its activity and its foreground service.
Remove none of them.
2. Initialise once
In Application.onCreate, on the main thread. After Android killed the process it brings an open
navigation back directly, and only Application.onCreate runs before it.
class MyApp : Application() {
override fun onCreate() {
super.onCreate()
TruemetricsNavigation.init(this, apiKey = BuildConfig.TRUEMETRICS_API_KEY)
}
}3. Describe the tour and the stop
One NavigationRequest each time a stop is opened: the whole tour and which of its stops comes
next, the same as the standalone app’s handover. Only destination is
required; a stop’s id brings its last meter.
val request = NavigationRequest(
destination = LatLon(stop.lat, stop.lon), // of the stop at nextStop
stops = tour.map {
NavigationStop(
id = it.addressId, // as sent in the metadata as address_id
line1 = it.street, // "Gymnasievej 39"
line2 = "Planned Arrival ${it.plannedArrival}", // "Planned Arrival 14:22"
)
},
nextStop = tour.indexOf(stop), // counted from 0
routing = true, // point-to-point navigation on; leave out for off
)Only the stop at nextStop is used for now; send the whole tour from the start, so nothing
changes on your side when the others are.
4. Show it
Pick one of three ways.
The fragment in a container of your own screen, between your own bar and buttons.
supportFragmentManager.beginTransaction()
.add(R.id.navigation_container, TruemetricsNavigationFragment.newInstance(request))
.commit()R.id.navigation_container is a FrameLayout or FragmentContainerView of at least about
400 dp height. The full example below shows the rest.
5. Hear the end
The navigation ends on arrival (NavigationResult.Arrived), or with Back, ✕ or the system Back
(NavigationResult.Ended). Take the fragment away then.
Either implement TruemetricsNavigationFragment.Host on the activity (or the parent fragment):
it hears the end at once, also while your screen is out of sight, for example with the phone
locked. Out of sight your screen’s state is already saved, so remove the fragment with
commitNowAllowingStateLoss(): a commitNow() throws there. Or listen for the fragment result,
delivered when your screen is started:
supportFragmentManager.setFragmentResultListener(TruemetricsNavigationFragment.RESULT_KEY, this) { _, bundle ->
val result = TruemetricsNavigationFragment.result(bundle)
closeNavigation()
}6. Declare the screen
The activity that holds the module keeps the map through dark-mode switches and rotation:
<activity
android:name=".StopActivity"
android:configChanges="uiMode"
android:screenOrientation="portrait" />Full example
A stop screen of a courier app: its own bar and “Delivered” button, the module in between. This is how truemetrics’ own test app embeds it.
class StopActivity : AppCompatActivity(), TruemetricsNavigationFragment.Host {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_stop) // bar, R.id.navigation_container, "Delivered" button
// Android restores the fragment itself after a configuration change or process death
if (savedInstanceState == null) {
val tour = intent.getParcelableArrayListExtra<Stop>("tour")!!
val next = intent.getIntExtra("next", 0)
val stop = tour[next]
show(
NavigationRequest(
destination = LatLon(stop.lat, stop.lon),
stops = tour.map {
NavigationStop(id = it.addressId, line1 = it.street, line2 = "Planned Arrival ${it.plannedArrival}")
},
nextStop = next,
routing = stop.routeInApp, // point-to-point navigation off or on
// The stop's details and the way out are this screen's own, around the map
showCard = false,
backLabel = null,
endOnArrival = true,
),
)
}
}
private fun show(request: NavigationRequest) {
// One navigation per activity: the old one goes first, with commitNow
close()
supportFragmentManager.beginTransaction()
.add(R.id.navigation_container, TruemetricsNavigationFragment.newInstance(request))
.commit()
}
private fun close() {
supportFragmentManager.findFragmentById(R.id.navigation_container)?.let {
// Also when the navigation ended out of sight, with this screen's state already saved
supportFragmentManager.beginTransaction().remove(it).commitNowAllowingStateLoss()
}
}
// Arrived, or Back inside the map: at once, also with the phone locked
override fun onNavigationFinished(result: NavigationResult) {
close()
if (result == NavigationResult.Arrived) showDeliveryForm() else finish()
}
}Parameters
TruemetricsNavigation.init
Your application context.
The truemetrics API key: last meter, routing, configuration. Keep it in your build config or
secrets, never in source. Call init again to switch the key; screens opened afterwards use the
new one.
NavigationRequest
The stop in WGS84 degrees, latitude first: the coordinate you hand to Google Maps today. Used for routing and the map when there is no last meter for the stop. An invalid position throws.
The whole tour, each stop at its place (up to 200 stops). Only the stop at nextStop is used for
now: its id gives the last meter, its lines the card. Without stops the map shows the
destination only.
Index in stops of the stop at destination, counted from 0. An index outside stops throws; without stops, leave it at 0.
Point-to-point navigation. false: the last meter and the courier’s position, no route. true: route to the
waypoint (else the parking, else the destination), turn-by-turn guidance, rerouting, arrival.
The card at the bottom (address, time left, arrival time, one button) and the ✕. Set false when
your own screen shows the stop and the way out around the map; with routing the drive then starts
by itself after the route overview.
The text of the card’s Back button. null for none; an ✕ ends the navigation then.
Whether the navigation ends by itself a few seconds after arrival (NavigationResult.Arrived), or
stays on the map until the courier leaves. Left out: it ends when there is a Back button and the
configuration for your key allows it. Without the card, pass true.
NavigationStop
One stop of the tour. Every field is optional.
The stop’s address_id, as sent in the metadata: typically your address identifier or route point
identifier. It tells the module where to guide the courier and which last meter to show. Without
it the map shows the destination only.
Optional, usually left out. Only if your system uses a second identifier for the stop (for example a separate parking or route point next to the address), and only if truemetrics asked for it.
The card’s first line: street and number, or a place name. One line, a longer one is cut with ”…”.
The card’s second line, shown only with line1: a line of your choice, for example
“Planned Arrival 14:22”, or postcode and city. One line, a longer one is cut with ”…”.
Results
NavigationResult | When |
|---|---|
Arrived | At the stop: after the arrival countdown, or the courier left the arrival card. |
Ended | Before arriving: Back, ✕ or the system Back. |
Good to know
- One module per activity. For the next stop, remove the old fragment with
commitNow()first, then add the new one. Areplacebuilds the new map while the old one still lives. In answer to an end heard as aHost, usecommitNowAllowingStateLoss()(see step 5). - Permissions. The module asks for location itself, and for notifications at the first drive (Android 13+). Without location it says so on the map and offers the fix. Until location is allowed it shows nothing of the stop, with navigation off too: the last meter loads only then.
- Back. The system Back ends the navigation (you hear it as the result); after that, Back is yours again.
- Theme. The map puts its own theme on the activity while it is created; the module puts your manifest theme back right away. If you set your theme in code, set it again after adding the fragment.
- Approximate location. With approximate location only, the courier’s dot is not shown and an arrival cannot be noticed; the module says so.
- Configuration from truemetrics. Reroute limits, arrival behaviour and similar settings come per API key from truemetrics. A drive keeps the configuration it started with.
- Battery. The map draws only when something changes, the screen stays on only while navigating, and the foreground service runs only during a drive on the device’s position.
- Your own MapLibre. If your app uses MapLibre itself, tell your truemetrics contact: the
module sets MapLibre’s process-wide settings at the first
init.
Let an AI agent do it
Integrate the module with an AI agent: a copy-paste prompt that analyses your app, asks you to confirm each step, and implements the integration above.
For testing and demos
Leave these out in production. With them a drive runs by itself, so a stop can be shown at a desk or in a customer meeting without driving there.
NavigationRequest, with routing = true: drive along the route by itself instead of following
the device location, with arrival as in a real drive. No rerouting.
NavigationRequest: where the simulated drive starts. Left out, at the device location. A few
hundred metres before the stop, on the road leading in, shows the approach and the last meter in
a minute.
TruemetricsNavigation.init: whether a drive from the module’s own address search is simulated
until the courier flips the switch. Not used for requests from your app.
NavigationRequest(
destination = LatLon(55.654436, 12.361784),
stops = listOf(NavigationStop(id = "9005000000042962227", idSecondary = "9005000000042962733")),
routing = true,
simulate = true,
simulationStart = LatLon(55.65624, 12.357918),
)