-
Notifications
You must be signed in to change notification settings - Fork 16
/
GoogleMarker.kt
57 lines (46 loc) · 1.78 KB
/
GoogleMarker.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
* Copyright 2019 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
*/
package dev.icerock.moko.maps.google
import android.animation.ValueAnimator
import dev.icerock.moko.geo.LatLng
import dev.icerock.moko.maps.Marker
import kotlin.time.Duration
import kotlin.time.ExperimentalTime
actual class GoogleMarker(
private val gmsMarker: com.google.android.gms.maps.model.Marker
) : Marker {
override var position: LatLng
get() = gmsMarker.position.toGeoLatLng()
set(value) {
gmsMarker.position = value.toAndroidLatLng()
}
override var rotation: Float
get() = gmsMarker.rotation
set(value) {
gmsMarker.rotation = value
}
override fun delete() {
gmsMarker.remove()
}
@OptIn(ExperimentalTime::class)
override fun move(position: LatLng, rotation: Float, duration: Duration) {
val currentPosition = gmsMarker.position
val newPosition = position.toAndroidLatLng()
val latDiff = newPosition.latitude - currentPosition.latitude
val lngDiff = newPosition.longitude - currentPosition.longitude
val currentRotation = gmsMarker.rotation
val rotationDiff = rotation - currentRotation
val animator = ValueAnimator.ofFloat(0.0f, 1.0f)
animator.addUpdateListener { valueAnimator ->
val value = valueAnimator.animatedValue as Float
gmsMarker.position = com.google.android.gms.maps.model.LatLng(
currentPosition.latitude + latDiff * value,
currentPosition.longitude + lngDiff * value
)
gmsMarker.rotation = currentRotation + rotationDiff * value
}
animator.duration = duration.inMilliseconds.toLong()
animator.start()
}
}