Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions android-auto-app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ android {
}

dependencies {
implementation(project(":extension-androidauto"))
// implementation(project(":extension-androidauto"))
implementation(libs.googleCarAppLibrary)
implementation("com.mapbox.maps:android:10.18.1")
implementation("com.mapbox.extension:maps-androidauto:0.5.0")

// Please review the compatibility guide. This app is showcasing the latest features with latest Maps SDK main branch.
// https://github.com/mapbox/mapbox-maps-android/tree/main/extension-androidauto#compatibility-with-maps-sdk-v11
implementation(project(":maps-sdk"))
// implementation(project(":maps-sdk"))

implementation(libs.kotlin)
implementation(libs.androidx.appCompat)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,27 @@ package com.mapbox.maps.testapp.auto.app

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.mapbox.geojson.Point
import com.mapbox.maps.MapInitOptions
import com.mapbox.maps.MapView
import com.mapbox.maps.dsl.cameraOptions
import com.mapbox.maps.plugin.locationcomponent.createDefault2DPuck
import com.mapbox.maps.plugin.locationcomponent.location
import com.mapbox.maps.plugin.locationcomponent.location2
import com.mapbox.maps.testapp.auto.R

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// $ANDROID_HOME/extras/google/auto/desktop-head-unit --usb
setContentView(
MapView(this, mapInitOptions = MapInitOptions(this, cameraOptions = cameraOptions {
center(Point.fromLngLat(24.9384, 60.1699))
zoom(17.0)
})).apply {
location.enabled = true
location.createDefault2DPuck(this@MainActivity, true)
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import com.mapbox.maps.threading.AnimationThreadController
@OptIn(MapboxExperimental::class)
class CarAnimationThreadController : MapboxCarMapObserver {
override fun onAttached(mapboxCarMapSurface: MapboxCarMapSurface) {
if (deviceRequiresBackgroundThreadAnimations()) {
// if (deviceRequiresBackgroundThreadAnimations()) {
AnimationThreadController.useBackgroundThread()
}
// }
}

override fun onDetached(mapboxCarMapSurface: MapboxCarMapSurface) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.mapbox.maps.dsl.cameraOptions
import com.mapbox.maps.extension.androidauto.DefaultMapboxCarMapGestureHandler
import com.mapbox.maps.extension.androidauto.MapboxCarMapObserver
import com.mapbox.maps.extension.androidauto.MapboxCarMapSurface
import com.mapbox.maps.plugin.PuckBearing
import com.mapbox.maps.plugin.animation.camera
import com.mapbox.maps.plugin.locationcomponent.OnIndicatorBearingChangedListener
import com.mapbox.maps.plugin.locationcomponent.OnIndicatorPositionChangedListener
Expand All @@ -30,7 +29,7 @@ class CarCameraController : MapboxCarMapObserver {
private val changePositionListener = OnIndicatorPositionChangedListener { point ->
lastGpsLocation = point
if (isTrackingPuck) {
surface?.mapSurface?.mapboxMap?.setCamera(
surface?.mapSurface?.getMapboxMap()?.setCamera(
cameraOptions {
center(point)
padding(insets)
Expand All @@ -41,7 +40,7 @@ class CarCameraController : MapboxCarMapObserver {

private val changeBearingListener = OnIndicatorBearingChangedListener { bearing ->
if (isTrackingPuck) {
surface?.mapSurface?.mapboxMap?.setCamera(
surface?.mapSurface?.getMapboxMap()?.setCamera(
cameraOptions {
bearing(bearing)
}
Expand All @@ -67,7 +66,7 @@ class CarCameraController : MapboxCarMapObserver {
override fun onAttached(mapboxCarMapSurface: MapboxCarMapSurface) {
super.onAttached(mapboxCarMapSurface)
this.surface = mapboxCarMapSurface
mapboxCarMapSurface.mapSurface.mapboxMap.setCamera(
mapboxCarMapSurface.mapSurface.getMapboxMap().setCamera(
cameraOptions {
pitch(previousCameraState?.pitch ?: INITIAL_PITCH)
zoom(previousCameraState?.zoom ?: INITIAL_ZOOM)
Expand All @@ -76,16 +75,15 @@ class CarCameraController : MapboxCarMapObserver {
)
with(mapboxCarMapSurface.mapSurface.location) {
// Show a 3D location puck
locationPuck = CarLocationPuck.duckLocationPuckConstantSize
locationPuck = CarLocationPuck.duckLocationPuckLowZoom
enabled = true
puckBearing = PuckBearing.HEADING
addOnIndicatorPositionChangedListener(changePositionListener)
addOnIndicatorBearingChangedListener(changeBearingListener)
}
}

override fun onDetached(mapboxCarMapSurface: MapboxCarMapSurface) {
previousCameraState = mapboxCarMapSurface.mapSurface.mapboxMap.cameraState
previousCameraState = mapboxCarMapSurface.mapSurface.getMapboxMap().cameraState
with(mapboxCarMapSurface.mapSurface.location) {
removeOnIndicatorPositionChangedListener(changePositionListener)
removeOnIndicatorBearingChangedListener(changeBearingListener)
Expand Down Expand Up @@ -121,7 +119,7 @@ class CarCameraController : MapboxCarMapObserver {

private fun scaleEaseBy(delta: Double) {
val mapSurface = surface?.mapSurface
val fromZoom = mapSurface?.mapboxMap?.cameraState?.zoom ?: return
val fromZoom = mapSurface?.getMapboxMap()?.cameraState?.zoom ?: return
val toZoom = (fromZoom + delta).coerceIn(MIN_ZOOM_OUT, MAX_ZOOM_IN)
mapSurface.camera.easeTo(cameraOptions { zoom(toZoom) })
}
Expand All @@ -145,7 +143,7 @@ class CarCameraController : MapboxCarMapObserver {
/**
* When zooming the camera by a delta, this will prevent the camera from zooming further.
*/
private const val MIN_ZOOM_OUT = 0.0
private const val MIN_ZOOM_OUT = 6.0

/**
* When zooming the camera by a delta, this will prevent the camera from zooming further.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.mapbox.maps.testapp.auto.car

import android.annotation.SuppressLint
import com.mapbox.maps.ImageHolder
import com.mapbox.maps.MapboxExperimental
import androidx.car.app.CarContext
import com.mapbox.maps.R
import com.mapbox.maps.extension.style.expressions.dsl.generated.literal
import com.mapbox.maps.plugin.LocationPuck2D
import com.mapbox.maps.plugin.LocationPuck3D
import com.mapbox.maps.plugin.ModelScaleMode

/**
* Provides car location puck definitions.
Expand All @@ -15,32 +14,28 @@ internal object CarLocationPuck {
/**
* 3D location puck with the real world size.
*/
@OptIn(MapboxExperimental::class)
val duckLocationPuckRealWorld = LocationPuck3D(
val duckLocationPuckHighZoom = LocationPuck3D(
modelUri = "https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/Duck/glTF-Embedded/Duck.gltf",
modelScale = listOf(20f, 20f, 20f),
modelScaleMode = ModelScaleMode.MAP,
modelScaleExpression = literal(listOf(10, 10, 10)).toJson(),
modelRotation = listOf(0f, 0f, -90f)
)

/**
* 3D location puck with a constant size across zoom levels.
*/
@OptIn(MapboxExperimental::class)
val duckLocationPuckConstantSize = LocationPuck3D(
val duckLocationPuckLowZoom = LocationPuck3D(
modelUri = "https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/Duck/glTF-Embedded/Duck.gltf",
modelScale = listOf(20f, 20f, 20f),
modelScaleMode = ModelScaleMode.VIEWPORT,
modelScale = listOf(0.2f, 0.2f, 0.2f),
modelRotation = listOf(0f, 0f, -90f)
)

/**
* Classic 2D location puck with blue dot and arrow.
*/
@SuppressLint("UseCompatLoadingForDrawables")
val classicLocationPuck2D = LocationPuck2D(
topImage = ImageHolder.Companion.from(R.drawable.mapbox_user_icon),
bearingImage = ImageHolder.Companion.from(R.drawable.mapbox_user_bearing_icon),
shadowImage = ImageHolder.Companion.from(R.drawable.mapbox_user_stroke_icon)
fun classicLocationPuck2D(carContext: CarContext) = LocationPuck2D(
topImage = carContext.getDrawable(R.drawable.mapbox_user_icon),
bearingImage = carContext.getDrawable(R.drawable.mapbox_user_bearing_icon),
shadowImage = carContext.getDrawable(R.drawable.mapbox_user_stroke_icon)
)
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.mapbox.maps.testapp.auto.car

import androidx.car.app.CarContext
import com.mapbox.bindgen.Value
import com.mapbox.maps.Style
import com.mapbox.maps.extension.androidauto.MapboxCarMapObserver
import com.mapbox.maps.extension.androidauto.MapboxCarMapSurface
import com.mapbox.maps.logE
import com.mapbox.maps.extension.style.layers.generated.skyLayer
import com.mapbox.maps.extension.style.layers.properties.generated.SkyType
import com.mapbox.maps.extension.style.sources.generated.rasterDemSource
import com.mapbox.maps.extension.style.style
import com.mapbox.maps.extension.style.terrain.generated.terrain

/**
* Example showing how you can add a sky layer that has a sun direction,
Expand All @@ -24,28 +27,35 @@ class CarMapShowcase : MapboxCarMapObserver {
this.mapboxCarMapSurface = null
}

fun loadMapStyle(carContext: CarContext) {
val lightPreset = if (carContext.isDarkMode) LightPresets.NIGHT else LightPresets.DAY

mapboxCarMapSurface?.mapSurface?.mapboxMap?.loadStyle(Style.STANDARD) { style ->
style.setStyleImportConfigProperty(
"basemap",
"lightPreset",
Value.valueOf(lightPreset.toString().lowercase())
).error?.let {
logE(TAG, it)
}
}
fun mapStyleUri(carContext: CarContext): String {
return if (carContext.isDarkMode) Style.TRAFFIC_NIGHT else Style.TRAFFIC_DAY
}

private enum class LightPresets {
DAY,
DAWN,
DUSK,
NIGHT
fun loadMapStyle(carContext: CarContext) {
// https://docs.mapbox.com/mapbox-gl-js/style-spec/layers/#paint-sky-sky-atmosphere-sun
// Position of the sun center [a azimuthal angle, p polar angle].
// Azimuthal is degrees from north, where 0.0 is north.
// Polar is degrees from overhead, where 0.0 is overhead.
val sunDirection = if (carContext.isDarkMode) listOf(-50.0, 90.2) else listOf(0.0, 0.0)

mapboxCarMapSurface?.mapSurface?.getMapboxMap()?.loadStyle(
styleExtension = style(mapStyleUri(carContext)) {
+rasterDemSource(DEM_SOURCE) {
url(TERRAIN_URL_TILE_RESOURCE)
tileSize(514)
}
+terrain(DEM_SOURCE)
+skyLayer(SKY_LAYER) {
skyType(SkyType.ATMOSPHERE)
skyAtmosphereSun(sunDirection)
}
}
)
}

companion object {
private const val TAG = "CarMapShowcase"
private const val SKY_LAYER = "sky"
private const val DEM_SOURCE = "mapbox-dem"
private const val TERRAIN_URL_TILE_RESOURCE = "mapbox://mapbox.mapbox-terrain-dem-v1"
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.mapbox.maps.testapp.auto.car

import com.mapbox.common.Cancelable
import com.mapbox.maps.MapboxExperimental
import com.mapbox.maps.extension.androidauto.MapboxCarMapObserver
import com.mapbox.maps.extension.androidauto.MapboxCarMapSurface
import com.mapbox.maps.extension.androidauto.widgets.CompassWidget
import com.mapbox.maps.extension.androidauto.widgets.LogoWidget
import com.mapbox.maps.plugin.delegates.listeners.OnCameraChangeListener

/**
* Note that the Widgets are only available when using android auto extension together with the
Expand All @@ -15,7 +15,7 @@ import com.mapbox.maps.extension.androidauto.widgets.LogoWidget
class CarMapWidgets : MapboxCarMapObserver {
private lateinit var logoWidget: LogoWidget
private lateinit var compassWidget: CompassWidget
private var cancellable: Cancelable? = null
private lateinit var onCameraChangeListener: OnCameraChangeListener
override fun onAttached(mapboxCarMapSurface: MapboxCarMapSurface) {
super.onAttached(mapboxCarMapSurface)
with(mapboxCarMapSurface) {
Expand All @@ -25,19 +25,17 @@ class CarMapWidgets : MapboxCarMapObserver {
marginX = 26f,
marginY = 120f,
)
onCameraChangeListener = OnCameraChangeListener { compassWidget.setRotation(-mapSurface.getMapboxMap().cameraState.bearing.toFloat()) }
mapSurface.addWidget(logoWidget)
mapSurface.addWidget(compassWidget)
cancellable = mapSurface.mapboxMap.subscribeCameraChanged {
compassWidget.setRotation(-mapSurface.mapboxMap.cameraState.bearing.toFloat())
}
mapSurface.getMapboxMap().addOnCameraChangeListener(onCameraChangeListener)
}
}

override fun onDetached(mapboxCarMapSurface: MapboxCarMapSurface) {
super.onDetached(mapboxCarMapSurface)
with(mapboxCarMapSurface) {
cancellable?.cancel()
cancellable = null
mapSurface.getMapboxMap().removeOnCameraChangeListener(onCameraChangeListener)
mapSurface.removeWidget(logoWidget)
mapSurface.removeWidget(compassWidget)
}
Expand Down
Loading