Current position
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.Log2import com.tomtom.automotive.integration.client.api.currentposition.CurrentPositionManager3import com.tomtom.automotive.integration.client.api.currentposition.observeposition.CurrentPositionInfo4import com.tomtom.automotive.integration.client.api.currentposition.observeposition.CurrentPositionListener5import com.tomtom.automotive.integration.client.common.Callback6import com.tomtom.automotive.integration.client.common.SdkReleasable78private val TAG = "CurrentPosition"9private var observeCurrentPositionReleasable: SdkReleasable? = null1011fun observeCurrentPosition(currentPositionManager: CurrentPositionManager) {12 observeCurrentPositionReleasable = currentPositionManager.observeCurrentPosition(13 object : CurrentPositionListener {14 override fun onCurrentPosition(info: CurrentPositionInfo) {15 val position = info.position16 if (position != null) {17 val coordinate = position.place?.coordinate18 val speedKmh = position.speed.inKilometersPerHour()19 val heading = position.headingInDegrees2021 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 }3031 override fun onFunctionalityUnavailable(reason: Callback.Reason) {32 Log.e(TAG, "Position updates unavailable: ${reason.devMessage}")33 }34 }35 )36}3738fun unobserveCurrentPosition() {39 observeCurrentPositionReleasable?.release()40 observeCurrentPositionReleasable = null41}
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
1import com.tomtom.automotive.integration.client.api.model.quantity.Speed23fun logSpeedInUnits(speed: Speed) {4 val metersPerSecond = speed.inMetersPerSecond()5 val kilometersPerHour = speed.inKilometersPerHour()6 val milesPerHour = speed.inMilesPerHour()78 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.Log2import com.tomtom.automotive.integration.client.api.currentposition.CurrentPositionManager3import com.tomtom.automotive.integration.client.api.currentposition.observeposition.CurrentPositionInfo4import com.tomtom.automotive.integration.client.api.currentposition.observeposition.CurrentPositionListener5import com.tomtom.automotive.integration.client.common.Callback6import com.tomtom.automotive.integration.client.common.SdkReleasable78private val TAG = "CurrentPosition"9private var observeCurrentPositionReleasable: SdkReleasable? = null1011fun observeVehicleMotion(currentPositionManager: CurrentPositionManager) {12 observeCurrentPositionReleasable = currentPositionManager.observeCurrentPosition(13 object : CurrentPositionListener {14 override fun onCurrentPosition(info: CurrentPositionInfo) {15 val position = info.position16 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 }2425 override fun onFunctionalityUnavailable(reason: Callback.Reason) {26 Log.e(TAG, "Position updates unavailable: ${reason.devMessage}")27 }28 }29 )30}3132fun unobserveVehicleMotion() {33 observeCurrentPositionReleasable?.release()34 observeCurrentPositionReleasable = null35}
Accessing address information
When position includes place data, you can access address details:
1import android.util.Log2import com.tomtom.automotive.integration.client.api.model.Address34private val TAG = "CurrentPosition"56fun 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}