Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Mapbox welcomes participation and contributions from everyone.

## Features ✨ and improvements 🏁
* Add accuracy radius support for LocationComponent. ([#1016](https://github.com/mapbox/mapbox-maps-android/pull/1016))
* Add support for custom widgets rendered on top of the map. ([#1036](https://github.com/mapbox/mapbox-maps-android/pull/1036))

# 10.4.0-beta.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import com.mapbox.maps.MapSurface
import com.mapbox.maps.dsl.cameraOptions
import com.mapbox.maps.extension.androidauto.OnMapScrollListener
import com.mapbox.maps.plugin.animation.camera
import com.mapbox.maps.plugin.locationcomponent.OnIndicatorBearingChangedListener
import com.mapbox.maps.plugin.locationcomponent.OnIndicatorPositionChangedListener

/**
* Controller class to handle map camera changes.
*/
class CarCameraController : OnIndicatorPositionChangedListener, OnMapScrollListener {
class CarCameraController : OnIndicatorPositionChangedListener, OnIndicatorBearingChangedListener, OnMapScrollListener {

private var lastGpsLocation: Point = HELSINKI
private var isTrackingPuck = true
Expand Down Expand Up @@ -46,6 +47,16 @@ class CarCameraController : OnIndicatorPositionChangedListener, OnMapScrollListe
}
}

override fun onIndicatorBearingChanged(bearing: Double) {
if (isTrackingPuck) {
surface.getMapboxMap().setCamera(
cameraOptions {
bearing(bearing)
}
)
}
}

override fun onMapScroll() {
dismissTracking()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import android.Manifest.permission.ACCESS_FINE_LOCATION
import android.content.Intent
import android.content.pm.PackageManager.PERMISSION_GRANTED
import android.content.res.Configuration
import androidx.car.app.Screen
import androidx.car.app.ScreenManager
import androidx.car.app.Session
import androidx.car.app.*
import com.mapbox.maps.EdgeInsets
import com.mapbox.maps.MapSurface
import com.mapbox.maps.Style
import com.mapbox.maps.extension.androidauto.CompassWidget
import com.mapbox.maps.extension.androidauto.LogoWidget
import com.mapbox.maps.extension.androidauto.initMapSurface
import com.mapbox.maps.extension.style.layers.generated.skyLayer
import com.mapbox.maps.extension.style.layers.properties.generated.SkyType
Expand All @@ -28,7 +28,16 @@ class MapSession : Session() {

override fun onCreateScreen(intent: Intent): Screen {
val mapScreen = MapScreen(carContext)
initMapSurface(scrollListener = carCameraController) { surface ->
initMapSurface(
scrollListener = carCameraController,
) { surface ->
val logo = LogoWidget(carContext)
val compass = CompassWidget(
carContext,
marginX = 26f,
marginY = 120f,
)

mapSurface = surface
carCameraController.init(
mapSurface,
Expand All @@ -42,6 +51,15 @@ class MapSession : Session() {
mapScreen.setMapCameraController(carCameraController)
loadStyle(surface)
initLocationComponent(surface)

surface.addWidget(logo)
surface.addWidget(compass)

surface.getMapboxMap().apply {
addOnCameraChangeListener {
compass.setRotation(this.cameraState.bearing.toFloat())
}
}
}
return if (carContext.checkSelfPermission(ACCESS_FINE_LOCATION) != PERMISSION_GRANTED) {
carContext.getCarService(ScreenManager::class.java).push(mapScreen)
Expand Down Expand Up @@ -73,6 +91,7 @@ class MapSession : Session() {
locationPuck = CarLocationPuck.duckLocationPuckLowZoom
enabled = true
addOnIndicatorPositionChangedListener(carCameraController)
addOnIndicatorBearingChangedListener(carCameraController)
}
}

Expand Down
13 changes: 12 additions & 1 deletion extension-androidauto/api/extension-androidauto.api
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,27 @@ public final class com/mapbox/maps/extension/androidauto/BuildConfig {
public fun <init> ()V
}

public final class com/mapbox/maps/extension/androidauto/CompassWidget : com/mapbox/maps/renderer/widget/BitmapWidget {
public fun <init> (Landroid/content/Context;Lcom/mapbox/maps/renderer/widget/WidgetPosition;FF)V
public synthetic fun <init> (Landroid/content/Context;Lcom/mapbox/maps/renderer/widget/WidgetPosition;FFILkotlin/jvm/internal/DefaultConstructorMarker;)V
}

public final class com/mapbox/maps/extension/androidauto/LogoWidget : com/mapbox/maps/renderer/widget/BitmapWidget {
public fun <init> (Landroid/content/Context;Lcom/mapbox/maps/renderer/widget/WidgetPosition;FF)V
public synthetic fun <init> (Landroid/content/Context;Lcom/mapbox/maps/renderer/widget/WidgetPosition;FFILkotlin/jvm/internal/DefaultConstructorMarker;)V
}

public abstract interface class com/mapbox/maps/extension/androidauto/MapSurfaceReadyCallback {
public abstract fun onMapSurfaceReady (Lcom/mapbox/maps/MapSurface;)V
}

public final class com/mapbox/maps/extension/androidauto/MapboxCarUtilsKt {
public static final fun initMapSurface (Landroidx/car/app/Session;Lcom/mapbox/maps/MapInitOptions;Lcom/mapbox/maps/extension/androidauto/MapSurfaceReadyCallback;)V
public static final fun initMapSurface (Landroidx/car/app/Session;Lcom/mapbox/maps/MapInitOptions;Lcom/mapbox/maps/extension/androidauto/OnMapScrollListener;Lcom/mapbox/maps/extension/androidauto/MapSurfaceReadyCallback;)V
public static final fun initMapSurface (Landroidx/car/app/Session;Lcom/mapbox/maps/MapInitOptions;Lcom/mapbox/maps/extension/androidauto/OnMapScrollListener;Lcom/mapbox/maps/extension/androidauto/OnMapScaleListener;Landroidx/car/app/SurfaceCallback;Lcom/mapbox/maps/extension/androidauto/MapSurfaceReadyCallback;)V
public static final fun initMapSurface (Landroidx/car/app/Session;Lcom/mapbox/maps/MapInitOptions;Lcom/mapbox/maps/extension/androidauto/OnMapScrollListener;Lcom/mapbox/maps/extension/androidauto/OnMapScaleListener;Lcom/mapbox/maps/extension/androidauto/MapSurfaceReadyCallback;)V
public static final fun initMapSurface (Landroidx/car/app/Session;Lcom/mapbox/maps/extension/androidauto/MapSurfaceReadyCallback;)V
public static synthetic fun initMapSurface$default (Landroidx/car/app/Session;Lcom/mapbox/maps/MapInitOptions;Lcom/mapbox/maps/extension/androidauto/OnMapScrollListener;Lcom/mapbox/maps/extension/androidauto/OnMapScaleListener;Lcom/mapbox/maps/extension/androidauto/MapSurfaceReadyCallback;ILjava/lang/Object;)V
public static synthetic fun initMapSurface$default (Landroidx/car/app/Session;Lcom/mapbox/maps/MapInitOptions;Lcom/mapbox/maps/extension/androidauto/OnMapScrollListener;Lcom/mapbox/maps/extension/androidauto/OnMapScaleListener;Landroidx/car/app/SurfaceCallback;Lcom/mapbox/maps/extension/androidauto/MapSurfaceReadyCallback;ILjava/lang/Object;)V
}

public abstract interface class com/mapbox/maps/extension/androidauto/OnMapScaleListener {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.mapbox.maps.extension.androidauto

import android.content.Context
import android.graphics.BitmapFactory
import com.mapbox.maps.MapboxExperimental
import com.mapbox.maps.renderer.widget.BitmapWidget
import com.mapbox.maps.renderer.widget.WidgetPosition

/**
* Widget shows compass. Positioned in the top right corner by default.
*
* @param position position of compass
* @param marginX horizontal margin in pixels
* @param marginY vertical margin in pixels
*/
@MapboxExperimental
class CompassWidget(
context: Context,
position: WidgetPosition = WidgetPosition(
horizontal = WidgetPosition.Horizontal.RIGHT,
vertical = WidgetPosition.Vertical.TOP,
),
marginX: Float = 20f,
marginY: Float = 20f,
) : BitmapWidget(
bitmap = BitmapFactory.decodeResource(context.resources, R.drawable.mapbox_compass_icon),
position = position,
marginX = marginX,
marginY = marginY,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.mapbox.maps.extension.androidauto

import android.content.Context
import android.graphics.BitmapFactory
import com.mapbox.maps.MapboxExperimental
import com.mapbox.maps.renderer.widget.BitmapWidget
import com.mapbox.maps.renderer.widget.WidgetPosition

/**
* Widget shows compass. Positioned in the bottom left corner by default.
*
* @param position position of logo
* @param marginX horizontal margin in pixels
* @param marginY vertical margin in pixels
*/
@MapboxExperimental
class LogoWidget constructor(
context: Context,
position: WidgetPosition = WidgetPosition(
horizontal = WidgetPosition.Horizontal.LEFT,
vertical = WidgetPosition.Vertical.BOTTOM,
),
marginX: Float = 20f,
marginY: Float = 20f,
) : BitmapWidget(
bitmap = BitmapFactory.decodeResource(context.resources, R.drawable.mapbox_logo_icon),
position = position,
marginX = marginX,
marginY = marginY,
)
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ fun Session.initMapSurface(
mapInitOptions: MapInitOptions = MapInitOptions(carContext),
scrollListener: OnMapScrollListener? = null,
scaleListener: OnMapScaleListener? = null,
callback: SurfaceCallback? = null,
mapSurfaceReadyCallback: MapSurfaceReadyCallback
) {
var mapSurface: MapSurface? = null
val surfaceCallback: SurfaceCallback = object : SurfaceCallback {
override fun onSurfaceAvailable(surfaceContainer: SurfaceContainer) {
callback?.onSurfaceAvailable(surfaceContainer)
synchronized(this) {
Logger.i(TAG, "Surface available $surfaceContainer")
surfaceContainer.surface?.let { surface ->
Expand Down Expand Up @@ -119,29 +121,25 @@ fun Session.initMapSurface(
lifecycle.addObserver(
object : DefaultLifecycleObserver {
override fun onCreate(owner: LifecycleOwner) {
Logger.i(TAG, "SurfaceRenderer created")
synchronized(this) {
carContext.getCarService(AppManager::class.java)
.setSurfaceCallback(surfaceCallback)
}
}

override fun onStart(owner: LifecycleOwner) {
Logger.i(TAG, "onStart")
synchronized(this) {
mapSurface?.onStart()
}
}

override fun onStop(owner: LifecycleOwner) {
Logger.i(TAG, "onStop")
synchronized(this) {
mapSurface?.onStop()
}
}

override fun onDestroy(owner: LifecycleOwner) {
Logger.i(TAG, "onDestroy")
synchronized(this) {
mapSurface?.onDestroy()
}
Expand All @@ -151,7 +149,6 @@ fun Session.initMapSurface(
}

private fun MapSurface.onScroll(distanceX: Float, distanceY: Float) {
Logger.i(TAG, "handleScroll $distanceX, $distanceY")
synchronized(this) {
val centerScreen = ScreenCoordinate(0.0, 0.0)
getMapboxMap().apply {
Expand All @@ -170,7 +167,6 @@ private fun MapSurface.onScroll(distanceX: Float, distanceY: Float) {
}

private fun MapSurface.onScale(focusX: Float, focusY: Float, scaleFactor: Float) {
Logger.i(TAG, "handleScale $focusX, $focusY. $scaleFactor")
synchronized(this) {
val cameraState = getMapboxMap().cameraState
Logger.i(TAG, "setting zoom ${cameraState.zoom * scaleFactor}")
Expand Down
47 changes: 47 additions & 0 deletions sdk/api/sdk.api
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ public final class com/mapbox/maps/BuildConfig {
}

public abstract interface class com/mapbox/maps/MapControllable : com/mapbox/maps/MapboxLifecycleObserver {
public abstract fun addWidget (Lcom/mapbox/maps/renderer/widget/Widget;)V
public abstract fun getMapboxMap ()Lcom/mapbox/maps/MapboxMap;
public abstract fun onGenericMotionEvent (Landroid/view/MotionEvent;)Z
public abstract fun onSizeChanged (II)V
public abstract fun onTouchEvent (Landroid/view/MotionEvent;)Z
public abstract fun queueEvent (Ljava/lang/Runnable;Z)V
public abstract fun removeWidget (Lcom/mapbox/maps/renderer/widget/Widget;)Z
public abstract fun setMaximumFps (I)V
public abstract fun setOnFpsChangedListener (Lcom/mapbox/maps/renderer/OnFpsChangedListener;)V
public abstract fun snapshot ()Landroid/graphics/Bitmap;
Expand Down Expand Up @@ -83,8 +85,10 @@ public final class com/mapbox/maps/MapSurface : com/mapbox/maps/MapControllable,
public fun <init> (Landroid/content/Context;Landroid/view/Surface;)V
public fun <init> (Landroid/content/Context;Landroid/view/Surface;Lcom/mapbox/maps/MapInitOptions;)V
public synthetic fun <init> (Landroid/content/Context;Landroid/view/Surface;Lcom/mapbox/maps/MapInitOptions;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun addWidget (Lcom/mapbox/maps/renderer/widget/Widget;)V
public fun getMapboxMap ()Lcom/mapbox/maps/MapboxMap;
public fun getPlugin (Ljava/lang/String;)Lcom/mapbox/maps/plugin/MapPlugin;
public final fun getSurface ()Landroid/view/Surface;
public fun onDestroy ()V
public fun onGenericMotionEvent (Landroid/view/MotionEvent;)Z
public fun onLowMemory ()V
Expand All @@ -93,6 +97,7 @@ public final class com/mapbox/maps/MapSurface : com/mapbox/maps/MapControllable,
public fun onStop ()V
public fun onTouchEvent (Landroid/view/MotionEvent;)Z
public fun queueEvent (Ljava/lang/Runnable;Z)V
public fun removeWidget (Lcom/mapbox/maps/renderer/widget/Widget;)Z
public fun setMaximumFps (I)V
public fun setOnFpsChangedListener (Lcom/mapbox/maps/renderer/OnFpsChangedListener;)V
public fun snapshot ()Landroid/graphics/Bitmap;
Expand All @@ -109,6 +114,7 @@ public class com/mapbox/maps/MapView : android/widget/FrameLayout, com/mapbox/ma
public fun <init> (Landroid/content/Context;Landroid/util/AttributeSet;I)V
public fun <init> (Landroid/content/Context;Lcom/mapbox/maps/MapInitOptions;)V
public synthetic fun <init> (Landroid/content/Context;Lcom/mapbox/maps/MapInitOptions;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun addWidget (Lcom/mapbox/maps/renderer/widget/Widget;)V
public final fun createPlugin (Lcom/mapbox/maps/plugin/Plugin;)V
public fun getMapboxMap ()Lcom/mapbox/maps/MapboxMap;
public fun getPlugin (Ljava/lang/String;)Lcom/mapbox/maps/plugin/MapPlugin;
Expand All @@ -124,6 +130,7 @@ public class com/mapbox/maps/MapView : android/widget/FrameLayout, com/mapbox/ma
public fun onStop ()V
public fun onTouchEvent (Landroid/view/MotionEvent;)Z
public fun queueEvent (Ljava/lang/Runnable;Z)V
public fun removeWidget (Lcom/mapbox/maps/renderer/widget/Widget;)Z
public fun setMaximumFps (I)V
public fun setOnFpsChangedListener (Lcom/mapbox/maps/renderer/OnFpsChangedListener;)V
public fun snapshot ()Landroid/graphics/Bitmap;
Expand Down Expand Up @@ -496,6 +503,46 @@ public final class com/mapbox/maps/renderer/RenderHandlerThread$sam$i$java_lang_
public final synthetic fun run ()V
}

public class com/mapbox/maps/renderer/widget/BitmapWidget : com/mapbox/maps/renderer/widget/Widget {
public fun <init> (Landroid/graphics/Bitmap;)V
public fun <init> (Landroid/graphics/Bitmap;Lcom/mapbox/maps/renderer/widget/WidgetPosition;)V
public fun <init> (Landroid/graphics/Bitmap;Lcom/mapbox/maps/renderer/widget/WidgetPosition;F)V
public fun <init> (Landroid/graphics/Bitmap;Lcom/mapbox/maps/renderer/widget/WidgetPosition;FF)V
public synthetic fun <init> (Landroid/graphics/Bitmap;Lcom/mapbox/maps/renderer/widget/WidgetPosition;FFILkotlin/jvm/internal/DefaultConstructorMarker;)V
public synthetic fun getRenderer$sdk_release ()Lcom/mapbox/maps/renderer/widget/WidgetRenderer;
public fun setRotation (F)V
public fun setTranslation (FF)V
public final fun updateBitmap (Landroid/graphics/Bitmap;)V
}

public abstract class com/mapbox/maps/renderer/widget/Widget {
public fun <init> ()V
public abstract fun setRotation (F)V
public abstract fun setTranslation (FF)V
}

public final class com/mapbox/maps/renderer/widget/WidgetPosition {
public fun <init> (Lcom/mapbox/maps/renderer/widget/WidgetPosition$Horizontal;Lcom/mapbox/maps/renderer/widget/WidgetPosition$Vertical;)V
public final fun getHorizontal ()Lcom/mapbox/maps/renderer/widget/WidgetPosition$Horizontal;
public final fun getVertical ()Lcom/mapbox/maps/renderer/widget/WidgetPosition$Vertical;
}

public final class com/mapbox/maps/renderer/widget/WidgetPosition$Horizontal : java/lang/Enum {
public static final field CENTER Lcom/mapbox/maps/renderer/widget/WidgetPosition$Horizontal;
public static final field LEFT Lcom/mapbox/maps/renderer/widget/WidgetPosition$Horizontal;
public static final field RIGHT Lcom/mapbox/maps/renderer/widget/WidgetPosition$Horizontal;
public static fun valueOf (Ljava/lang/String;)Lcom/mapbox/maps/renderer/widget/WidgetPosition$Horizontal;
public static fun values ()[Lcom/mapbox/maps/renderer/widget/WidgetPosition$Horizontal;
}

public final class com/mapbox/maps/renderer/widget/WidgetPosition$Vertical : java/lang/Enum {
public static final field BOTTOM Lcom/mapbox/maps/renderer/widget/WidgetPosition$Vertical;
public static final field CENTER Lcom/mapbox/maps/renderer/widget/WidgetPosition$Vertical;
public static final field TOP Lcom/mapbox/maps/renderer/widget/WidgetPosition$Vertical;
public static fun valueOf (Ljava/lang/String;)Lcom/mapbox/maps/renderer/widget/WidgetPosition$Vertical;
public static fun values ()[Lcom/mapbox/maps/renderer/widget/WidgetPosition$Vertical;
}

public abstract interface class com/mapbox/maps/viewannotation/ViewAnnotationManager {
public abstract fun addViewAnnotation (ILcom/mapbox/maps/ViewAnnotationOptions;)Landroid/view/View;
public abstract fun addViewAnnotation (ILcom/mapbox/maps/ViewAnnotationOptions;Landroidx/asynclayoutinflater/view/AsyncLayoutInflater;Lkotlin/jvm/functions/Function1;)V
Expand Down
15 changes: 15 additions & 0 deletions sdk/src/main/java/com/mapbox/maps/MapControllable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.mapbox.maps
import android.graphics.Bitmap
import android.view.MotionEvent
import com.mapbox.maps.renderer.OnFpsChangedListener
import com.mapbox.maps.renderer.widget.Widget

/**
* MapControllable interface is the gateway for public API to talk to the internal map controller.
Expand Down Expand Up @@ -73,4 +74,18 @@ interface MapControllable : MapboxLifecycleObserver {
* Set [OnFpsChangedListener] to get map rendering FPS.
*/
fun setOnFpsChangedListener(listener: OnFpsChangedListener)

/**
* Add [Widget] to the map.
*/
@MapboxExperimental
fun addWidget(widget: Widget)

/**
* Remove [Widget] from the map.
*
* @return true if widget was present and removed, false otherwise
*/
@MapboxExperimental
fun removeWidget(widget: Widget): Boolean
}
7 changes: 7 additions & 0 deletions sdk/src/main/java/com/mapbox/maps/MapController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import com.mapbox.maps.plugin.scalebar.ScaleBarPluginImpl
import com.mapbox.maps.plugin.viewport.ViewportPluginImpl
import com.mapbox.maps.renderer.MapboxRenderer
import com.mapbox.maps.renderer.OnFpsChangedListener
import com.mapbox.maps.renderer.widget.Widget
import java.lang.ref.WeakReference

internal class MapController : MapPluginProviderDelegate, MapControllable {
Expand Down Expand Up @@ -205,6 +206,12 @@ internal class MapController : MapPluginProviderDelegate, MapControllable {
renderer.setOnFpsChangedListener(listener)
}

override fun addWidget(widget: Widget) {
renderer.renderThread.addWidget(widget)
}

override fun removeWidget(widget: Widget) = renderer.renderThread.removeWidget(widget)

//
// Telemetry
//
Expand Down
Loading