Skip to content

Commit

Permalink
feat: Revisit the "Starting navigation" guide after removal of NK2 de…
Browse files Browse the repository at this point in the history
…pendencies

Implements: GOSDK-14453 Revisit the "Starting navigation" guide after removal of NK2 dependencies
  • Loading branch information
Serhii Shanddiuk committed Sep 21, 2023
1 parent e0900d8 commit e6a0ffb
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,121 @@
package com.tomtom.sdk.examples.navigation

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.tomtom.sdk.datamanagement.navigationtile.NavigationTileStore
import com.tomtom.sdk.datamanagement.navigationtile.NavigationTileStoreConfiguration
import com.tomtom.sdk.examples.BuildConfig.TOMTOM_API_KEY
import com.tomtom.sdk.examples.databinding.ActivityNavigationExamplesBinding
import com.tomtom.sdk.location.GeoLocation
import com.tomtom.sdk.location.GeoPoint
import com.tomtom.sdk.location.LocationProvider
import com.tomtom.sdk.location.simulation.SimulationLocationProvider
import com.tomtom.sdk.location.simulation.strategy.InterpolationStrategy
import com.tomtom.sdk.navigation.NavigationOptions
import com.tomtom.sdk.navigation.ProgressUpdatedListener
import com.tomtom.sdk.navigation.RoutePlan
import com.tomtom.sdk.navigation.online.Configuration
import com.tomtom.sdk.navigation.online.OnlineTomTomNavigationFactory
import com.tomtom.sdk.navigation.routereplanner.online.OnlineRouteReplannerFactory
import com.tomtom.sdk.routing.RoutePlanner
import com.tomtom.sdk.routing.RoutePlanningCallback
import com.tomtom.sdk.routing.RoutePlanningResponse
import com.tomtom.sdk.routing.RoutingFailure
import com.tomtom.sdk.routing.online.OnlineRoutePlanner
import com.tomtom.sdk.routing.options.Itinerary
import com.tomtom.sdk.routing.options.RoutePlanningOptions
import com.tomtom.sdk.routing.options.guidance.GuidanceOptions
import com.tomtom.sdk.routing.options.guidance.ProgressPoints
import com.tomtom.sdk.routing.route.Route
import com.tomtom.sdk.vehicle.Vehicle

class NavigationExamplesActivity : AppCompatActivity() {

private lateinit var binding: ActivityNavigationExamplesBinding
private lateinit var routePlanner: RoutePlanner
private lateinit var routePlanningOptions: RoutePlanningOptions
private lateinit var route: Route

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

binding = ActivityNavigationExamplesBinding.inflate(layoutInflater)
setContentView(binding.root)
planRoute()
}

private fun planRoute() {
routePlanner = OnlineRoutePlanner.create(context = this, apiKey = TOMTOM_API_KEY)
val amsterdam = GeoPoint(latitude = 52.37616, longitude = 4.90828)
val rotterdam = GeoPoint(latitude = 51.90546, longitude = 4.46662)
val itinerary = Itinerary(origin = amsterdam, destination = rotterdam)
routePlanningOptions = RoutePlanningOptions(
itinerary = itinerary,
guidanceOptions = GuidanceOptions(progressPoints = ProgressPoints.All),
vehicle = Vehicle.Car()
)
routePlanner.planRoute(
routePlanningOptions,
object : RoutePlanningCallback {
override fun onSuccess(result: RoutePlanningResponse) {
route = result.routes.first()
startNavigation()
}

override fun onFailure(failure: RoutingFailure) {
Log.e(TAG, "Unable to calculate a route: " + failure.message)
}

override fun onRoutePlanned(route: Route) = Unit
}
)
}

private fun createLocationProvider(): LocationProvider {
val routeGeoLocations = route.geometry.map { GeoLocation(it) }
val simulationStrategy = InterpolationStrategy(routeGeoLocations)
return SimulationLocationProvider.create(strategy = simulationStrategy)
.also { locationProvider ->
locationProvider.enable()
}
}

private fun createNavigationConfiguration(): Configuration {
val navigationTileStore = NavigationTileStore.create(
context = this,
navigationTileStoreConfig = NavigationTileStoreConfiguration(apiKey = TOMTOM_API_KEY)
)
val routeReplanner = OnlineRouteReplannerFactory.create(routePlanner)
val locationProvider = createLocationProvider()
return Configuration(
context = this,
locationProvider = locationProvider,
navigationTileStore = navigationTileStore,
routeReplanner = routeReplanner
)
}

private fun startNavigation() {
val configuration = createNavigationConfiguration()
val tomTomNavigation = OnlineTomTomNavigationFactory.create(configuration)

tomTomNavigation.addProgressUpdatedListener(progressUpdatedListener)

val routePlan = RoutePlan(route, routePlanningOptions)
val navigationOptions = NavigationOptions(routePlan)
tomTomNavigation.start(navigationOptions)
}

private val progressUpdatedListener = ProgressUpdatedListener { routeProgress ->
binding.navExampleList.text = String.format(
"Distance along the route: %s\nRemaining travel time: %s",
routeProgress.distanceAlongRoute,
routeProgress.remainingTime
)
}

companion object {
private const val TAG = "NavigationExamplesActivity"
}
}
5 changes: 2 additions & 3 deletions app/src/main/res/layout/activity_navigation_examples.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@

<TextView
android:id="@+id/navExampleList"
android:layout_width="216dp"
android:layout_height="44dp"
android:ems="10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/navigation_activity_title"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<string name="navigation">Navigation</string>
<string name="offline_maps">Offline</string>
<string name="map_demo_activity_title">Map display examples coming soon</string>
<string name="navigation_activity_title">Navigation examples coming soon</string>
<string name="navigation_activity_title">Starting navigation…</string>
<string name="offline_demo_activity_title">Offline examples coming soon</string>
<string name="routing_demo_activity_title">Routing examples coming soon</string>
<string name="search_demo_activity_title">Search examples coming soon</string>
Expand Down

0 comments on commit e6a0ffb

Please sign in to comment.