|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.maps.android.utils.demo |
| 18 | + |
| 19 | +import android.graphics.Canvas |
| 20 | +import android.graphics.Color |
| 21 | +import android.view.ViewGroup |
| 22 | +import android.widget.SeekBar |
| 23 | +import androidx.core.content.ContextCompat |
| 24 | +import androidx.core.graphics.createBitmap |
| 25 | +import androidx.core.graphics.toColorInt |
| 26 | +import androidx.lifecycle.MutableLiveData |
| 27 | +import androidx.lifecycle.lifecycleScope |
| 28 | +import com.google.android.gms.maps.CameraUpdateFactory |
| 29 | +import com.google.android.gms.maps.model.BitmapDescriptor |
| 30 | +import com.google.android.gms.maps.model.BitmapDescriptorFactory |
| 31 | +import com.google.android.gms.maps.model.LatLng |
| 32 | +import com.google.android.gms.maps.model.LatLngBounds |
| 33 | +import com.google.android.gms.maps.model.Marker |
| 34 | +import com.google.android.gms.maps.model.MarkerOptions |
| 35 | +import com.google.android.gms.maps.model.Polyline |
| 36 | +import com.google.android.gms.maps.model.PolylineOptions |
| 37 | +import com.google.maps.android.SphericalUtil |
| 38 | +import com.google.maps.android.utils.demo.databinding.ActivityPolylineProgressDemoBinding |
| 39 | +import kotlinx.coroutines.Job |
| 40 | +import kotlinx.coroutines.delay |
| 41 | +import kotlinx.coroutines.launch |
| 42 | + |
| 43 | +/** |
| 44 | + * This demo showcases how to animate a marker along a geodesic polyline, illustrating |
| 45 | + * key features of the Android Maps Utils library and modern Android development practices. |
| 46 | + */ |
| 47 | +class PolylineProgressDemoActivity : BaseDemoActivity(), SeekBar.OnSeekBarChangeListener { |
| 48 | + |
| 49 | + companion object { |
| 50 | + private const val POLYLINE_WIDTH = 15f |
| 51 | + private const val PROGRESS_POLYLINE_WIDTH = 7f |
| 52 | + private const val ANIMATION_STEP_SIZE = 1 |
| 53 | + private const val ANIMATION_DELAY_MS = 75L |
| 54 | + } |
| 55 | + |
| 56 | + private lateinit var binding: ActivityPolylineProgressDemoBinding |
| 57 | + private lateinit var originalPolyline: Polyline |
| 58 | + private var progressPolyline: Polyline? = null |
| 59 | + private var progressMarker: Marker? = null |
| 60 | + |
| 61 | + private val planeIcon: BitmapDescriptor by lazy { |
| 62 | + bitmapDescriptorFromVector(R.drawable.baseline_airplanemode_active_24, "#FFD700".toColorInt()) |
| 63 | + } |
| 64 | + |
| 65 | + private data class AnimationState(val progress: Int, val direction: Int) |
| 66 | + |
| 67 | + private val animationState = MutableLiveData<AnimationState>() |
| 68 | + private var animationJob: Job? = null |
| 69 | + |
| 70 | + private val polylinePoints = listOf( |
| 71 | + LatLng(40.7128, -74.0060), // New York |
| 72 | + LatLng(47.6062, -122.3321), // Seattle |
| 73 | + LatLng(39.7392, -104.9903), // Denver |
| 74 | + LatLng(37.7749, -122.4194), // San Francisco |
| 75 | + LatLng(34.0522, -118.2437), // Los Angeles |
| 76 | + LatLng(41.8781, -87.6298), // Chicago |
| 77 | + LatLng(29.7604, -95.3698), // Houston |
| 78 | + LatLng(39.9526, -75.1652) // Philadelphia |
| 79 | + ) |
| 80 | + |
| 81 | + override fun getLayoutId(): Int = R.layout.activity_polyline_progress_demo |
| 82 | + |
| 83 | + /** |
| 84 | + * This is where the demo begins. It is called from the base activity's `onMapReady` callback. |
| 85 | + */ |
| 86 | + override fun startDemo(isRestore: Boolean) { |
| 87 | + // The layout is already inflated by the base class. We can now bind to it. |
| 88 | + val rootView = (findViewById<ViewGroup>(android.R.id.content)).getChildAt(0) |
| 89 | + binding = ActivityPolylineProgressDemoBinding.bind(rootView) |
| 90 | + |
| 91 | + setupMap() |
| 92 | + setupUI() |
| 93 | + // Set the initial state. The observer in setupUI will handle the first UI update. |
| 94 | + animationState.value = AnimationState(progress = 0, direction = 1) |
| 95 | + startAnimation() |
| 96 | + } |
| 97 | + |
| 98 | + private fun setupMap() { |
| 99 | + originalPolyline = map.addPolyline( |
| 100 | + PolylineOptions() |
| 101 | + .addAll(polylinePoints) |
| 102 | + .color(Color.GRAY) |
| 103 | + .width(POLYLINE_WIDTH) |
| 104 | + .geodesic(true) // A geodesic polyline follows the curvature of the Earth. |
| 105 | + ) |
| 106 | + |
| 107 | + val bounds = LatLngBounds.builder().apply { |
| 108 | + polylinePoints.forEach { include(it) } |
| 109 | + }.build() |
| 110 | + map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100)) |
| 111 | + } |
| 112 | + |
| 113 | + private fun setupUI() { |
| 114 | + binding.seekBar.setOnSeekBarChangeListener(this) |
| 115 | + binding.resetButton.setOnClickListener { |
| 116 | + stopAnimation() |
| 117 | + animationState.value = AnimationState(progress = 0, direction = 1) |
| 118 | + startAnimation() |
| 119 | + } |
| 120 | + binding.pauseButton.setOnClickListener { |
| 121 | + if (animationJob?.isActive == true) { |
| 122 | + stopAnimation() |
| 123 | + } else { |
| 124 | + startAnimation() |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + animationState.observe(this) { state -> |
| 129 | + binding.seekBar.progress = state.progress |
| 130 | + binding.percentageTextView.text = getString(R.string.percentage_format, state.progress) |
| 131 | + updateProgressOnMap(state.progress / 100.0, state.direction) |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private fun startAnimation() { |
| 136 | + stopAnimation() |
| 137 | + val currentState = animationState.value ?: return |
| 138 | + |
| 139 | + animationJob = lifecycleScope.launch { |
| 140 | + var progress = currentState.progress |
| 141 | + var direction = currentState.direction |
| 142 | + while (true) { |
| 143 | + progress = when { |
| 144 | + progress > 100 -> { |
| 145 | + direction = -1 |
| 146 | + 100 |
| 147 | + } |
| 148 | + progress < 0 -> { |
| 149 | + direction = 1 |
| 150 | + 0 |
| 151 | + } |
| 152 | + else -> progress + direction * ANIMATION_STEP_SIZE |
| 153 | + } |
| 154 | + |
| 155 | + animationState.postValue(AnimationState(progress, direction)) |
| 156 | + delay(ANIMATION_DELAY_MS) |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + private fun stopAnimation() { |
| 162 | + animationJob?.cancel() |
| 163 | + animationJob = null |
| 164 | + } |
| 165 | + |
| 166 | + override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { |
| 167 | + if (fromUser) { |
| 168 | + stopAnimation() |
| 169 | + animationState.value = AnimationState(progress, animationState.value?.direction ?: 1) |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + override fun onStartTrackingTouch(seekBar: SeekBar?) { /* No-op */ } |
| 174 | + |
| 175 | + override fun onStopTrackingTouch(seekBar: SeekBar?) { /* No-op */ } |
| 176 | + |
| 177 | + private fun updateProgressOnMap(percentage: Double, direction: Int) { |
| 178 | + progressPolyline?.remove() |
| 179 | + |
| 180 | + val prefix = SphericalUtil.getPolylinePrefix(polylinePoints, percentage) |
| 181 | + if (prefix.isNotEmpty()) { |
| 182 | + progressPolyline = map.addPolyline( |
| 183 | + PolylineOptions() |
| 184 | + .addAll(prefix) |
| 185 | + .color(Color.BLUE) |
| 186 | + .width(PROGRESS_POLYLINE_WIDTH) |
| 187 | + .zIndex(1f) |
| 188 | + .geodesic(true) |
| 189 | + ) |
| 190 | + } |
| 191 | + |
| 192 | + SphericalUtil.getPointOnPolyline(polylinePoints, percentage)?.let { point -> |
| 193 | + updateMarker(point, percentage, direction) |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + private fun updateMarker(point: LatLng, percentage: Double, direction: Int) { |
| 198 | + val heading = SphericalUtil.getPointOnPolyline(polylinePoints, percentage + 0.0001) |
| 199 | + ?.let { SphericalUtil.computeHeading(point, it) } |
| 200 | + ?.let { if (direction == -1) it + 180 else it } // Adjust for reverse direction. |
| 201 | + |
| 202 | + if (progressMarker == null) { |
| 203 | + progressMarker = map.addMarker( |
| 204 | + MarkerOptions() |
| 205 | + .position(point) |
| 206 | + .flat(true) |
| 207 | + .draggable(false) |
| 208 | + .icon(planeIcon) |
| 209 | + .apply { heading?.let { rotation(it.toFloat()) } } |
| 210 | + ) |
| 211 | + } else { |
| 212 | + progressMarker?.also { |
| 213 | + it.position = point |
| 214 | + heading?.let { newHeading -> it.rotation = newHeading.toFloat() } |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + private fun bitmapDescriptorFromVector(vectorResId: Int, color: Int): BitmapDescriptor { |
| 220 | + val vectorDrawable = ContextCompat.getDrawable(this, vectorResId)!! |
| 221 | + vectorDrawable.setTint(color) |
| 222 | + val bitmap = createBitmap( |
| 223 | + vectorDrawable.intrinsicWidth, |
| 224 | + vectorDrawable.intrinsicHeight |
| 225 | + ) |
| 226 | + val canvas = Canvas(bitmap) |
| 227 | + vectorDrawable.setBounds(0, 0, canvas.width, canvas.height) |
| 228 | + vectorDrawable.draw(canvas) |
| 229 | + return BitmapDescriptorFactory.fromBitmap(bitmap) |
| 230 | + } |
| 231 | +} |
0 commit comments