Skip to content

Commit

Permalink
fix(andorid): fix potential null mapView when applying camera (#3586)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfazekas authored Aug 10, 2024
1 parent 22155a1 commit cdce1cd
Showing 1 changed file with 23 additions and 22 deletions.
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

0 comments on commit cdce1cd

Please sign in to comment.