Skip to content

Commit 39c994f

Browse files
authored
Merge pull request react-native-maps#2479 from marcelkalveram/master
add edgePadding to fitToSuppliedMarkers function
2 parents 69f771b + c928c71 commit 39c994f

File tree

7 files changed

+33
-8
lines changed

7 files changed

+33
-8
lines changed

docs/mapview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ To access event data, you will need to use `e.nativeEvent`. For example, `onPres
7676
| `animateToViewingAngle` | `angle: Number`, `duration: Number` |
7777
| `setMapBoundaries` | `northEast: LatLng`, `southWest: LatLng` | `GoogleMaps only`
7878
| `fitToElements` | `animated: Boolean` |
79-
| `fitToSuppliedMarkers` | `markerIDs: String[]`, `animated: Boolean` | If you need to use this in `ComponentDidMount`, make sure you put it in a timeout or it will cause performance problems.
79+
| `fitToSuppliedMarkers` | `markerIDs: String[], options: { edgePadding: EdgePadding, animated: Boolean }` | If you need to use this in `ComponentDidMount`, make sure you put it in a timeout or it will cause performance problems.
8080
| `fitToCoordinates` | `coordinates: Array<LatLng>, options: { edgePadding: EdgePadding, animated: Boolean }` | If called in `ComponentDidMount` in android, it will cause an exception. It is recommended to call it from the MapView `onLayout` event.
8181
| `pointForCoordinate` | `coordinate: LatLng` | Converts a map coordinate to a view coordinate (`Point`). Returns a `Promise<Point>`.
8282
| `coordinateForPoint` | `point: Point` | Converts a view coordinate (`Point`) to a map coordinate. Returns a `Promise<Coordinate>`.

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ declare module "react-native-maps" {
221221
animateToBearing(bearing: number, duration?: number): void;
222222
animateToViewingAngle(angle: number, duration?: number): void;
223223
fitToElements(animated: boolean): void;
224-
fitToSuppliedMarkers(markers: string[], animated: boolean): void;
224+
fitToSuppliedMarkers(markers: string[], options?: { edgePadding?: EdgePadding, animated?: boolean }): void;
225225
fitToCoordinates(coordinates?: LatLng[], options?: { edgePadding?: EdgePadding, animated?: boolean }): void;
226226
setMapBoundaries(northEast: LatLng, southWest: LatLng): void;
227227
takeSnapshot(options?: SnapshotOptions): Promise<string>;

lib/android/src/main/java/com/airbnb/android/react/maps/AirMapManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ public void receiveCommand(AirMapView view, int commandId, @Nullable ReadableArr
303303
break;
304304

305305
case FIT_TO_SUPPLIED_MARKERS:
306-
view.fitToSuppliedMarkers(args.getArray(0), args.getBoolean(1));
306+
view.fitToSuppliedMarkers(args.getArray(0), args.getMap(1), args.getBoolean(2));
307307
break;
308308

309309
case FIT_TO_COORDINATES:

lib/android/src/main/java/com/airbnb/android/react/maps/AirMapView.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ public void fitToElements(boolean animated) {
703703
}
704704
}
705705

706-
public void fitToSuppliedMarkers(ReadableArray markerIDsArray, boolean animated) {
706+
public void fitToSuppliedMarkers(ReadableArray markerIDsArray, ReadableMap edgePadding, boolean animated) {
707707
if (map == null) return;
708708

709709
LatLngBounds.Builder builder = new LatLngBounds.Builder();
@@ -731,6 +731,12 @@ public void fitToSuppliedMarkers(ReadableArray markerIDsArray, boolean animated)
731731
if (addedPosition) {
732732
LatLngBounds bounds = builder.build();
733733
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, baseMapPadding);
734+
735+
if (edgePadding != null) {
736+
map.setPadding(edgePadding.getInt("left"), edgePadding.getInt("top"),
737+
edgePadding.getInt("right"), edgePadding.getInt("bottom"));
738+
}
739+
734740
if (animated) {
735741
map.animateCamera(cu);
736742
} else {

lib/components/MapView.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,8 +573,13 @@ class MapView extends React.Component {
573573
this._runCommand('fitToElements', [animated]);
574574
}
575575

576-
fitToSuppliedMarkers(markers, animated) {
577-
this._runCommand('fitToSuppliedMarkers', [markers, animated]);
576+
fitToSuppliedMarkers(markers, options = {}) {
577+
const {
578+
edgePadding = { top: 0, right: 0, bottom: 0, left: 0 },
579+
animated = true,
580+
} = options;
581+
582+
this._runCommand('fitToSuppliedMarkers', [markers, edgePadding, animated]);
578583
}
579584

580585
fitToCoordinates(coordinates = [], options = {}) {

lib/ios/AirGoogleMaps/AIRGoogleMapManager.m

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ - (UIView *)view
200200

201201
RCT_EXPORT_METHOD(fitToSuppliedMarkers:(nonnull NSNumber *)reactTag
202202
markers:(nonnull NSArray *)markers
203+
edgePadding:(nonnull NSDictionary *)edgePadding
203204
animated:(BOOL)animated)
204205
{
205206
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
@@ -222,7 +223,13 @@ - (UIView *)view
222223
for (AIRGoogleMapMarker *marker in filteredMarkers)
223224
bounds = [bounds includingCoordinate:marker.realMarker.position];
224225

225-
[mapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:55.0f]];
226+
// Set Map viewport
227+
CGFloat top = [RCTConvert CGFloat:edgePadding[@"top"]];
228+
CGFloat right = [RCTConvert CGFloat:edgePadding[@"right"]];
229+
CGFloat bottom = [RCTConvert CGFloat:edgePadding[@"bottom"]];
230+
CGFloat left = [RCTConvert CGFloat:edgePadding[@"left"]];
231+
232+
[mapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:55.0f withEdgeInsets:UIEdgeInsetsMake(top, left, bottom, right)]];
226233
}
227234
}];
228235
}

lib/ios/AirMaps/AIRMapManager.m

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ - (UIView *)view
252252

253253
RCT_EXPORT_METHOD(fitToSuppliedMarkers:(nonnull NSNumber *)reactTag
254254
markers:(nonnull NSArray *)markers
255+
edgePadding:(nonnull NSDictionary *)edgePadding
255256
animated:(BOOL)animated)
256257
{
257258
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
@@ -270,7 +271,13 @@ - (UIView *)view
270271

271272
NSArray *filteredMarkers = [mapView.annotations filteredArrayUsingPredicate:filterMarkers];
272273

273-
[mapView showAnnotations:filteredMarkers animated:animated];
274+
CGFloat top = [RCTConvert CGFloat:edgePadding[@"top"]];
275+
CGFloat right = [RCTConvert CGFloat:edgePadding[@"right"]];
276+
CGFloat bottom = [RCTConvert CGFloat:edgePadding[@"bottom"]];
277+
CGFloat left = [RCTConvert CGFloat:edgePadding[@"left"]];
278+
279+
[mapView showAnnotations:filteredMarkers edgePadding:UIEdgeInsetsMake(top, left, bottom, right) animated:animated];
280+
274281
}
275282
}];
276283
}

0 commit comments

Comments
 (0)