Skip to content

Commit bf50530

Browse files
feat: add map padding (#373)
Co-authored-by: Joonas Kerttula <[email protected]>
1 parent 8867894 commit bf50530

File tree

13 files changed

+147
-22
lines changed

13 files changed

+147
-22
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ public enum Command {
5454
REMOVE_CIRCLE(35, "removeCircle"),
5555
REMOVE_GROUND_OVERLAY(36, "removeGroundOverlay"),
5656
SET_ZOOM_CONTROLS_ENABLED(37, "setZoomControlsEnabled"),
57-
SET_RECENTER_BUTTON_ENABLED(38, "setRecenterButtonEnabled");
57+
SET_RECENTER_BUTTON_ENABLED(38, "setRecenterButtonEnabled"),
58+
SET_PADDING(39, "setPadding");
5859

5960
private final int value;
6061
private final String name;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,12 @@ public void setFollowingPerspective(int jsValue) {
536536
mGoogleMap.followMyLocation(EnumTranslationUtil.getCameraPerspectiveFromJsValue(jsValue));
537537
}
538538

539+
public void setPadding(int top, int left, int bottom, int right) {
540+
if (mGoogleMap != null) {
541+
mGoogleMap.setPadding(left, top, right, bottom);
542+
}
543+
}
544+
539545
private String fetchJsonFromUrl(String urlString) throws IOException {
540546
URL url = new URL(urlString);
541547
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,19 @@ public void isAutoScreenAvailable(final Promise promise) {
506506
promise.resolve(mMapViewController != null);
507507
}
508508

509+
@ReactMethod
510+
public void setPadding(
511+
final Integer top, final Integer left, final Integer bottom, final Integer right) {
512+
UiThreadUtil.runOnUiThread(
513+
() -> {
514+
if (mMapViewController == null) {
515+
return;
516+
}
517+
518+
mMapViewController.setPadding(top, left, bottom, right);
519+
});
520+
}
521+
509522
public void sendScreenState(boolean available) {
510523
WritableNativeArray params = new WritableNativeArray();
511524
params.pushBoolean(available);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ public Map<String, Integer> getCommandsMap() {
119119
map.put(REMOVE_GROUND_OVERLAY.toString(), REMOVE_GROUND_OVERLAY.getValue());
120120
map.put(SET_HEADER_ENABLED.toString(), SET_HEADER_ENABLED.getValue());
121121
map.put(SET_FOOTER_ENABLED.toString(), SET_FOOTER_ENABLED.getValue());
122+
map.put(SET_PADDING.toString(), SET_PADDING.getValue());
122123
return map;
123124
}
124125

@@ -298,6 +299,10 @@ public void receiveCommand(
298299
case REMOVE_GROUND_OVERLAY:
299300
getFragmentForRoot(root).getMapController().removeGroundOverlay(args.getString(0));
300301
break;
302+
case SET_PADDING:
303+
getFragmentForRoot(root)
304+
.getMapController()
305+
.setPadding(args.getInt(0), args.getInt(1), args.getInt(2), args.getInt(3));
301306
}
302307
}
303308

example/src/controls/mapsControls.tsx

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
type Circle,
2828
type Polyline,
2929
type Polygon,
30+
type Padding,
3031
} from '@googlemaps/react-native-navigation-sdk';
3132

3233
export interface MapControlsProps {
@@ -41,6 +42,12 @@ const MapsControls: React.FC<MapControlsProps> = ({ mapViewController }) => {
4142
const [enableLocationMarker, setEnableLocationMarker] = useState(true);
4243
const [latitude, onLatChanged] = useState('');
4344
const [longitude, onLngChanged] = useState('');
45+
const [padding, setPadding] = useState<Padding>({
46+
top: 0,
47+
bottom: 0,
48+
left: 0,
49+
right: 0,
50+
});
4451

4552
useEffect(() => {
4653
if (zoom !== null) {
@@ -195,6 +202,12 @@ const MapsControls: React.FC<MapControlsProps> = ({ mapViewController }) => {
195202
mapViewController.clearMapView();
196203
};
197204

205+
const onPaddingChanged = (edge: keyof typeof padding, value: string) => {
206+
const updatedPadding: Padding = { ...padding, [edge]: Number(value) };
207+
setPadding(updatedPadding);
208+
mapViewController.setPadding(updatedPadding);
209+
};
210+
198211
return (
199212
<View>
200213
<TextInput
@@ -280,6 +293,47 @@ const MapsControls: React.FC<MapControlsProps> = ({ mapViewController }) => {
280293
dropdownStyle={styles.dropdownMenuStyle}
281294
/>
282295
</View>
296+
<View style={styles.controlButtonGap} />
297+
<View style={styles.rowContainer}>
298+
<Text>Top padding</Text>
299+
<TextInput
300+
style={styles.input}
301+
onChangeText={value => onPaddingChanged('top', value)}
302+
value={padding.top?.toFixed(0)}
303+
keyboardType="numeric"
304+
inputMode="numeric"
305+
/>
306+
</View>
307+
<View style={styles.rowContainer}>
308+
<Text>Bottom padding</Text>
309+
<TextInput
310+
style={styles.input}
311+
onChangeText={value => onPaddingChanged('bottom', value)}
312+
value={padding.bottom?.toFixed(0)}
313+
keyboardType="numeric"
314+
inputMode="numeric"
315+
/>
316+
</View>
317+
<View style={styles.rowContainer}>
318+
<Text>Left padding</Text>
319+
<TextInput
320+
style={styles.input}
321+
onChangeText={value => onPaddingChanged('left', value)}
322+
value={padding.left?.toFixed(0)}
323+
keyboardType="numeric"
324+
inputMode="numeric"
325+
/>
326+
</View>
327+
<View style={styles.rowContainer}>
328+
<Text>Right padding</Text>
329+
<TextInput
330+
style={styles.input}
331+
onChangeText={value => onPaddingChanged('right', value)}
332+
value={padding.right?.toFixed(0)}
333+
keyboardType="numeric"
334+
inputMode="numeric"
335+
/>
336+
</View>
283337
</View>
284338
);
285339
};

example/src/styles.ts

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,42 +27,23 @@ const styles = StyleSheet.create({
2727
button: {
2828
backgroundColor: '#2196f3',
2929
},
30-
center: {
31-
alignItems: 'center',
32-
},
33-
toggleControl: {
34-
backgroundColor: '#9ee2ff',
35-
flexDirection: 'row',
36-
alignItems: 'center',
37-
alignSelf: 'baseline',
38-
position: 'absolute',
39-
right: 0,
40-
padding: 6,
41-
marginTop: 150,
42-
},
4330
input: {
4431
backgroundColor: '#ffffff',
4532
height: 40,
4633
margin: 12,
4734
borderWidth: 1,
4835
padding: 10,
49-
},
50-
zoomBtn: {
51-
color: '#fff',
36+
flexGrow: 1,
5237
},
5338
rowContainer: {
5439
margin: 5,
5540
marginLeft: 20,
5641
marginRight: 20,
5742
flexDirection: 'row',
5843
justifyContent: 'space-between',
44+
alignItems: 'center',
5945
color: 'white',
6046
},
61-
rowBtnContainer: {
62-
flexDirection: 'row',
63-
alignSelf: 'center',
64-
justifyContent: 'space-between',
65-
},
6647
controlButtons: {
6748
alignSelf: 'stretch',
6849
flexDirection: 'row',

ios/react-native-navigation-sdk/NavAutoModule.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,19 @@ + (void)unregisterNavAutoModuleReadyCallback {
387387
});
388388
}
389389

390+
RCT_EXPORT_METHOD(setPadding
391+
: (nonnull NSNumber *)top left
392+
: (nonnull NSNumber *)left bottom
393+
: (nonnull NSNumber *)bottom right
394+
: (nonnull NSNumber *)right) {
395+
dispatch_async(dispatch_get_main_queue(), ^{
396+
if (self->_viewController) {
397+
[self->_viewController setPadding:UIEdgeInsetsMake(top.floatValue, left.floatValue,
398+
bottom.floatValue, right.floatValue)];
399+
}
400+
});
401+
}
402+
390403
- (void)onScreenStateChange:(BOOL)available {
391404
[self sendCommandToReactNative:@"onAutoScreenAvailabilityChanged"
392405
args:[NSNumber numberWithBool:available]];

ios/react-native-navigation-sdk/NavViewController.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ typedef void (^OnArrayResult)(NSArray *_Nullable result);
8383
- (void)removeGroundOverlay:(NSString *)overlayId;
8484
- (BOOL)attachToNavigationSession:(GMSNavigationSession *)session;
8585
- (void)setTravelMode:(GMSNavigationTravelMode)travelMode;
86+
- (void)setPadding:(UIEdgeInsets)insets;
8687
//- (void)removeNavigationListeners;
8788
@end
8889

ios/react-native-navigation-sdk/NavViewController.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,4 +661,8 @@ - (BOOL)compare:(nullable id)userData to:(NSString *)elementId {
661661
return [userData[0] isEqualToString:elementId];
662662
}
663663

664+
- (void)setPadding:(UIEdgeInsets)insets {
665+
_mapView.padding = insets;
666+
}
667+
664668
@end

ios/react-native-navigation-sdk/RCTNavViewManager.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,4 +426,17 @@ - (void)unregisterViewControllerForTag:(NSNumber *)reactTag {
426426
});
427427
}
428428

429+
RCT_EXPORT_METHOD(setPadding
430+
: (nonnull NSNumber *)reactTag top
431+
: (nonnull NSNumber *)top left
432+
: (nonnull NSNumber *)left bottom
433+
: (nonnull NSNumber *)bottom right
434+
: (nonnull NSNumber *)right) {
435+
dispatch_async(dispatch_get_main_queue(), ^{
436+
NavViewController *viewController = [self getViewControllerForTag:reactTag];
437+
[viewController setPadding:UIEdgeInsetsMake(top.floatValue, left.floatValue, bottom.floatValue,
438+
right.floatValue)];
439+
});
440+
}
441+
429442
@end

0 commit comments

Comments
 (0)