|
| 1 | +package org.maplibre.navigation.android.example |
| 2 | + |
| 3 | +import android.annotation.SuppressLint |
| 4 | +import android.os.Bundle |
| 5 | +import android.view.View |
| 6 | +import androidx.appcompat.app.AppCompatActivity |
| 7 | +import com.google.android.material.snackbar.Snackbar |
| 8 | +import com.google.gson.Gson |
| 9 | +import org.maplibre.geojson.model.Point |
| 10 | +import org.maplibre.android.annotations.MarkerOptions |
| 11 | +import org.maplibre.android.camera.CameraPosition |
| 12 | +import org.maplibre.android.geometry.LatLng |
| 13 | +import org.maplibre.android.location.LocationComponent |
| 14 | +import org.maplibre.android.location.LocationComponentActivationOptions |
| 15 | +import org.maplibre.android.location.modes.CameraMode |
| 16 | +import org.maplibre.android.location.modes.RenderMode |
| 17 | +import org.maplibre.android.maps.MapLibreMap |
| 18 | +import org.maplibre.android.maps.OnMapReadyCallback |
| 19 | +import org.maplibre.android.maps.Style |
| 20 | +import org.maplibre.navigation.android.example.databinding.ActivityNavigationUiBinding |
| 21 | +import org.maplibre.navigation.android.navigation.ui.v5.NavigationLauncher |
| 22 | +import org.maplibre.navigation.android.navigation.ui.v5.NavigationLauncherOptions |
| 23 | +import org.maplibre.navigation.core.models.DirectionsResponse |
| 24 | +import org.maplibre.navigation.core.models.DirectionsRoute |
| 25 | +import org.maplibre.navigation.core.models.RouteOptions |
| 26 | +import okhttp3.MediaType.Companion.toMediaType |
| 27 | +import okhttp3.OkHttpClient |
| 28 | +import okhttp3.Request |
| 29 | +import okhttp3.RequestBody.Companion.toRequestBody |
| 30 | +import org.maplibre.geojson.turf.TurfUnit |
| 31 | +import org.maplibre.navigation.android.navigation.ui.v5.route.NavigationMapRoute |
| 32 | +import timber.log.Timber |
| 33 | +import java.io.IOException |
| 34 | +import java.util.Locale |
| 35 | + |
| 36 | +class GraphHopperNavigationActivity : |
| 37 | + AppCompatActivity(), |
| 38 | + OnMapReadyCallback, |
| 39 | + MapLibreMap.OnMapClickListener { |
| 40 | + private lateinit var mapLibreMap: MapLibreMap |
| 41 | + |
| 42 | + // Navigation related variables |
| 43 | + private var language = Locale.getDefault().language |
| 44 | + private var route: DirectionsRoute? = null |
| 45 | + private var navigationMapRoute: NavigationMapRoute? = null |
| 46 | + private var destination: Point? = null |
| 47 | + private var locationComponent: LocationComponent? = null |
| 48 | + |
| 49 | + private lateinit var binding: ActivityNavigationUiBinding |
| 50 | + |
| 51 | + private var simulateRoute = false |
| 52 | + |
| 53 | + @SuppressLint("MissingPermission") |
| 54 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 55 | + super.onCreate(savedInstanceState) |
| 56 | + |
| 57 | + if (BuildConfig.DEBUG) { |
| 58 | + Timber.plant(Timber.DebugTree()) |
| 59 | + } |
| 60 | + |
| 61 | + binding = ActivityNavigationUiBinding.inflate(layoutInflater) |
| 62 | + setContentView(binding.root) |
| 63 | + binding.mapView.apply { |
| 64 | + onCreate(savedInstanceState) |
| 65 | + getMapAsync(this@GraphHopperNavigationActivity) |
| 66 | + } |
| 67 | + |
| 68 | + binding.startRouteButton.setOnClickListener { |
| 69 | + route?.let { route -> |
| 70 | + val userLocation = mapLibreMap.locationComponent.lastKnownLocation ?: return@let |
| 71 | + val options = NavigationLauncherOptions.builder() |
| 72 | + .directionsRoute(route) |
| 73 | + .shouldSimulateRoute(simulateRoute) |
| 74 | + .initialMapCameraPosition( |
| 75 | + CameraPosition.Builder() |
| 76 | + .target(LatLng(userLocation.latitude, userLocation.longitude)).build() |
| 77 | + ) |
| 78 | + .lightThemeResId(R.style.TestNavigationViewLight) |
| 79 | + .darkThemeResId(R.style.TestNavigationViewDark) |
| 80 | + .build() |
| 81 | + NavigationLauncher.startNavigation(this@GraphHopperNavigationActivity, options) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + binding.simulateRouteSwitch.setOnCheckedChangeListener { _, checked -> |
| 86 | + simulateRoute = checked |
| 87 | + } |
| 88 | + |
| 89 | + binding.clearPoints.setOnClickListener { |
| 90 | + if (::mapLibreMap.isInitialized) { |
| 91 | + mapLibreMap.markers.forEach { |
| 92 | + mapLibreMap.removeMarker(it) |
| 93 | + } |
| 94 | + } |
| 95 | + destination = null |
| 96 | + it.visibility = View.GONE |
| 97 | + binding.startRouteLayout.visibility = View.GONE |
| 98 | + |
| 99 | + navigationMapRoute?.removeRoute() |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + override fun onMapReady(mapLibreMap: MapLibreMap) { |
| 104 | + this.mapLibreMap = mapLibreMap |
| 105 | + mapLibreMap.setStyle( |
| 106 | + Style.Builder().fromUri(getString(R.string.map_style_light)) |
| 107 | + ) { style -> |
| 108 | + enableLocationComponent(style) |
| 109 | + navigationMapRoute = NavigationMapRoute(binding.mapView, mapLibreMap) |
| 110 | + mapLibreMap.addOnMapClickListener(this) |
| 111 | + |
| 112 | + Snackbar.make( |
| 113 | + findViewById(R.id.container), |
| 114 | + "Tap map to place destination", |
| 115 | + Snackbar.LENGTH_LONG, |
| 116 | + ).show() |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + @SuppressWarnings("MissingPermission") |
| 121 | + private fun enableLocationComponent(style: Style) { |
| 122 | + // Get an instance of the component |
| 123 | + locationComponent = mapLibreMap.locationComponent |
| 124 | + |
| 125 | + locationComponent?.let { |
| 126 | + // Activate with a built LocationComponentActivationOptions object |
| 127 | + it.activateLocationComponent( |
| 128 | + LocationComponentActivationOptions.builder(this, style).build(), |
| 129 | + ) |
| 130 | + |
| 131 | + // Enable to make component visible |
| 132 | + it.isLocationComponentEnabled = true |
| 133 | + |
| 134 | + // Set the component's camera mode |
| 135 | + it.cameraMode = CameraMode.TRACKING_GPS_NORTH |
| 136 | + |
| 137 | + // Set the component's render mode |
| 138 | + it.renderMode = RenderMode.NORMAL |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + override fun onMapClick(point: LatLng): Boolean { |
| 143 | + destination = Point(point.longitude, point.latitude) |
| 144 | + |
| 145 | + mapLibreMap.addMarker(MarkerOptions().position(point)) |
| 146 | + binding.clearPoints.visibility = View.VISIBLE |
| 147 | + calculateRoute() |
| 148 | + return true |
| 149 | + } |
| 150 | + |
| 151 | + private fun calculateRoute() { |
| 152 | + binding.startRouteLayout.visibility = View.GONE |
| 153 | + val userLocation = mapLibreMap.locationComponent.lastKnownLocation |
| 154 | + val destination = destination |
| 155 | + if (userLocation == null) { |
| 156 | + Timber.d("calculateRoute: User location is null, therefore, origin can't be set.") |
| 157 | + return |
| 158 | + } |
| 159 | + |
| 160 | + if (destination == null) { |
| 161 | + Timber.d("calculateRoute: destination is null, therefore, destination can't be set.") |
| 162 | + return |
| 163 | + } |
| 164 | + |
| 165 | + val origin = Point(userLocation.longitude, userLocation.latitude) |
| 166 | + if (org.maplibre.geojson.turf.TurfMeasurement.distance(origin, destination, TurfUnit.METRES) < 50) { |
| 167 | + Timber.d("calculateRoute: distance < 50 m") |
| 168 | + binding.startRouteButton.visibility = View.GONE |
| 169 | + return |
| 170 | + } |
| 171 | + |
| 172 | + // The full GraphHopper API is documented here: |
| 173 | + // https://docs.graphhopper.com/openapi/routing |
| 174 | + val requestBody = mapOf( |
| 175 | + "type" to "mapbox", |
| 176 | + "profile" to "car", |
| 177 | + "locale" to language, |
| 178 | + "points" to listOf( |
| 179 | + listOf(origin.longitude, origin.latitude), |
| 180 | + listOf(destination.longitude, destination.latitude) |
| 181 | + ) |
| 182 | + ) |
| 183 | + |
| 184 | + val requestBodyJson = Gson().toJson(requestBody) |
| 185 | + val client = OkHttpClient() |
| 186 | + |
| 187 | + // Create request object. Requires graphhopper_url to be set in developer-config.xml |
| 188 | + val request = Request.Builder() |
| 189 | + .header("User-Agent", "MapLibre Android Navigation SDK Demo App") |
| 190 | + .url(getString(R.string.graphhopper_url)) |
| 191 | + .post(requestBodyJson.toRequestBody("application/json; charset=utf-8".toMediaType())) |
| 192 | + .build() |
| 193 | + |
| 194 | + Timber.d("calculateRoute enqueued requestBodyJson: %s", requestBodyJson) |
| 195 | + client.newCall(request).enqueue(object : okhttp3.Callback { |
| 196 | + |
| 197 | + override fun onFailure(call: okhttp3.Call, e: IOException) { |
| 198 | + Timber.e(e, "calculateRoute Failed to get route from GraphHopperRouting") |
| 199 | + } |
| 200 | + |
| 201 | + override fun onResponse(call: okhttp3.Call, response: okhttp3.Response) { |
| 202 | + response.use { |
| 203 | + if (response.isSuccessful) { |
| 204 | + Timber.e( |
| 205 | + "calculateRoute to GraphHopperRouting successful with status code: %s", |
| 206 | + response.code |
| 207 | + ) |
| 208 | + val responseBodyJson = response.body!!.string() |
| 209 | + Timber.d( |
| 210 | + "calculateRoute GraphHopperRouting responseBodyJson: %s", |
| 211 | + responseBodyJson |
| 212 | + ) |
| 213 | + val maplibreResponse = DirectionsResponse.fromJson(responseBodyJson); |
| 214 | + this@GraphHopperNavigationActivity.route = maplibreResponse.routes |
| 215 | + .first() |
| 216 | + .copy( |
| 217 | + routeOptions = RouteOptions( |
| 218 | + // See ValhallaNavigationActivity why these dummy route options are necessary |
| 219 | + baseUrl = "https://valhalla.routing", |
| 220 | + profile = "valhalla", |
| 221 | + user = "valhalla", |
| 222 | + accessToken = "valhalla", |
| 223 | + voiceInstructions = true, |
| 224 | + bannerInstructions = true, |
| 225 | + language = language, |
| 226 | + coordinates = listOf(origin, destination), |
| 227 | + requestUuid = "0000-0000-0000-0000" |
| 228 | + ) |
| 229 | + ) |
| 230 | + |
| 231 | + runOnUiThread { |
| 232 | + navigationMapRoute?.addRoutes(maplibreResponse.routes) |
| 233 | + binding.startRouteLayout.visibility = View.VISIBLE |
| 234 | + } |
| 235 | + } else { |
| 236 | + Timber.e( |
| 237 | + "calculateRoute Request to GraphHopper failed with status code: %s: %s", |
| 238 | + response.code, |
| 239 | + response.body |
| 240 | + ) |
| 241 | + } |
| 242 | + } |
| 243 | + } |
| 244 | + }) |
| 245 | + } |
| 246 | + |
| 247 | + override fun onResume() { |
| 248 | + super.onResume() |
| 249 | + binding.mapView.onResume() |
| 250 | + } |
| 251 | + |
| 252 | + override fun onPause() { |
| 253 | + super.onPause() |
| 254 | + binding.mapView.onPause() |
| 255 | + } |
| 256 | + |
| 257 | + override fun onStart() { |
| 258 | + super.onStart() |
| 259 | + binding.mapView.onStart() |
| 260 | + } |
| 261 | + |
| 262 | + override fun onStop() { |
| 263 | + super.onStop() |
| 264 | + binding.mapView.onStop() |
| 265 | + } |
| 266 | + |
| 267 | + override fun onLowMemory() { |
| 268 | + super.onLowMemory() |
| 269 | + binding.mapView.onLowMemory() |
| 270 | + } |
| 271 | + |
| 272 | + override fun onDestroy() { |
| 273 | + super.onDestroy() |
| 274 | + if (::mapLibreMap.isInitialized) { |
| 275 | + mapLibreMap.removeOnMapClickListener(this) |
| 276 | + } |
| 277 | + binding.mapView.onDestroy() |
| 278 | + } |
| 279 | + |
| 280 | + override fun onSaveInstanceState(outState: Bundle) { |
| 281 | + super.onSaveInstanceState(outState) |
| 282 | + binding.mapView.onSaveInstanceState(outState) |
| 283 | + } |
| 284 | +} |
0 commit comments