Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(andorid): fix potential null mapView when applying camera #3586

Merged
merged 1 commit into from
Aug 10, 2024
Merged
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
Expand Up @@ -99,9 +99,11 @@ class RNMBXCamera(private val mContext: Context, private val mManager: RNMBXCame
override fun addToMap(mapView: RNMBXMapView) {
super.addToMap(mapView)
mapView.callIfAttachedToWindow {
setInitialCamera()
updateMaxBounds()
mCameraStop?.let { updateCamera(it) }
withMapView { mapView ->
setInitialCamera(mapView)
updateMaxBounds(mapView)
mCameraStop?.let { updateCamera(it, mapView) }
}
}
_observeViewportState(mapView.mapView)
_updateViewportState()
Expand All @@ -117,8 +119,8 @@ class RNMBXCamera(private val mContext: Context, private val mManager: RNMBXCame
fun setStop(stop: CameraStop) {
mCameraStop = stop
stop.setCallback(mCameraCallback)
if (mMapView != null) {
stop.let { updateCamera(it) }
withMapView { mapView ->
stop.let { updateCamera(it, mapView) }
}
}

Expand Down Expand Up @@ -172,25 +174,24 @@ class RNMBXCamera(private val mContext: Context, private val mManager: RNMBXCame

fun setMaxBounds(bounds: LatLngBounds?) {
mMaxBounds = bounds
updateMaxBounds()
withMapView { mapView ->
updateMaxBounds(mapView)
}
}


private fun updateMaxBounds() {
withMapView { mapView ->
val map = mapView.getMapboxMap()
val builder = CameraBoundsOptions.Builder()
builder.bounds(mMaxBounds?.toBounds())
builder.minZoom(mMinZoomLevel ?: 0.0) // Passing null does not reset this value.
builder.maxZoom(mMaxZoomLevel ?: 25.0) // Passing null does not reset this value.
map.setBounds(builder.build())
mCameraStop?.let { updateCamera(it) }
}
private fun updateMaxBounds(mapView: RNMBXMapView) {
val map = mapView.getMapboxMap()
val builder = CameraBoundsOptions.Builder()
builder.bounds(mMaxBounds?.toBounds())
builder.minZoom(mMinZoomLevel ?: 0.0) // Passing null does not reset this value.
builder.maxZoom(mMaxZoomLevel ?: 25.0) // Passing null does not reset this value.
map.setBounds(builder.build())
mCameraStop?.let { updateCamera(it, mapView) }
}

private fun setInitialCamera() {
private fun setInitialCamera(mapView: RNMBXMapView) {
mDefaultStop?.let {
val mapView = mMapView!!
val map = mapView.getMapboxMap()

it.setDuration(0)
Expand All @@ -200,9 +201,9 @@ class RNMBXCamera(private val mContext: Context, private val mManager: RNMBXCame
}
}

private fun updateCamera(cameraStop: CameraStop) {
private fun updateCamera(cameraStop: CameraStop, mapView: RNMBXMapView) {
mCameraUpdateQueue.offer(cameraStop)
mCameraUpdateQueue.execute(mMapView)
mCameraUpdateQueue.execute(mapView)
}

private fun updateUserLocation(isAnimated: Boolean) {
Expand Down Expand Up @@ -243,12 +244,12 @@ class RNMBXCamera(private val mContext: Context, private val mManager: RNMBXCame

fun setMinZoomLevel(zoomLevel: Double?) {
mMinZoomLevel = zoomLevel
updateMaxBounds()
withMapView { updateMaxBounds(it) }
}

fun setMaxZoomLevel(zoomLevel: Double?) {
mMaxZoomLevel = zoomLevel
updateMaxBounds()
withMapView { updateMaxBounds(it) }
}

fun setZoomLevel(zoomLevel: Double) {
Expand Down
Loading