Current position
Request accessCurrent 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:
import android.util.Logimport com.tomtom.automotive.integration.client.api.currentposition.CurrentPositionManagerimport com.tomtom.automotive.integration.client.api.currentposition.observeposition.CurrentPositionInfoimport com.tomtom.automotive.integration.client.api.currentposition.observeposition.CurrentPositionListenerimport com.tomtom.automotive.integration.client.common.Callbackimport com.tomtom.automotive.integration.client.common.SdkReleasable
private val TAG = "CurrentPosition"private var observeCurrentPositionReleasable: SdkReleasable? = null
fun observeCurrentPosition(currentPositionManager: CurrentPositionManager) { observeCurrentPositionReleasable = currentPositionManager.observeCurrentPosition( object : CurrentPositionListener { override fun onCurrentPosition(info: CurrentPositionInfo) { val position = info.position if (position != null) { val coordinate = position.place?.coordinate val speedKmh = position.speed.inKilometersPerHour() val heading = position.headingInDegrees
if (coordinate != null) { Log.d(TAG, "Location: ${coordinate.latitude}, ${coordinate.longitude}") } Log.d(TAG, "Speed: ${speedKmh} km/h, Heading: ${heading}°") Log.d(TAG, "In motion: ${position.isInMotion()}") } else { Log.d(TAG, "Position unavailable") } }
override fun onFunctionalityUnavailable(reason: Callback.Reason) { Log.e(TAG, "Position updates unavailable: ${reason.devMessage}") } } )}
fun unobserveCurrentPosition() { observeCurrentPositionReleasable?.release() observeCurrentPositionReleasable = null}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 longitudelatitude- 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 aSpeedobject- Use
inKilometersPerHour(),inMilesPerHour(),inMetersPerSecond()to get values
- Use
headingInDegrees- Compass direction in degrees (0-360)- 0° = North
- 90° = East
- 180° = South
- 270° = West
currentSpeedLimit- Current speed limit informationroadProperties- Properties of the road (optional)
Converting speed units
import com.tomtom.automotive.integration.client.api.model.quantity.Speed
fun logSpeedInUnits(speed: Speed) { val metersPerSecond = speed.inMetersPerSecond() val kilometersPerHour = speed.inKilometersPerHour() val milesPerHour = speed.inMilesPerHour()
println("Speed: $metersPerSecond m/s") println("Speed: $kilometersPerHour km/h") println("Speed: $milesPerHour mph")}Checking if vehicle is in motion
The Position object provides a convenient method to check if the vehicle is moving:
import android.util.Logimport com.tomtom.automotive.integration.client.api.currentposition.CurrentPositionManagerimport com.tomtom.automotive.integration.client.api.currentposition.observeposition.CurrentPositionInfoimport com.tomtom.automotive.integration.client.api.currentposition.observeposition.CurrentPositionListenerimport com.tomtom.automotive.integration.client.common.Callbackimport com.tomtom.automotive.integration.client.common.SdkReleasable
private val TAG = "CurrentPosition"private var observeCurrentPositionReleasable: SdkReleasable? = null
fun observeVehicleMotion(currentPositionManager: CurrentPositionManager) { observeCurrentPositionReleasable = currentPositionManager.observeCurrentPosition( object : CurrentPositionListener { override fun onCurrentPosition(info: CurrentPositionInfo) { val position = info.position if (position != null) { if (position.isInMotion()) { Log.d(TAG, "Vehicle is moving at ${position.speed.inKilometersPerHour()} km/h") } else { Log.d(TAG, "Vehicle is stationary") } } }
override fun onFunctionalityUnavailable(reason: Callback.Reason) { Log.e(TAG, "Position updates unavailable: ${reason.devMessage}") } } )}
fun unobserveVehicleMotion() { observeCurrentPositionReleasable?.release() observeCurrentPositionReleasable = null}Accessing address information
When position includes place data, you can access address details:
import android.util.Logimport com.tomtom.automotive.integration.client.api.model.Address
private val TAG = "CurrentPosition"
fun logAddressInfo(address: Address) { Log.d(TAG, "Street: ${address.streetName}") Log.d(TAG, "House number: ${address.houseNumber}") Log.d(TAG, "City: ${address.cityName}") Log.d(TAG, "Postal code: ${address.postalCode}") Log.d(TAG, "State: ${address.stateName}") Log.d(TAG, "Country: ${address.countryName}") Log.d(TAG, "Formatted: ${address.formattedAddress}")}