Basic Navigation + Maps SDK integration
activity_basic_navigation_sdk_only_layout
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/container"android:layout_width="match_parent"android:layout_height="match_parent"app:layout_behavior="@string/appbar_scrolling_view_behavior"tools:context=".core.BasicNavSdkOnlyActivity"> <com.mapbox.mapboxsdk.maps.MapViewandroid:id="@+id/mapView"android:layout_width="match_parent"android:layout_height="match_parent"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /> <ProgressBarandroid:id="@+id/route_retrieval_progress_spinner"android:layout_width="wrap_content"android:visibility="invisible"android:layout_height="wrap_content"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="@+id/mapView" /> </androidx.constraintlayout.widget.ConstraintLayout>
BasicNavSdkOnlyActivity.kt
package com.mapbox.navigation.examples.core import android.annotation.SuppressLintimport android.graphics.Color.parseColorimport android.os.Bundleimport android.view.View.INVISIBLEimport android.view.View.VISIBLEimport androidx.appcompat.app.AppCompatActivityimport androidx.core.content.ContextCompatimport com.google.android.material.snackbar.Snackbarimport com.google.android.material.snackbar.Snackbar.LENGTH_SHORTimport com.mapbox.api.directions.v5.DirectionsCriteriaimport com.mapbox.api.directions.v5.models.DirectionsRouteimport com.mapbox.api.directions.v5.models.RouteOptionsimport com.mapbox.geojson.LineStringimport com.mapbox.geojson.Pointimport com.mapbox.mapboxsdk.geometry.LatLngimport com.mapbox.mapboxsdk.location.LocationComponentActivationOptionsimport com.mapbox.mapboxsdk.location.modes.CameraModeimport com.mapbox.mapboxsdk.location.modes.RenderModeimport com.mapbox.mapboxsdk.maps.MapboxMapimport com.mapbox.mapboxsdk.maps.OnMapReadyCallbackimport com.mapbox.mapboxsdk.maps.Styleimport com.mapbox.mapboxsdk.style.expressions.Expression.colorimport com.mapbox.mapboxsdk.style.expressions.Expression.interpolateimport com.mapbox.mapboxsdk.style.expressions.Expression.lineProgressimport com.mapbox.mapboxsdk.style.expressions.Expression.linearimport com.mapbox.mapboxsdk.style.expressions.Expression.stopimport com.mapbox.mapboxsdk.style.layers.LineLayerimport com.mapbox.mapboxsdk.style.layers.Property.LINE_CAP_ROUNDimport com.mapbox.mapboxsdk.style.layers.Property.LINE_JOIN_ROUNDimport com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconImageimport com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineCapimport com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineGradientimport com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineJoinimport com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineWidthimport com.mapbox.mapboxsdk.style.layers.SymbolLayerimport com.mapbox.mapboxsdk.style.sources.GeoJsonOptionsimport com.mapbox.mapboxsdk.style.sources.GeoJsonSourceimport com.mapbox.mapboxsdk.utils.BitmapUtilsimport com.mapbox.navigation.base.internal.extensions.applyDefaultParamsimport com.mapbox.navigation.base.internal.extensions.coordinatesimport com.mapbox.navigation.core.MapboxNavigationimport com.mapbox.navigation.core.directions.session.RoutesRequestCallbackimport com.mapbox.navigation.examples.Rimport com.mapbox.navigation.examples.utils.Utilsimport com.mapbox.navigation.examples.utils.extensions.toPointimport kotlinx.android.synthetic.main.activity_basic_navigation_sdk_only_layout.*import timber.log.Timber /*** This activity shows how to combine the Mapbox Maps SDK with* ONLY the Navigation SDK. There is no Navigation UI SDK code* of any kind in this example.*/class BasicNavSdkOnlyActivity :AppCompatActivity(),OnMapReadyCallback,MapboxMap.OnMapLongClickListener { private var mapboxNavigation: MapboxNavigation? = nullprivate var mapboxMap: MapboxMap? = nullprivate val ORIGIN_COLOR = "#32a852" // Greenprivate val DESTINATION_COLOR = "#F84D4D" // Red @SuppressLint("MissingPermission")override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_basic_navigation_sdk_only_layout)mapView.onCreate(savedInstanceState)mapView.getMapAsync(this) val mapboxNavigationOptions = MapboxNavigation.defaultNavigationOptionsBuilder(this, Utils.getMapboxAccessToken(this)).build() mapboxNavigation = MapboxNavigation(mapboxNavigationOptions)Snackbar.make(container, R.string.msg_long_press_map_to_place_waypoint, LENGTH_SHORT).show()} override fun onMapReady(mapboxMap: MapboxMap) {mapboxMap.setStyle(Style.LIGHT) {this.mapboxMap = mapboxMap enableLocationComponent() // Add the click and route sourcesit.addSource(GeoJsonSource("CLICK_SOURCE"))it.addSource(GeoJsonSource("ROUTE_LINE_SOURCE_ID",GeoJsonOptions().withLineMetrics(true))) // Add the destination marker imageit.addImage("ICON_ID",BitmapUtils.getBitmapFromDrawable(ContextCompat.getDrawable(this,R.drawable.mapbox_marker_icon_default))!!) // Add the LineLayer below the LocationComponent's bottom layer, which is the// circular accuracy layer. The LineLayer will display the directions route.it.addLayerBelow(LineLayer("ROUTE_LAYER_ID", "ROUTE_LINE_SOURCE_ID").withProperties(lineCap(LINE_CAP_ROUND),lineJoin(LINE_JOIN_ROUND),lineWidth(6f),lineGradient(interpolate(linear(),lineProgress(),stop(0f, color(parseColor(ORIGIN_COLOR))),stop(1f, color(parseColor(DESTINATION_COLOR)))))),"mapbox-location-shadow-layer") // Add the SymbolLayer to show the destination markerit.addLayerAbove(SymbolLayer("CLICK_LAYER", "CLICK_SOURCE").withProperties(iconImage("ICON_ID")),"ROUTE_LAYER_ID") mapboxMap.addOnMapLongClickListener(this)Snackbar.make(container, R.string.msg_long_press_map_to_place_waypoint, LENGTH_SHORT).show()}} override fun onMapLongClick(latLng: LatLng): Boolean {route_retrieval_progress_spinner.visibility = VISIBLE// Place the destination marker at the map long click locationmapboxMap?.getStyle {val clickPointSource = it.getSourceAs<GeoJsonSource>("CLICK_SOURCE")clickPointSource?.setGeoJson(Point.fromLngLat(latLng.longitude, latLng.latitude))}mapboxMap?.locationComponent?.lastKnownLocation?.let { originLocation ->mapboxNavigation?.requestRoutes(RouteOptions.builder().applyDefaultParams().accessToken(Utils.getMapboxAccessToken(applicationContext)).coordinates(originLocation.toPoint(), null, latLng.toPoint()).alternatives(true).profile(DirectionsCriteria.PROFILE_DRIVING_TRAFFIC).build(),routesReqCallback)}return true} private val routesReqCallback = object : RoutesRequestCallback {override fun onRoutesReady(routes: List<DirectionsRoute>) {if (routes.isNotEmpty()) {Snackbar.make(container,String.format(getString(R.string.steps_in_route),routes[0].legs()?.get(0)?.steps()?.size),LENGTH_SHORT).show() // Update a gradient route LineLayer's source with the Maps SDK. This will// visually add/update the line on the map. All of this is being done// directly with Maps SDK code and NOT the Navigation UI SDK.mapboxMap?.getStyle {val clickPointSource = it.getSourceAs<GeoJsonSource>("ROUTE_LINE_SOURCE_ID")val routeLineString = LineString.fromPolyline(routes[0].geometry()!!,6)clickPointSource?.setGeoJson(routeLineString)}route_retrieval_progress_spinner.visibility = INVISIBLE} else {Snackbar.make(container, R.string.no_routes, LENGTH_SHORT).show()}} override fun onRoutesRequestFailure(throwable: Throwable, routeOptions: RouteOptions) {Timber.e("route request failure %s", throwable.toString())Snackbar.make(container, R.string.route_request_failed, LENGTH_SHORT).show()} override fun onRoutesRequestCanceled(routeOptions: RouteOptions) {Timber.d("route request canceled")}} /*** Enable the Maps SDK's LocationComponent*/private fun enableLocationComponent() {mapboxMap?.getStyle {mapboxMap?.locationComponent?.apply {activateLocationComponent(LocationComponentActivationOptions.builder(this@BasicNavSdkOnlyActivity,it).build())isLocationComponentEnabled = truecameraMode = CameraMode.TRACKINGrenderMode = RenderMode.COMPASS}}} override fun onStart() {super.onStart()mapView.onStart()} public override fun onResume() {super.onResume()mapView.onResume()} public override fun onPause() {super.onPause()mapView.onPause()} override fun onStop() {super.onStop()mapView.onStop()} override fun onDestroy() {super.onDestroy()mapboxNavigation?.onDestroy()mapView.onDestroy()} override fun onLowMemory() {super.onLowMemory()mapView.onLowMemory()} override fun onSaveInstanceState(outState: Bundle) {super.onSaveInstanceState(outState)mapView.onSaveInstanceState(outState)}}