Skip to content

feat: Make CameraPosition properties nullable to support Bounds-based initialization#149

Open
arturgesiarz wants to merge 2 commits into143-determine-map-bounds-from-current-camera-positionfrom
143-optional-props
Open

feat: Make CameraPosition properties nullable to support Bounds-based initialization#149
arturgesiarz wants to merge 2 commits into143-determine-map-bounds-from-current-camera-positionfrom
143-optional-props

Conversation

@arturgesiarz
Copy link
Member

No description provided.

@arturgesiarz
Copy link
Member Author

arturgesiarz commented Mar 4, 2026

@arturgesiarz arturgesiarz force-pushed the 143-optional-props branch 4 times, most recently from 44ee69f to d9cdf0a Compare March 4, 2026 15:43
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the multiplatform CameraPosition model to allow bounds-based initialization by making coordinates and zoom nullable, and adapts platform-specific camera conversions to tolerate nulls.

Changes:

  • Make CameraPosition.coordinates and CameraPosition.zoom nullable with default null values, updating KDoc to describe bounds-vs-coordinates behavior.
  • Update iOS (MapKit + Google Maps) camera application code paths to handle nullable camera fields.
  • Update Android Google Maps camera conversion to handle nullable camera fields.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
kmp-maps/google-maps/src/iosMain/kotlin/com/swmansion/kmpmaps/googlemaps/Extensions.kt Uses bounds-fit when present; otherwise now falls back when coordinates/zoom are null.
kmp-maps/core/src/iosMain/kotlin/com/swmansion/kmpmaps/core/Extensions.kt MapKit region conversion now tolerates nullable coordinates/zoom (with fallbacks).
kmp-maps/core/src/commonMain/kotlin/com/swmansion/kmpmaps/core/MapTypes.kt Makes CameraPosition fields nullable and updates documentation for bounds-based positioning.
kmp-maps/core/src/androidMain/kotlin/com/swmansion/kmpmaps/core/Extensions.kt Android camera conversion now tolerates nullable coordinates/zoom (with fallbacks).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +219 to 223
val coordinates: Coordinates? = null,
val zoom: Float? = null,
val bounds: MapBounds? = null,
val androidCameraPosition: AndroidCameraPosition? = null,
val iosCameraPosition: IosCameraPosition? = null,
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CameraPosition can now be constructed with both coordinates and bounds null (because both are nullable with defaults). Downstream platform code falls back to (0,0)/zoom 0, which can silently move the camera to an unintended location. Consider enforcing the invariant in the model (e.g., require coordinates != null || bounds != null in an init block or via factory constructors) and/or explicitly handling the invalid state.

Copilot uses AI. Check for mistakes.
Comment on lines +65 to +66
bounds?.toLatLngBounds()?.center
?: LatLng(coordinates?.latitude ?: 0.0, coordinates?.longitude ?: 0.0)
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Falling back to (latitude, longitude) = (0, 0) when bounds is null but coordinates is also null can cause unexpected camera jumps to the Gulf of Guinea instead of failing fast. If coordinates is optional only to support bounds-based positioning, consider guarding this branch (e.g., return/no-op or throw) when both bounds and coordinates are null.

Suggested change
bounds?.toLatLngBounds()?.center
?: LatLng(coordinates?.latitude ?: 0.0, coordinates?.longitude ?: 0.0)
when {
bounds != null -> bounds.toLatLngBounds().center
coordinates != null -> LatLng(coordinates.latitude, coordinates.longitude)
else -> throw IllegalStateException("CameraPosition must have either bounds or coordinates")
}

Copilot uses AI. Check for mistakes.
Comment on lines +139 to +143
CLLocationCoordinate2DMake(coordinates?.latitude ?: 0.0, coordinates?.longitude ?: 0.0)
val span =
MKCoordinateSpanMake(
calculateLatitudeDelta(zoom),
calculateLongitudeDelta(zoom, coordinates.latitude),
calculateLatitudeDelta(zoom ?: 0.0f),
calculateLongitudeDelta(zoom ?: 0.0f, coordinates?.latitude ?: 0.0),
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When bounds is null, these Elvis fallbacks will treat a missing coordinates/zoom as (0,0)/0, which can silently move the camera to an unintended region. Consider explicitly rejecting/handling the "no bounds and no coordinates" case (fail fast or no-op) instead of defaulting to (0,0) here.

Copilot uses AI. Check for mistakes.
Comment on lines 297 to +301
CLLocationCoordinate2DMake(
position.coordinates.latitude,
position.coordinates.longitude,
position.coordinates?.latitude ?: 0.0,
position.coordinates?.longitude ?: 0.0,
),
zoom = position.zoom,
zoom = position.zoom ?: 0f,
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the non-bounds path, defaulting missing coordinates/zoom to (0,0)/0 can silently move the camera to an unintended location when CameraPosition is under-specified. If nullability is only meant to support bounds-based initialization, consider guarding this branch and rejecting (or no-oping) when both bounds and coordinates are null instead of applying a hardcoded fallback.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants