Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@

package com.google.maps.android.utils.demo

import android.os.Build
import android.util.DisplayMetrics
import android.view.View
import android.view.WindowManager
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Spinner
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.MapView
import com.google.android.gms.maps.model.LatLng
import com.google.maps.android.clustering.ClusterManager
import com.google.maps.android.clustering.algo.AbstractAlgorithm
import com.google.maps.android.clustering.algo.CentroidNonHierarchicalDistanceBasedAlgorithm
import com.google.maps.android.clustering.algo.ContinuousZoomEuclideanCentroidAlgorithm
import com.google.maps.android.clustering.algo.GridBasedAlgorithm
Expand All @@ -40,7 +41,6 @@ import kotlin.random.Random
class ClusterAlgorithmsDemoActivity : BaseDemoActivity() {

private var clusterManager: ClusterManager<MyItem>? = null
private lateinit var mapView: MapView

override fun getLayoutId(): Int {
return R.layout.activity_cluster_algorithms_demo
Expand Down Expand Up @@ -85,6 +85,28 @@ class ClusterAlgorithmsDemoActivity : BaseDemoActivity() {
* Sets up the ClusterManager with the chosen algorithm and populates it with items.
*/
private fun setupClusterer(algorithmPosition: Int) {

val windowManager = getSystemService(WINDOW_SERVICE) as WindowManager
val metrics = DisplayMetrics()
val width: Int
val height: Int

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// For devices with Android 11 (API 30) and above
val windowMetrics = windowManager.currentWindowMetrics
width = windowMetrics.bounds.width()
height = windowMetrics.bounds.height()
metrics.density = resources.displayMetrics.density
} else {
// For devices below Android 11
windowManager.defaultDisplay.getMetrics(metrics)
width = metrics.widthPixels
height = metrics.heightPixels
}

val widthDp = (width / metrics.density).toInt()
val heightDp = (height / metrics.density).toInt()

// 1. Clear the map and previous cluster manager
map.clear()

Expand All @@ -96,9 +118,11 @@ class ClusterAlgorithmsDemoActivity : BaseDemoActivity() {
1 -> GridBasedAlgorithm()
2 -> NonHierarchicalDistanceBasedAlgorithm()
3 -> CentroidNonHierarchicalDistanceBasedAlgorithm()
4 -> NonHierarchicalViewBasedAlgorithm(mapView.width, mapView.height)
4 -> NonHierarchicalViewBasedAlgorithm(widthDp, heightDp)
5 -> ContinuousZoomEuclideanCentroidAlgorithm()
else -> error("Unsupported algorithm position: $algorithmPosition")
else -> {
GridBasedAlgorithm()
}
}

// 4. Point the map's listeners to the ClusterManager
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
core-ktx = "1.16.0"
robolectric = "4.15.1"
kxml2 = "2.3.0"
androidx-test-core = "1.5.0"
mockk = "1.14.5"
lint = "31.12.0"
org-jacoco-core = "0.8.13"
Expand All @@ -43,6 +44,7 @@
core-ktx = { module = "androidx.core:core-ktx", version.ref = "core-ktx" }
robolectric = { module = "org.robolectric:robolectric", version.ref = "robolectric" }
kxml2 = { module = "net.sf.kxml:kxml2", version.ref = "kxml2" }
androidx-test-core = { module = "androidx.test:core", version.ref = "androidx-test-core" }
mockk = { module = "io.mockk:mockk", version.ref = "mockk" }
lint-api = { module = "com.android.tools.lint:lint-api", version.ref = "lint" }
lint-checks = { module = "com.android.tools.lint:lint-checks", version.ref = "lint" }
Expand Down
1 change: 1 addition & 0 deletions library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ dependencies {
testImplementation(libs.mockk)
testImplementation(libs.kotlin.test)
testImplementation(libs.truth)
testImplementation(libs.androidx.test.core)
implementation(libs.kotlin.stdlib.jdk8)
}

Expand Down
110 changes: 0 additions & 110 deletions library/src/main/java/com/google/maps/android/ui/AnimationUtil.java

This file was deleted.

101 changes: 101 additions & 0 deletions library/src/main/java/com/google/maps/android/ui/AnimationUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright 2025 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.maps.android.ui

import android.os.Handler
import android.os.Looper
import android.os.SystemClock
import android.view.animation.AccelerateDecelerateInterpolator
import android.view.animation.Interpolator
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.Marker
import kotlin.math.abs
import kotlin.math.sign

/**
* Animation utilities for markers with Maps API.
*/
object AnimationUtil {
/**
* Animates a marker from it's current position to the provided finalPosition
*
* @param marker marker to animate
* @param finalPosition the final position of the marker after the animation
*/
@JvmStatic
fun animateMarkerTo(marker: Marker, finalPosition: LatLng) {
animateMarkerTo(marker, finalPosition, 2000) // delegate to new version
}

/**
* Animates a marker from its current position to the provided finalPosition.
*
* @param marker marker to animate
* @param finalPosition the final position of the marker after the animation
* @param durationInMs the duration of the animation in milliseconds
*/
@JvmStatic
fun animateMarkerTo(
marker: Marker,
finalPosition: LatLng,
durationInMs: Long
) {
val latLngInterpolator: LatLngInterpolator = LatLngInterpolator.Linear()
val startPosition = marker.position
val handler = Handler(Looper.getMainLooper())
val start = SystemClock.uptimeMillis()
val interpolator: Interpolator = AccelerateDecelerateInterpolator()
handler.post(object : Runnable {
var elapsed: Long = 0
var t = 0f
var v = 0f
override fun run() {
// Calculate progress using interpolator
elapsed = SystemClock.uptimeMillis() - start
t = elapsed / durationInMs.toFloat()
v = interpolator.getInterpolation(t)
marker.position = latLngInterpolator.interpolate(v, startPosition, finalPosition)

// Repeat till progress is complete.
if (t < 1) {
// Post again 16ms later.
handler.postDelayed(this, 16)
}
}
})
}

/**
* For other LatLngInterpolator interpolators, see [this link](https://gist.github.com/broady/6314689)
*/
fun interface LatLngInterpolator {
fun interpolate(fraction: Float, a: LatLng, b: LatLng): LatLng
class Linear : LatLngInterpolator {
override fun interpolate(fraction: Float, a: LatLng, b: LatLng): LatLng {
val lat = (b.latitude - a.latitude) * fraction + a.latitude
var lngDelta = b.longitude - a.longitude

// Take the shortest path across the 180th meridian.
if (abs(lngDelta) > 180) {
lngDelta -= sign(lngDelta) * 360
}
val lng = lngDelta * fraction + a.longitude
return LatLng(lat, lng)
}
}
}
}

This file was deleted.

Loading