Skip to content

Commit 59a163c

Browse files
committed
fix(android): setCameraToCoordinates padding
1 parent aaad0d2 commit 59a163c

File tree

2 files changed

+11
-45
lines changed

2 files changed

+11
-45
lines changed

android/src/main/java/com/rngooglemapsplus/GoogleMapsViewImpl.kt

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ import com.rngooglemapsplus.extensions.toRnCamera
5353
import com.rngooglemapsplus.extensions.toRnLatLng
5454
import com.rngooglemapsplus.extensions.toRnLocation
5555
import com.rngooglemapsplus.extensions.toRnRegion
56-
import com.rngooglemapsplus.extensions.withPaddingPixels
5756
import idTag
5857
import tagData
5958
import java.io.ByteArrayInputStream
@@ -451,27 +450,25 @@ class GoogleMapsViewImpl(
451450
) = onUi {
452451
if (coordinates.isEmpty()) return@onUi
453452

454-
val w = mapView?.width ?: 0
455-
val h = mapView?.height ?: 0
453+
val bounds =
454+
LatLngBounds
455+
.builder()
456+
.apply {
457+
coordinates.forEach { include(it.toLatLng()) }
458+
}.build()
456459

457-
val builder = LatLngBounds.builder()
458-
coordinates.forEach { coord -> builder.include(coord.toLatLng()) }
460+
val previousMapPadding = mapPadding
461+
mapPadding = padding
459462

460-
val baseBounds = builder.build()
461-
val paddedBounds = baseBounds.withPaddingPixels(w, h, padding)
462-
463-
val adjustedWidth =
464-
(w - padding.left.dpToPx() - padding.right.dpToPx()).toInt().coerceAtLeast(0)
465-
val adjustedHeight =
466-
(h - padding.top.dpToPx() - padding.bottom.dpToPx()).toInt().coerceAtLeast(0)
467-
468-
val update = CameraUpdateFactory.newLatLngBounds(paddedBounds, adjustedWidth, adjustedHeight, 0)
463+
val update = CameraUpdateFactory.newLatLngBounds(bounds, 0)
469464

470465
if (animated) {
471466
googleMap?.animateCamera(update, durationMs, null)
472467
} else {
473468
googleMap?.moveCamera(update)
474469
}
470+
471+
mapPadding = previousMapPadding
475472
}
476473

477474
fun setCameraBounds(bounds: LatLngBounds?) =
Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,10 @@
11
package com.rngooglemapsplus.extensions
22

3-
import com.facebook.react.uimanager.PixelUtil.dpToPx
4-
import com.google.android.gms.maps.model.LatLng
53
import com.google.android.gms.maps.model.LatLngBounds
64
import com.rngooglemapsplus.RNLatLngBounds
7-
import com.rngooglemapsplus.RNMapPadding
85

96
fun LatLngBounds.toRnLatLngBounds(): RNLatLngBounds =
107
RNLatLngBounds(
118
northeast = northeast.toRnLatLng(),
129
southwest = southwest.toRnLatLng(),
1310
)
14-
15-
fun LatLngBounds.withPaddingPixels(
16-
mapWidthPx: Int,
17-
mapHeightPx: Int,
18-
padding: RNMapPadding,
19-
): LatLngBounds {
20-
val latSpan = northeast.latitude - southwest.latitude
21-
val lngSpan = northeast.longitude - southwest.longitude
22-
if (latSpan == 0.0 && lngSpan == 0.0) return this
23-
24-
val latPerPixel = if (mapHeightPx != 0) latSpan / mapHeightPx else 0.0
25-
val lngPerPixel = if (mapWidthPx != 0) lngSpan / mapWidthPx else 0.0
26-
27-
val builder = LatLngBounds.builder()
28-
builder.include(
29-
LatLng(
30-
northeast.latitude + (padding.top.dpToPx() * latPerPixel),
31-
northeast.longitude + (padding.right.dpToPx() * lngPerPixel),
32-
),
33-
)
34-
builder.include(
35-
LatLng(
36-
southwest.latitude - (padding.bottom.dpToPx() * latPerPixel),
37-
southwest.longitude - (padding.left.dpToPx() * lngPerPixel),
38-
),
39-
)
40-
return builder.build()
41-
}

0 commit comments

Comments
 (0)