-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Projection.kt
77 lines (66 loc) · 2.87 KB
/
Projection.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* SPDX-FileCopyrightText: 2023 microG Project Team
* SPDX-License-Identifier: Apache-2.0
*/
package org.microg.gms.maps.hms
import android.graphics.Point
import android.util.Log
import com.google.android.gms.dynamic.IObjectWrapper
import com.google.android.gms.dynamic.ObjectWrapper
import com.google.android.gms.dynamic.unwrap
import com.google.android.gms.maps.internal.IProjectionDelegate
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.LatLngBounds
import com.google.android.gms.maps.model.VisibleRegion
import com.huawei.hms.maps.Projection
import org.microg.gms.maps.hms.utils.toGms
import org.microg.gms.maps.hms.utils.toHms
private const val TAG = "GmsMapProjection"
class ProjectionImpl(private val projection: Projection) : IProjectionDelegate.Stub() {
private var lastVisibleRegion: VisibleRegion? = null
override fun fromScreenLocation(obj: IObjectWrapper?): LatLng? {
Log.d(TAG, "fromScreenLocation")
return try {
obj.unwrap<Point>()?.let {
projection.fromScreenLocation(it).toGms()
}
} catch (e: Exception) {
Log.d(TAG, "fromScreenLocation() used from outside UI thread on map with tilt or bearing, expect bugs")
LatLng(0.0, 0.0)
}
}
override fun toScreenLocation(latLng: LatLng?): IObjectWrapper {
Log.d(TAG, "toScreenLocation: $latLng")
return try {
ObjectWrapper.wrap(latLng?.toHms()?.let {
projection.toScreenLocation(it).let { Point(it.x, it.y) }
})
} catch (e: Exception) {
Log.d(TAG, "toScreenLocation() used from outside UI thread on map with tilt or bearing, expect bugs")
ObjectWrapper.wrap(Point(0, 0))
}
}
override fun getVisibleRegion(): VisibleRegion? {
val visibleRegion = projection.visibleRegion
if (visibleRegion.farLeft.latitude.isNaN() || visibleRegion.farLeft.longitude.isNaN()) {
return lastVisibleRegion
}
lastVisibleRegion = visibleRegion.toGms()
Log.d(TAG, "getVisibleRegion: $visibleRegion")
return lastVisibleRegion
}
}
class DummyProjection : IProjectionDelegate.Stub() {
override fun fromScreenLocation(obj: IObjectWrapper?): LatLng {
Log.d(TAG, "Map not initialized when calling getProjection(). Cannot calculate fromScreenLocation")
return LatLng(0.0, 0.0)
}
override fun toScreenLocation(latLng: LatLng?): IObjectWrapper {
Log.d(TAG, "Map not initialized when calling getProjection(). Cannot calculate toScreenLocation")
return ObjectWrapper.wrap(Point(0, 0))
}
override fun getVisibleRegion(): VisibleRegion {
Log.d(TAG, "Map not initialized when calling getProjection(). Cannot calculate getVisibleRegion")
return VisibleRegion(LatLngBounds(LatLng(0.0, 0.0), LatLng(0.0, 0.0)))
}
}