Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add map padding #373

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public enum Command {
REMOVE_CIRCLE(35, "removeCircle"),
REMOVE_GROUND_OVERLAY(36, "removeGroundOverlay"),
SET_ZOOM_CONTROLS_ENABLED(37, "setZoomControlsEnabled"),
SET_RECENTER_BUTTON_ENABLED(38, "setRecenterButtonEnabled");
SET_RECENTER_BUTTON_ENABLED(38, "setRecenterButtonEnabled"),
SET_PADDING(39, "setPadding");

private final int value;
private final String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,12 @@ public void setFollowingPerspective(int jsValue) {
mGoogleMap.followMyLocation(EnumTranslationUtil.getCameraPerspectiveFromJsValue(jsValue));
}

public void setPadding(int top, int left, int bottom, int right) {
if (mGoogleMap != null) {
mGoogleMap.setPadding(left, top, right, bottom);
}
}

private String fetchJsonFromUrl(String urlString) throws IOException {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,19 @@ public void isAutoScreenAvailable(final Promise promise) {
promise.resolve(mMapViewController != null);
}

@ReactMethod
public void setPadding(
final Integer top, final Integer left, final Integer bottom, final Integer right) {
UiThreadUtil.runOnUiThread(
() -> {
if (mMapViewController == null) {
return;
}

mMapViewController.setPadding(top, left, bottom, right);
});
}

public void sendScreenState(boolean available) {
WritableNativeArray params = new WritableNativeArray();
params.pushBoolean(available);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public Map<String, Integer> getCommandsMap() {
map.put(REMOVE_GROUND_OVERLAY.toString(), REMOVE_GROUND_OVERLAY.getValue());
map.put(SET_HEADER_ENABLED.toString(), SET_HEADER_ENABLED.getValue());
map.put(SET_FOOTER_ENABLED.toString(), SET_FOOTER_ENABLED.getValue());
map.put(SET_PADDING.toString(), SET_PADDING.getValue());
return map;
}

Expand Down Expand Up @@ -298,6 +299,10 @@ public void receiveCommand(
case REMOVE_GROUND_OVERLAY:
getFragmentForRoot(root).getMapController().removeGroundOverlay(args.getString(0));
break;
case SET_PADDING:
getFragmentForRoot(root)
.getMapController()
.setPadding(args.getInt(0), args.getInt(1), args.getInt(2), args.getInt(3));
}
}

Expand Down
54 changes: 54 additions & 0 deletions example/src/controls/mapsControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
type Circle,
type Polyline,
type Polygon,
type Padding,
} from '@googlemaps/react-native-navigation-sdk';

export interface MapControlsProps {
Expand All @@ -41,6 +42,12 @@ const MapsControls: React.FC<MapControlsProps> = ({ mapViewController }) => {
const [enableLocationMarker, setEnableLocationMarker] = useState(true);
const [latitude, onLatChanged] = useState('');
const [longitude, onLngChanged] = useState('');
const [padding, setPadding] = useState<Padding>({
top: 0,
bottom: 0,
left: 0,
right: 0,
});

useEffect(() => {
if (zoom !== null) {
Expand Down Expand Up @@ -195,6 +202,12 @@ const MapsControls: React.FC<MapControlsProps> = ({ mapViewController }) => {
mapViewController.clearMapView();
};

const onPaddingChanged = (edge: keyof typeof padding, value: string) => {
const updatedPadding: Padding = { ...padding, [edge]: Number(value) };
setPadding(updatedPadding);
mapViewController.setPadding(updatedPadding);
};

return (
<View>
<TextInput
Expand Down Expand Up @@ -280,6 +293,47 @@ const MapsControls: React.FC<MapControlsProps> = ({ mapViewController }) => {
dropdownStyle={styles.dropdownMenuStyle}
/>
</View>
<View style={styles.controlButtonGap} />
<View style={styles.rowContainer}>
<Text>Top padding</Text>
<TextInput
style={styles.input}
onChangeText={value => onPaddingChanged('top', value)}
value={padding.top?.toFixed(0)}
keyboardType="numeric"
inputMode="numeric"
/>
</View>
<View style={styles.rowContainer}>
<Text>Bottom padding</Text>
<TextInput
style={styles.input}
onChangeText={value => onPaddingChanged('bottom', value)}
value={padding.bottom?.toFixed(0)}
keyboardType="numeric"
inputMode="numeric"
/>
</View>
<View style={styles.rowContainer}>
<Text>Left padding</Text>
<TextInput
style={styles.input}
onChangeText={value => onPaddingChanged('left', value)}
value={padding.left?.toFixed(0)}
keyboardType="numeric"
inputMode="numeric"
/>
</View>
<View style={styles.rowContainer}>
<Text>Right padding</Text>
<TextInput
style={styles.input}
onChangeText={value => onPaddingChanged('right', value)}
value={padding.right?.toFixed(0)}
keyboardType="numeric"
inputMode="numeric"
/>
</View>
</View>
);
};
Expand Down
23 changes: 2 additions & 21 deletions example/src/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,42 +27,23 @@ const styles = StyleSheet.create({
button: {
backgroundColor: '#2196f3',
},
center: {
alignItems: 'center',
},
toggleControl: {
backgroundColor: '#9ee2ff',
flexDirection: 'row',
alignItems: 'center',
alignSelf: 'baseline',
position: 'absolute',
right: 0,
padding: 6,
marginTop: 150,
},
input: {
backgroundColor: '#ffffff',
height: 40,
margin: 12,
borderWidth: 1,
padding: 10,
},
zoomBtn: {
color: '#fff',
flexGrow: 1,
},
rowContainer: {
margin: 5,
marginLeft: 20,
marginRight: 20,
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
color: 'white',
},
rowBtnContainer: {
flexDirection: 'row',
alignSelf: 'center',
justifyContent: 'space-between',
},
controlButtons: {
alignSelf: 'stretch',
flexDirection: 'row',
Expand Down
13 changes: 13 additions & 0 deletions ios/react-native-navigation-sdk/NavAutoModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,19 @@ + (void)unregisterNavAutoModuleReadyCallback {
});
}

RCT_EXPORT_METHOD(setPadding
: (nonnull NSNumber *)top left
: (nonnull NSNumber *)left bottom
: (nonnull NSNumber *)bottom right
: (nonnull NSNumber *)right) {
dispatch_async(dispatch_get_main_queue(), ^{
if (self->_viewController) {
[self->_viewController setPadding:UIEdgeInsetsMake(top.floatValue, left.floatValue,
bottom.floatValue, right.floatValue)];
}
});
}

- (void)onScreenStateChange:(BOOL)available {
[self sendCommandToReactNative:@"onAutoScreenAvailabilityChanged"
args:[NSNumber numberWithBool:available]];
Expand Down
1 change: 1 addition & 0 deletions ios/react-native-navigation-sdk/NavViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ typedef void (^OnArrayResult)(NSArray *_Nullable result);
- (void)removeGroundOverlay:(NSString *)overlayId;
- (BOOL)attachToNavigationSession:(GMSNavigationSession *)session;
- (void)setTravelMode:(GMSNavigationTravelMode)travelMode;
- (void)setPadding:(UIEdgeInsets)insets;
//- (void)removeNavigationListeners;
@end

Expand Down
4 changes: 4 additions & 0 deletions ios/react-native-navigation-sdk/NavViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -661,4 +661,8 @@ - (BOOL)compare:(nullable id)userData to:(NSString *)elementId {
return [userData[0] isEqualToString:elementId];
}

- (void)setPadding:(UIEdgeInsets)insets {
_mapView.padding = insets;
}

@end
13 changes: 13 additions & 0 deletions ios/react-native-navigation-sdk/RCTNavViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -426,4 +426,17 @@ - (void)unregisterViewControllerForTag:(NSNumber *)reactTag {
});
}

RCT_EXPORT_METHOD(setPadding
: (nonnull NSNumber *)reactTag top
: (nonnull NSNumber *)top left
: (nonnull NSNumber *)left bottom
: (nonnull NSNumber *)bottom right
: (nonnull NSNumber *)right) {
dispatch_async(dispatch_get_main_queue(), ^{
NavViewController *viewController = [self getViewControllerForTag:reactTag];
[viewController setPadding:UIEdgeInsetsMake(top.floatValue, left.floatValue, bottom.floatValue,
right.floatValue)];
});
}

@end
6 changes: 6 additions & 0 deletions src/auto/useNavigationAuto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import type {
Polygon,
CameraPosition,
UISettings,
Padding,
} from '../maps';
import { useMemo } from 'react';

Expand Down Expand Up @@ -188,6 +189,11 @@ export const useNavigationAuto = (): {
moveCamera: (cameraPosition: CameraPosition) => {
return NavAutoModule.moveCamera(cameraPosition);
},

setPadding: (padding: Padding) => {
const { top = 0, left = 0, bottom = 0, right = 0 } = padding;
return NavAutoModule.setPadding(top, left, bottom, right);
},
}),
[moduleListenersHandler]
);
Expand Down
6 changes: 6 additions & 0 deletions src/maps/mapView/mapViewController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import type {
MapType,
MapViewController,
MarkerOptions,
Padding,
PolygonOptions,
PolylineOptions,
} from './types';
Expand Down Expand Up @@ -164,5 +165,10 @@ export const getMapViewController = (viewId: number): MapViewController => {
moveCamera: (cameraPosition: CameraPosition) => {
sendCommand(viewId, commands.moveCamera, [cameraPosition]);
},

setPadding: (padding: Padding) => {
const { top = 0, left = 0, bottom = 0, right = 0 } = padding;
sendCommand(viewId, commands.setPadding, [top, left, bottom, right]);
},
};
};
22 changes: 22 additions & 0 deletions src/maps/mapView/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@ export enum MapType {
HYBRID,
}

/**
* Defines the padding options for a map.
*/
export interface Padding {
/** Top padding in pixels. */
top?: number;
/** Left padding in pixels. */
left?: number;
/** Bottom padding in pixels. */
bottom?: number;
/** Right padding in pixels. */
right?: number;
}

/**
* Defines the type of the map fragment.
*/
Expand Down Expand Up @@ -412,4 +426,12 @@ export interface MapViewController {
* @param cameraPosition - Defines the position the camera will take with the move.
*/
moveCamera(cameraPosition: CameraPosition): void;

/**
* Sets padding to the map.
*
* @param padding - An object defining padding for each side.
* Example: { top: 10, left: 5, bottom: 15, right: 10 }
*/
setPadding(padding: Padding): void;
}
Loading