Skip to content

Commit 720823e

Browse files
committed
Maps: Calculate missing fields in GroundOverlay
1 parent 56ce1ee commit 720823e

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

play-services-maps/core/mapbox/src/main/kotlin/org/microg/gms/maps/mapbox/model/GroundOverlay.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ import com.google.android.gms.maps.model.LatLngBounds
1616
import com.google.android.gms.maps.model.internal.IGroundOverlayDelegate
1717
import org.microg.gms.maps.mapbox.GoogleMapImpl
1818
import org.microg.gms.utils.warnOnTransactionIssues
19+
import kotlin.math.cos
20+
import kotlin.math.min
1921

2022
class GroundOverlayImpl(private val map: GoogleMapImpl, private val id: String, options: GroundOverlayOptions) : IGroundOverlayDelegate.Stub() {
23+
private val image: BitmapDescriptorImpl? = options.image?.remoteObject?.unwrap()
2124
private var location: LatLng? = options.location
2225
private var width: Float = options.width
2326
private var height: Float = options.height
@@ -26,9 +29,39 @@ class GroundOverlayImpl(private val map: GoogleMapImpl, private val id: String,
2629
private var zIndex: Float = options.zIndex
2730
private var visible: Boolean = options.isVisible
2831
private var transparency: Float = options.transparency
32+
private var anchorU: Float = options.anchorU
33+
private var anchorV: Float = options.anchorV
2934
private var clickable: Boolean = options.isClickable
3035
private var tag: Any? = null
3136

37+
init {
38+
// Compute missing values
39+
if (height < 0 || height.isNaN()) {
40+
val imageHeight = image?.size?.getOrNull(0) ?: -1f
41+
val imageWidth = image?.size?.getOrNull(1) ?: -1f
42+
if (imageHeight >= 0 && imageWidth > 0) {
43+
height = (imageHeight / imageWidth) * width
44+
}
45+
}
46+
if (bounds == null && width >= 0 && height >= 0 && location != null) {
47+
val latitudeSpan = Math.toDegrees(height.toDouble() / EARTH_RADIUS)
48+
val longitudeSpan = min(Math.toDegrees(width.toDouble() / (cos(Math.toRadians(location!!.latitude)) * EARTH_RADIUS)), 359.999999)
49+
val north = location!!.latitude + (latitudeSpan * anchorV)
50+
val south = location!!.latitude - (latitudeSpan * (1.0 - anchorV))
51+
val west = location!!.longitude - (longitudeSpan * anchorU)
52+
val east = location!!.longitude + (longitudeSpan * (1.0 - anchorU))
53+
bounds = LatLngBounds(LatLng(south, west), LatLng(north, east))
54+
}
55+
if (location == null && bounds != null) {
56+
val north = bounds!!.northeast.latitude
57+
val south = bounds!!.southwest.latitude
58+
var east = bounds!!.northeast.longitude
59+
val west = bounds!!.southwest.longitude
60+
while (east < west) east += 360.0
61+
location = LatLng((1.0 - anchorV) * north + anchorV * south, (1.0 - anchorU) * west + anchorU * east)
62+
}
63+
}
64+
3265
override fun getId(): String {
3366
return id
3467
}
@@ -136,5 +169,6 @@ class GroundOverlayImpl(private val map: GoogleMapImpl, private val id: String,
136169

137170
companion object {
138171
private const val TAG = "GroundOverlay"
172+
private const val EARTH_RADIUS = 6371009.0
139173
}
140174
}

play-services-maps/src/main/java/com/google/android/gms/maps/model/GroundOverlayOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class GroundOverlayOptions extends AutoSafeParcelable {
3535
@Field(4)
3636
private float width;
3737
@Field(5)
38-
private float height;
38+
private float height = NO_DIMENSION;
3939
@Field(6)
4040
private LatLngBounds bounds;
4141
@Field(7)

0 commit comments

Comments
 (0)