Skip to content

Commit fcf583f

Browse files
feat!: unify map padding to use density-independent pixels (#628)
1 parent 0b1819c commit fcf583f

5 files changed

Lines changed: 23 additions & 16 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ Both `NavigationView` and `MapView` support the following props. Props marked wi
504504
| `mapId` | `string` | - | | Cloud-based map styling ID from Google Cloud Console |
505505
| `mapColorScheme` | `MapColorScheme` | `FOLLOW_SYSTEM` | | Color scheme for map tiles (FOLLOW_SYSTEM, LIGHT, DARK) |
506506
| `mapStyle` | `string` | - | | Custom map styling via JSON |
507-
| `mapPadding` | `Padding` | - | | Padding applied to the map in pixels |
507+
| `mapPadding` | `Padding` | - | | Padding applied to the map in density-independent pixels |
508508
| `initialCameraPosition` | `CameraPosition` | - | | Initial camera position when map loads |
509509
| `minZoomLevel` | `number` | - | | Minimum allowed zoom level |
510510
| `maxZoomLevel` | `number` | - | | Maximum allowed zoom level |

android/src/main/java/com/google/android/react/navsdk/NavAutoModule.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.facebook.react.bridge.UiThreadUtil;
2626
import com.facebook.react.bridge.WritableArray;
2727
import com.facebook.react.bridge.WritableMap;
28+
import com.facebook.react.uimanager.PixelUtil;
2829
import com.google.android.gms.maps.UiSettings;
2930
import com.google.android.gms.maps.model.CameraPosition;
3031
import com.google.android.gms.maps.model.Circle;
@@ -619,10 +620,10 @@ public void isAutoScreenAvailable(final Promise promise) {
619620

620621
@Override
621622
public void setMapPadding(double top, double left, double bottom, double right) {
622-
int topInt = (int) top;
623-
int leftInt = (int) left;
624-
int bottomInt = (int) bottom;
625-
int rightInt = (int) right;
623+
int topInt = Math.round(PixelUtil.toPixelFromDIP(top));
624+
int leftInt = Math.round(PixelUtil.toPixelFromDIP(left));
625+
int bottomInt = Math.round(PixelUtil.toPixelFromDIP(bottom));
626+
int rightInt = Math.round(PixelUtil.toPixelFromDIP(right));
626627
UiThreadUtil.runOnUiThread(
627628
() -> {
628629
if (mMapViewController == null) {

android/src/main/java/com/google/android/react/navsdk/NavViewManager.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.facebook.react.bridge.ReadableMap;
2626
import com.facebook.react.common.MapBuilder;
2727
import com.facebook.react.module.annotations.ReactModule;
28+
import com.facebook.react.uimanager.PixelUtil;
2829
import com.facebook.react.uimanager.SimpleViewManager;
2930
import com.facebook.react.uimanager.ThemedReactContext;
3031
import com.facebook.react.uimanager.ViewManagerDelegate;
@@ -462,14 +463,19 @@ public void setNavigationNightMode(FrameLayout view, int nightMode) {
462463
@ReactProp(name = "mapPadding")
463464
public void setMapPadding(FrameLayout view, @Nullable ReadableMap padding) {
464465
if (padding != null) {
465-
int top = padding.hasKey("top") ? padding.getInt("top") : 0;
466-
int left = padding.hasKey("left") ? padding.getInt("left") : 0;
467-
int bottom = padding.hasKey("bottom") ? padding.getInt("bottom") : 0;
468-
int right = padding.hasKey("right") ? padding.getInt("right") : 0;
469-
getMapControllerProperties(view.getId()).setPadding(top, left, bottom, right);
466+
getMapControllerProperties(view.getId())
467+
.setPadding(
468+
getPaddingInPixels(padding, "top"),
469+
getPaddingInPixels(padding, "left"),
470+
getPaddingInPixels(padding, "bottom"),
471+
getPaddingInPixels(padding, "right"));
470472
}
471473
}
472474

475+
private int getPaddingInPixels(ReadableMap padding, String edge) {
476+
return padding.hasKey(edge) ? Math.round(PixelUtil.toPixelFromDIP(padding.getDouble(edge))) : 0;
477+
}
478+
473479
@ReactProp(name = "mapStyle")
474480
public void setMapStyle(FrameLayout view, @Nullable String mapStyle) {
475481
if (mapStyle != null) {

src/maps/mapView/types.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,13 @@ export enum MapType {
213213
* Defines the padding options for a map.
214214
*/
215215
export interface Padding {
216-
/** Top padding in pixels. */
216+
/** Top padding in density-independent pixels. */
217217
top?: number;
218-
/** Left padding in pixels. */
218+
/** Left padding in density-independent pixels. */
219219
left?: number;
220-
/** Bottom padding in pixels. */
220+
/** Bottom padding in density-independent pixels. */
221221
bottom?: number;
222-
/** Right padding in pixels. */
222+
/** Right padding in density-independent pixels. */
223223
right?: number;
224224
}
225225

@@ -381,7 +381,7 @@ export interface MapViewController {
381381
moveCamera(cameraPosition: CameraPosition): void;
382382

383383
/**
384-
* Sets padding to the map.
384+
* Sets padding on the map in density-independent pixels.
385385
*
386386
* @param padding - An object defining padding for each side.
387387
* Example: { top: 10, left: 5, bottom: 15, right: 10 }

src/maps/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ export interface MapViewProps {
263263
readonly mapType?: MapViewType;
264264

265265
/**
266-
* Sets padding on the map in pixels.
266+
* Sets padding on the map in density-independent pixels.
267267
*/
268268
readonly mapPadding?: Padding;
269269

0 commit comments

Comments
 (0)