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 display options to navigation #370

Merged
merged 2 commits into from
Dec 4, 2024
Merged
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
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,13 @@ try {
avoidTolls: false,
};

await navigationController.setDestinations([waypoint], routingOptions);
const displayOptions: DisplayOptions = {
showDestinationMarkers: true,
showStopSigns: true,
showTrafficLights: true,
};

await navigationController.setDestinations([waypoint], routingOptions, displayOptions);
await navigationController.startGuidance();
} catch (error) {
console.error('Error starting navigation', error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,31 +403,20 @@ private void createWaypoint(Map map) {
}

@ReactMethod
public void setDestination(ReadableMap waypoint, @Nullable ReadableMap routingOptions) {
pendingRoute = null; // reset pendingRoute.
mWaypoints.clear(); // reset waypoints
createWaypoint(waypoint.toHashMap());

if (routingOptions != null) {
pendingRoute =
mNavigator.setDestination(
mWaypoints.get(0),
ObjectTranslationUtil.getRoutingOptionsFromMap(routingOptions.toHashMap()));
} else {
pendingRoute = mNavigator.setDestination(mWaypoints.get(0));
}

setOnResultListener(
new IRouteStatusResult() {
@Override
public void onResult(Navigator.RouteStatus code) {
sendCommandToReactNative("onRouteStatusResult", code.toString());
}
});
public void setDestination(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setDestination could call setDestinations internally with array containing only the given waypoint

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

ReadableMap waypoint,
@Nullable ReadableMap routingOptions,
@Nullable ReadableMap displayOptions) {
WritableArray array = new WritableNativeArray();
array.pushMap(waypoint);
setDestinations(array, routingOptions, displayOptions);
}

@ReactMethod
public void setDestinations(ReadableArray waypoints, @Nullable ReadableMap routingOptions) {
public void setDestinations(
ReadableArray waypoints,
@Nullable ReadableMap routingOptions,
@Nullable ReadableMap displayOptions) {
if (mNavigator == null) {
// TODO: HANDLE THIS
return;
Expand All @@ -443,10 +432,18 @@ public void setDestinations(ReadableArray waypoints, @Nullable ReadableMap routi
}

if (routingOptions != null) {
pendingRoute =
mNavigator.setDestinations(
mWaypoints,
ObjectTranslationUtil.getRoutingOptionsFromMap(routingOptions.toHashMap()));
if (displayOptions != null) {
jokerttu marked this conversation as resolved.
Show resolved Hide resolved
pendingRoute =
mNavigator.setDestinations(
mWaypoints,
ObjectTranslationUtil.getRoutingOptionsFromMap(routingOptions.toHashMap()),
ObjectTranslationUtil.getDisplayOptionsFromMap(displayOptions.toHashMap()));
} else {
pendingRoute =
mNavigator.setDestinations(
mWaypoints,
ObjectTranslationUtil.getRoutingOptionsFromMap(routingOptions.toHashMap()));
}
} else {
pendingRoute = mNavigator.setDestinations(mWaypoints);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.android.gms.maps.model.Polyline;
import com.google.android.libraries.mapsplatform.turnbyturn.model.StepInfo;
import com.google.android.libraries.navigation.AlternateRoutesStrategy;
import com.google.android.libraries.navigation.DisplayOptions;
import com.google.android.libraries.navigation.NavigationRoadStretchRenderingData;
import com.google.android.libraries.navigation.RouteSegment;
import com.google.android.libraries.navigation.RoutingOptions;
Expand Down Expand Up @@ -105,6 +106,24 @@ public static WritableMap getMapFromStepInfo(StepInfo stepInfo) {
return map;
}

public static DisplayOptions getDisplayOptionsFromMap(Map map) {
DisplayOptions options = new DisplayOptions();

if (map.containsKey("showDestinationMarkers")) {
options.hideDestinationMarkers(!CollectionUtil.getBool("showDestinationMarkers", map, true));
}

if (map.containsKey("showStopSigns")) {
options.showStopSigns(CollectionUtil.getBool("showStopSigns", map, false));
}

if (map.containsKey("showTrafficLights")) {
options.showTrafficLights(CollectionUtil.getBool("showTrafficLights", map, false));
}

return options;
}

public static RoutingOptions getRoutingOptionsFromMap(Map map) {
RoutingOptions options = new RoutingOptions();

Expand Down
25 changes: 23 additions & 2 deletions example/src/controls/navigationControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
type Waypoint,
type CameraPosition,
type NavigationController,
type DisplayOptions,
} from '@googlemaps/react-native-navigation-sdk';
import SelectDropdown from 'react-native-select-dropdown';

Expand Down Expand Up @@ -103,7 +104,17 @@ const NavigationControls: React.FC<NavigationControlsProps> = ({
avoidTolls: false,
};

navigationController.setDestination(waypoint, routingOptions);
const displayOptions: DisplayOptions = {
showDestinationMarkers: true,
showStopSigns: true,
showTrafficLights: true,
};

navigationController.setDestination(
waypoint,
routingOptions,
displayOptions
);
};

const initWaypointToCameraLocation = async () => {
Expand Down Expand Up @@ -135,7 +146,17 @@ const NavigationControls: React.FC<NavigationControlsProps> = ({
avoidTolls: false,
};

navigationController.setDestinations(waypoints, routingOptions);
const displayOptions: DisplayOptions = {
showDestinationMarkers: true,
showStopSigns: true,
showTrafficLights: true,
};

navigationController.setDestinations(
waypoints,
routingOptions,
displayOptions
);
};

const setFollowingPerspective = (index: CameraPerspective) => {
Expand Down
32 changes: 29 additions & 3 deletions ios/react-native-navigation-sdk/NavModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -325,16 +325,22 @@ - (void)showTermsAndConditionsDialog {

RCT_EXPORT_METHOD(setDestination
: (nonnull NSDictionary *)waypoint routingOptions
: (NSDictionary *)routingOptions resolve
: (NSDictionary *)routingOptions displayOptions
: (NSDictionary *)displayOptions resolve
: (RCTPromiseResolveBlock)resolve rejecter
: (RCTPromiseRejectBlock)reject) {
NSArray *waypoints = @[ waypoint ];
[self setDestinations:waypoints routingOptions:routingOptions resolve:resolve rejecter:reject];
[self setDestinations:waypoints
routingOptions:routingOptions
displayOptions:displayOptions
resolve:resolve
rejecter:reject];
}

RCT_EXPORT_METHOD(setDestinations
: (nonnull NSArray *)waypoints routingOptions
: (NSDictionary *)routingOptions resolve
: (NSDictionary *)routingOptions displayOptions
: (NSDictionary *)displayOptions resolve
: (RCTPromiseResolveBlock)resolve rejecter
: (RCTPromiseRejectBlock)reject) {
__weak typeof(self) weakSelf = self;
Expand All @@ -350,6 +356,10 @@ - (void)showTermsAndConditionsDialog {
return;
}

if (displayOptions != NULL) {
[self setDisplayOptionsToViews:displayOptions];
}

strongSelf->_destinations = [[NSMutableArray alloc] init];

for (NSDictionary *wp in waypoints) {
Expand Down Expand Up @@ -400,6 +410,22 @@ - (void)showTermsAndConditionsDialog {
});
}

- (void)setDisplayOptionsToViews:(NSDictionary *)options {
for (NavViewController *viewController in [NavViewModule sharedInstance]
.viewControllers.allValues) {
if (options[@"showDestinationMarkers"] != nil) {
[viewController
setShowDestinationMarkersEnabled:[options[@"showDestinationMarkers"] boolValue]];
}
if (options[@"showStopSigns"] != nil) {
[viewController setShowStopSignsEnabled:[options[@"showStopSigns"] boolValue]];
}
if (options[@"showTrafficLights"] != nil) {
[viewController setShowTrafficLightsEnabled:[options[@"showTrafficLights"] boolValue]];
}
}
}

+ (GMSNavigationRoutingOptions *)getRoutingOptions:(NSDictionary *)options {
GMSNavigationMutableRoutingOptions *routingOptions = [[GMSNavigationMutableRoutingOptions alloc]
initWithRoutingStrategy:(GMSNavigationRoutingStrategy)[options[@"routingStrategy"] intValue]];
Expand Down
3 changes: 3 additions & 0 deletions ios/react-native-navigation-sdk/NavViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ typedef void (^OnArrayResult)(NSArray *_Nullable result);
- (void)setCompassEnabled:(BOOL)isEnabled;
- (void)setMyLocationButtonEnabled:(BOOL)isEnabled;
- (void)setMyLocationEnabled:(BOOL)isEnabled;
- (void)setShowDestinationMarkersEnabled:(BOOL)isEnabled;
- (void)setShowTrafficLightsEnabled:(BOOL)isEnabled;
- (void)setShowStopSignsEnabled:(BOOL)isEnabled;
- (void)setRotateGesturesEnabled:(BOOL)isEnabled;
- (void)setScrollGesturesEnabled:(BOOL)isEnabled;
- (void)setScrollGesturesEnabledDuringRotateOrZoom:(BOOL)isEnabled;
Expand Down
12 changes: 12 additions & 0 deletions ios/react-native-navigation-sdk/NavViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,18 @@ - (void)setMyLocationButtonEnabled:(BOOL)isEnabled {
[_mapView.settings setMyLocationButton:isEnabled];
}

- (void)setShowDestinationMarkersEnabled:(BOOL)isEnabled {
[_mapView.settings setShowsDestinationMarkers:isEnabled];
}

- (void)setShowTrafficLightsEnabled:(BOOL)isEnabled {
[_mapView.settings setShowsTrafficLights:isEnabled];
}

- (void)setShowStopSignsEnabled:(BOOL)isEnabled {
[_mapView.settings setShowsStopSigns:isEnabled];
}

- (void)setMyLocationEnabled:(BOOL)isEnabled {
_mapView.myLocationEnabled = isEnabled;
}
Expand Down
28 changes: 26 additions & 2 deletions src/navigation/navigation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,28 @@ export interface RoutingOptions {
*/
avoidHighways?: boolean;
}

/**
* Defines the options used by the Navigator for dislaying different elements.
*/
export interface DisplayOptions {
/**
* Configures whether destination markers are shown during navigation.
* If true, destination markers are shown.
*/
showDestinationMarkers?: boolean;
/**
* Configures whether stop signs are shown during navigation.
* If true, stop signs are shown.
*/
showStopSigns?: boolean;
/**
* Configures whether traffic lights are shown during navigation.
* If true, traffic lights are shown.
*/
showTrafficLights?: boolean;
}

/**
* Defines triggering thresholds for different severity levels of speed alerts, represented by
* SpeedAlertSeverity.
Expand Down Expand Up @@ -292,7 +314,8 @@ export interface NavigationController {
*/
setDestination(
waypoint: Waypoint,
routingOptions?: RoutingOptions
routingOptions?: RoutingOptions,
displayOptions?: DisplayOptions
): Promise<void>;

/**
Expand All @@ -303,7 +326,8 @@ export interface NavigationController {
*/
setDestinations(
waypoints: Waypoint[],
routingOptions?: RoutingOptions
routingOptions?: RoutingOptions,
displayOptions?: DisplayOptions
): Promise<void>;

/**
Expand Down
19 changes: 15 additions & 4 deletions src/navigation/navigation/useNavigationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
type SpeedAlertOptions,
type LocationSimulationOptions,
TaskRemovedBehavior,
type DisplayOptions,
} from './types';
import { getRouteStatusFromStringValue } from '../navigationView';
import { useMemo } from 'react';
Expand Down Expand Up @@ -93,16 +94,26 @@ export const useNavigationController = (

setDestination: async (
waypoint: Waypoint,
routingOptions?: RoutingOptions
routingOptions?: RoutingOptions,
displayOptions?: DisplayOptions
) => {
return await NavModule.setDestination(waypoint, routingOptions);
return await NavModule.setDestination(
waypoint,
routingOptions,
displayOptions
);
},

setDestinations: async (
waypoints: Waypoint[],
routingOptions?: RoutingOptions
routingOptions?: RoutingOptions,
displayOptions?: DisplayOptions
) => {
return await NavModule.setDestinations(waypoints, routingOptions);
return await NavModule.setDestinations(
waypoints,
routingOptions,
displayOptions
);
},

continueToNextDestination: async () => {
Expand Down
Loading