Skip to content
Closed
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
@@ -1,5 +1,7 @@
package com.margelo.nitro.nitromaps

import android.graphics.Color
import com.google.android.gms.maps.model.Circle
import com.google.android.gms.maps.model.CircleOptions
import com.google.android.gms.maps.model.LatLng

Expand All @@ -15,3 +17,13 @@ fun CircleDescriptor.toCircleOptions(): CircleOptions {

return options
}

/** Updates an existing circle in place, with the same defaults as [toCircleOptions]. */
fun CircleDescriptor.applyTo(circle: Circle) {
circle.center = LatLng(center.latitude, center.longitude)
circle.radius = radius
circle.strokeColor = strokeColor?.toColorInt() ?: Color.BLACK
circle.fillColor = fillColor?.toColorInt() ?: Color.TRANSPARENT
circle.strokeWidth = (strokeWidth ?: 2.0).toFloat()
circle.isClickable = tappable != false
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.GoogleMapOptions
import com.google.android.gms.maps.MapView
import com.google.android.gms.maps.model.CameraPosition
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.LatLngBounds
import com.google.android.gms.maps.model.MapStyleOptions
Expand All @@ -39,6 +40,8 @@ class GoogleMapProviderAdapter(
private var pendingPolygons: Array<PolygonDescriptor>? = null
private var pendingCircles: Array<CircleDescriptor>? = null
private val mainHandler = Handler(Looper.getMainLooper())
private var lastAppliedRegion: Region? = null
private var lastAppliedRegionCamera: CameraPosition? = null

private val googleMapIdAtCreation: String? = normalizeGoogleMapId(initialGoogleMapId)

Expand Down Expand Up @@ -573,19 +576,36 @@ class GoogleMapProviderAdapter(

private fun applyRegion(region: Region, animated: Boolean = false) {
val map = googleMap ?: return
val bounds = region.toLatLngBounds()
val paddingPx = _mapPadding.toPaddingPixels()
runWhenMapViewLaidOut { fitCamera(map, region, animated) }
}

val runUpdate = {
val update = CameraUpdateFactory.newLatLngBounds(bounds, paddingPx)
if (animated) {
map.animateCamera(update)
} else {
map.moveCamera(update)
}
private fun fitCamera(map: GoogleMap, region: Region, animated: Boolean) {
val lastRegion = lastAppliedRegion
val lastCamera = lastAppliedRegionCamera
if (
lastRegion != null &&
lastCamera != null &&
region.approximatelyEquals(lastRegion) &&
map.cameraPosition.approximatelyEquals(lastCamera)
) {
// Same region as last time and the camera has not moved since, so the
// fit would land on the camera the map already shows.
return
}

runWhenMapViewLaidOut(runUpdate)
val update = CameraUpdateFactory.newLatLngBounds(
region.toLatLngBounds(),
_mapPadding.toPaddingPixels(),
)
if (animated) {
map.animateCamera(update)
// The camera settles later; there is nothing reliable to remember yet.
lastAppliedRegionCamera = null
} else {
map.moveCamera(update)
lastAppliedRegionCamera = map.cameraPosition
}
lastAppliedRegion = region
}

private fun updateMapCamera(
Expand Down Expand Up @@ -749,6 +769,8 @@ class GoogleMapProviderAdapter(
_mapType = MapType.STANDARD
_region = null
_camera = null
lastAppliedRegion = null
lastAppliedRegionCamera = null
scrollEnabled = true
zoomEnabled = true
rotateEnabled = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.margelo.nitro.nitromaps

object MapApproximateEquality {
const val COORDINATE_EPSILON: Double = 1e-6
const val SPAN_EPSILON: Double = 1e-6
const val ZOOM_EPSILON: Float = 1e-4f
const val ANGLE_EPSILON: Float = 1e-3f
}
Loading
Loading