From 720823e5d40ce69890692350c549857d2671bf2b Mon Sep 17 00:00:00 2001 From: Marvin W Date: Sat, 4 May 2024 18:15:18 +0200 Subject: [PATCH] Maps: Calculate missing fields in GroundOverlay --- .../gms/maps/mapbox/model/GroundOverlay.kt | 34 +++++++++++++++++++ .../gms/maps/model/GroundOverlayOptions.java | 2 +- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/play-services-maps/core/mapbox/src/main/kotlin/org/microg/gms/maps/mapbox/model/GroundOverlay.kt b/play-services-maps/core/mapbox/src/main/kotlin/org/microg/gms/maps/mapbox/model/GroundOverlay.kt index 0c99d4dd4a..20bf63215f 100644 --- a/play-services-maps/core/mapbox/src/main/kotlin/org/microg/gms/maps/mapbox/model/GroundOverlay.kt +++ b/play-services-maps/core/mapbox/src/main/kotlin/org/microg/gms/maps/mapbox/model/GroundOverlay.kt @@ -16,8 +16,11 @@ import com.google.android.gms.maps.model.LatLngBounds import com.google.android.gms.maps.model.internal.IGroundOverlayDelegate import org.microg.gms.maps.mapbox.GoogleMapImpl import org.microg.gms.utils.warnOnTransactionIssues +import kotlin.math.cos +import kotlin.math.min class GroundOverlayImpl(private val map: GoogleMapImpl, private val id: String, options: GroundOverlayOptions) : IGroundOverlayDelegate.Stub() { + private val image: BitmapDescriptorImpl? = options.image?.remoteObject?.unwrap() private var location: LatLng? = options.location private var width: Float = options.width private var height: Float = options.height @@ -26,9 +29,39 @@ class GroundOverlayImpl(private val map: GoogleMapImpl, private val id: String, private var zIndex: Float = options.zIndex private var visible: Boolean = options.isVisible private var transparency: Float = options.transparency + private var anchorU: Float = options.anchorU + private var anchorV: Float = options.anchorV private var clickable: Boolean = options.isClickable private var tag: Any? = null + init { + // Compute missing values + if (height < 0 || height.isNaN()) { + val imageHeight = image?.size?.getOrNull(0) ?: -1f + val imageWidth = image?.size?.getOrNull(1) ?: -1f + if (imageHeight >= 0 && imageWidth > 0) { + height = (imageHeight / imageWidth) * width + } + } + if (bounds == null && width >= 0 && height >= 0 && location != null) { + val latitudeSpan = Math.toDegrees(height.toDouble() / EARTH_RADIUS) + val longitudeSpan = min(Math.toDegrees(width.toDouble() / (cos(Math.toRadians(location!!.latitude)) * EARTH_RADIUS)), 359.999999) + val north = location!!.latitude + (latitudeSpan * anchorV) + val south = location!!.latitude - (latitudeSpan * (1.0 - anchorV)) + val west = location!!.longitude - (longitudeSpan * anchorU) + val east = location!!.longitude + (longitudeSpan * (1.0 - anchorU)) + bounds = LatLngBounds(LatLng(south, west), LatLng(north, east)) + } + if (location == null && bounds != null) { + val north = bounds!!.northeast.latitude + val south = bounds!!.southwest.latitude + var east = bounds!!.northeast.longitude + val west = bounds!!.southwest.longitude + while (east < west) east += 360.0 + location = LatLng((1.0 - anchorV) * north + anchorV * south, (1.0 - anchorU) * west + anchorU * east) + } + } + override fun getId(): String { return id } @@ -136,5 +169,6 @@ class GroundOverlayImpl(private val map: GoogleMapImpl, private val id: String, companion object { private const val TAG = "GroundOverlay" + private const val EARTH_RADIUS = 6371009.0 } } diff --git a/play-services-maps/src/main/java/com/google/android/gms/maps/model/GroundOverlayOptions.java b/play-services-maps/src/main/java/com/google/android/gms/maps/model/GroundOverlayOptions.java index 9cf169d225..159d78b4c8 100644 --- a/play-services-maps/src/main/java/com/google/android/gms/maps/model/GroundOverlayOptions.java +++ b/play-services-maps/src/main/java/com/google/android/gms/maps/model/GroundOverlayOptions.java @@ -35,7 +35,7 @@ public class GroundOverlayOptions extends AutoSafeParcelable { @Field(4) private float width; @Field(5) - private float height; + private float height = NO_DIMENSION; @Field(6) private LatLngBounds bounds; @Field(7)