Current position

The CurrentPositionManager provides access to the vehicle's position, including coordinates, speed, and heading. Use this information to enable location-based features.

Overview

CurrentPositionManager provides functionality for:

  • Observing position updates
  • Accessing coordinate information
  • Reading speed and heading data
  • Getting current speed limit information

Observing position updates

Subscribe to receive periodic position updates:

1import android.util.Log
2import com.tomtom.automotive.integration.client.api.currentposition.CurrentPositionManager
3import com.tomtom.automotive.integration.client.api.currentposition.observeposition.CurrentPositionInfo
4import com.tomtom.automotive.integration.client.api.currentposition.observeposition.CurrentPositionListener
5import com.tomtom.automotive.integration.client.common.Callback
6import com.tomtom.automotive.integration.client.common.SdkReleasable
7
8private val TAG = "CurrentPosition"
9private var observeCurrentPositionReleasable: SdkReleasable? = null
10
11fun observeCurrentPosition(currentPositionManager: CurrentPositionManager) {
12 observeCurrentPositionReleasable = currentPositionManager.observeCurrentPosition(
13 object : CurrentPositionListener {
14 override fun onCurrentPosition(info: CurrentPositionInfo) {
15 val position = info.position
16 if (position != null) {
17 val coordinate = position.place?.coordinate
18 val speedKmh = position.speed.inKilometersPerHour()
19 val heading = position.headingInDegrees
20
21 if (coordinate != null) {
22 Log.d(TAG, "Location: ${coordinate.latitude}, ${coordinate.longitude}")
23 }
24 Log.d(TAG, "Speed: ${speedKmh} km/h, Heading: ${heading}°")
25 Log.d(TAG, "In motion: ${position.isInMotion()}")
26 } else {
27 Log.d(TAG, "Position unavailable")
28 }
29 }
30
31 override fun onFunctionalityUnavailable(reason: Callback.Reason) {
32 Log.e(TAG, "Position updates unavailable: ${reason.devMessage}")
33 }
34 }
35 )
36}
37
38fun unobserveCurrentPosition() {
39 observeCurrentPositionReleasable?.release()
40 observeCurrentPositionReleasable = null
41}

Position structure

The CurrentPositionInfo object contains:

  • position - The current position data (nullable if unavailable)
    • place - Place information with coordinate and address (nullable)
      • coordinate - Latitude and longitude
        • latitude - Decimal degrees (-90 to 90)
        • longitude - Decimal degrees (-180 to 180)
      • address - Address information (nullable)
      • name - Name of the place (nullable)
      • poiDetails - Point of Interest details (nullable)
    • speed - Vehicle speed as a Speed object
      • Use inKilometersPerHour(), inMilesPerHour(), inMetersPerSecond() to get values
    • headingInDegrees - Compass direction in degrees (0-360)
      • 0° = North
      • 90° = East
      • 180° = South
      • 270° = West
    • currentSpeedLimit - Current speed limit information
    • roadProperties - Properties of the road (optional)

Converting speed units

1import com.tomtom.automotive.integration.client.api.model.quantity.Speed
2
3fun logSpeedInUnits(speed: Speed) {
4 val metersPerSecond = speed.inMetersPerSecond()
5 val kilometersPerHour = speed.inKilometersPerHour()
6 val milesPerHour = speed.inMilesPerHour()
7
8 println("Speed: $metersPerSecond m/s")
9 println("Speed: $kilometersPerHour km/h")
10 println("Speed: $milesPerHour mph")
11}

Checking if vehicle is in motion

The Position object provides a convenient method to check if the vehicle is moving:

1import android.util.Log
2import com.tomtom.automotive.integration.client.api.currentposition.CurrentPositionManager
3import com.tomtom.automotive.integration.client.api.currentposition.observeposition.CurrentPositionInfo
4import com.tomtom.automotive.integration.client.api.currentposition.observeposition.CurrentPositionListener
5import com.tomtom.automotive.integration.client.common.Callback
6import com.tomtom.automotive.integration.client.common.SdkReleasable
7
8private val TAG = "CurrentPosition"
9private var observeCurrentPositionReleasable: SdkReleasable? = null
10
11fun observeVehicleMotion(currentPositionManager: CurrentPositionManager) {
12 observeCurrentPositionReleasable = currentPositionManager.observeCurrentPosition(
13 object : CurrentPositionListener {
14 override fun onCurrentPosition(info: CurrentPositionInfo) {
15 val position = info.position
16 if (position != null) {
17 if (position.isInMotion()) {
18 Log.d(TAG, "Vehicle is moving at ${position.speed.inKilometersPerHour()} km/h")
19 } else {
20 Log.d(TAG, "Vehicle is stationary")
21 }
22 }
23 }
24
25 override fun onFunctionalityUnavailable(reason: Callback.Reason) {
26 Log.e(TAG, "Position updates unavailable: ${reason.devMessage}")
27 }
28 }
29 )
30}
31
32fun unobserveVehicleMotion() {
33 observeCurrentPositionReleasable?.release()
34 observeCurrentPositionReleasable = null
35}

Accessing address information

When position includes place data, you can access address details:

1import android.util.Log
2import com.tomtom.automotive.integration.client.api.model.Address
3
4private val TAG = "CurrentPosition"
5
6fun logAddressInfo(address: Address) {
7 Log.d(TAG, "Street: ${address.streetName}")
8 Log.d(TAG, "House number: ${address.houseNumber}")
9 Log.d(TAG, "City: ${address.cityName}")
10 Log.d(TAG, "Postal code: ${address.postalCode}")
11 Log.d(TAG, "State: ${address.stateName}")
12 Log.d(TAG, "Country: ${address.countryName}")
13 Log.d(TAG, "Formatted: ${address.formattedAddress}")
14}