diff --git a/src/wayfinding/components/MapView.tsx b/src/wayfinding/components/MapView.tsx index dcb0bf10..26d11295 100644 --- a/src/wayfinding/components/MapView.tsx +++ b/src/wayfinding/components/MapView.tsx @@ -18,6 +18,7 @@ import useSitum, { useCallbackRef } from "../hooks"; import { setWebViewRef } from "../store"; import { useDispatch } from "../store/utils"; import { + type NavigateToPointType, type MapViewError, type MapViewRef, type NavigateToPoiType, @@ -169,6 +170,31 @@ const MapView = React.forwardRef( [pois] ); + const navigateToPointRef = useCallbackRef( + ({ + lat, + lng, + floorIdentifier, + navigationName, + type, + }: NavigateToPointType) => { + if (!webViewRef.current || (!lat && !lng && !floorIdentifier)) return; + + sendMessageToViewer( + webViewRef.current, + Mapper.navigateToPoint({ + // @ts-ignore + lat: lat, + lng: lng, + floorIdentifier: floorIdentifier, + navigationName: navigationName, + type: type, + } as NavigateToPointType) + ); + }, + [pois] + ); + const selectPoiRef = useCallbackRef( (poiId: number) => { if (!webViewRef.current) { @@ -212,6 +238,21 @@ const MapView = React.forwardRef( navigateToPoi({ poi, poiId }: { poi?: Poi; poiId?: number }): void { navigateToPoiRef.current({ poi, poiId }); }, + navigateToPoint({ + lat, + lng, + floorIdentifier, + navigationName, + type, + }: NavigateToPointType): void { + navigateToPointRef.current({ + lat, + lng, + floorIdentifier, + navigationName, + type, + }); + }, cancelNavigation(): void { if (!webViewRef.current) return; stopNavigation(); @@ -219,7 +260,7 @@ const MapView = React.forwardRef( }, }; }, - [stopNavigation, navigateToPoiRef, selectPoiRef] + [stopNavigation, navigateToPoiRef, selectPoiRef, navigateToPointRef] ); useEffect(() => { diff --git a/src/wayfinding/hooks/index.ts b/src/wayfinding/hooks/index.ts index aa776955..29fa29e5 100644 --- a/src/wayfinding/hooks/index.ts +++ b/src/wayfinding/hooks/index.ts @@ -46,6 +46,10 @@ import { UseSitumContext, } from "../store/index"; import { useDispatch, useSelector } from "../store/utils"; +import { + CURRENT_USER_LOCATION_ID, + CUSTOM_DESTINATION_LOCATION_ID, +} from "../types/constants"; const defaultNavigationOptions = { distanceToGoalThreshold: 4, @@ -309,15 +313,18 @@ export const useSitumInternal = () => { (p: Poi) => p.identifier === destinationId?.toString() ); - if (!poiDestination || (!poiOrigin && originId !== -1) || lockDirections) { + if ( + (!poiDestination && destinationId !== CUSTOM_DESTINATION_LOCATION_ID) || + (!poiOrigin && originId !== CURRENT_USER_LOCATION_ID) || + lockDirections + ) { console.debug( `Situm > hook > Could not compute route for origin: ${originId} or destination: ${destinationId} (lockDirections: ${lockDirections})` ); return; } - const from = - originId === -1 && location + originId === CURRENT_USER_LOCATION_ID && location ? location.position : { buildingIdentifier: poiOrigin.buildingIdentifier, @@ -326,12 +333,20 @@ export const useSitumInternal = () => { coordinate: poiOrigin.coordinate, }; - const to = { - buildingIdentifier: poiDestination.buildingIdentifier, - floorIdentifier: poiDestination.floorIdentifier, - cartesianCoordinate: poiDestination.cartesianCoordinate, - coordinate: poiDestination.coordinate, - }; + const to = + destinationId === CUSTOM_DESTINATION_LOCATION_ID + ? { + buildingIdentifier: directionsOptions.to.buildingIdentifier, + floorIdentifier: directionsOptions.to.floorIdentifier, + cartesianCoordinate: directionsOptions.to.cartesianCoordinate, + coordinate: directionsOptions.to.coordinate, + } + : { + buildingIdentifier: poiDestination.buildingIdentifier, + floorIdentifier: poiDestination.floorIdentifier, + cartesianCoordinate: poiDestination.cartesianCoordinate, + coordinate: poiDestination.coordinate, + }; // iOS workaround -> does not allow for several direction petitions setLockDirections(true); @@ -344,7 +359,7 @@ export const useSitumInternal = () => { type: directionsOptions?.accessibilityMode, }; updateRoute && dispatch(setDirections(extendedRoute)); - return directions; + return extendedRoute; // directions }) .catch((e: string) => { dispatch(setDirections({ error: JSON.stringify(e) })); @@ -375,7 +390,7 @@ export const useSitumInternal = () => { directionsOptions, updateRoute: false, }).then((r) => { - if (originId !== -1 || !location || !r) { + if (originId !== CURRENT_USER_LOCATION_ID || !location || !r) { callback && callback("error"); return; } diff --git a/src/wayfinding/types/constants.ts b/src/wayfinding/types/constants.ts index ea095e43..abc935fd 100644 --- a/src/wayfinding/types/constants.ts +++ b/src/wayfinding/types/constants.ts @@ -3,3 +3,5 @@ export enum ErrorName { ERR_INTERNET_DISCONNECTED = "ERR_INTERNET_DISCONNECTED", ERR_INTERNAL_SERVER_ERROR = "ERR_INTERNAL_SERVER_ERROR", } +export const CURRENT_USER_LOCATION_ID = -1; +export const CUSTOM_DESTINATION_LOCATION_ID = -2; diff --git a/src/wayfinding/types/index.ts b/src/wayfinding/types/index.ts index 9b7723c5..6145151e 100644 --- a/src/wayfinding/types/index.ts +++ b/src/wayfinding/types/index.ts @@ -9,6 +9,13 @@ export interface MapViewError { export interface MapViewRef { selectPoi: (poiId: number) => void; navigateToPoi: ({ poi, poiId }: { poi?: Poi; poiId?: number }) => void; + navigateToPoint: ({ + lat, + lng, + floorIdentifier, + navigationName, + type, + }: NavigateToPointType) => void; cancelNavigation: () => void; } @@ -61,3 +68,16 @@ export type NavigateToPoiType = { navigationTo: number; type?: string; }; + +export type NavigationAccessibilityTypes = + | "CHOOSE_SHORTEST" + | "ONLY_NOT_ACCESSIBLE_FLOOR_CHANGES" + | "ONLY_ACCESSIBLE"; + +export type NavigateToPointType = { + lat: number; + lng: number; + floorIdentifier: string; + navigationName?: string; + type?: NavigationAccessibilityTypes; +}; diff --git a/src/wayfinding/utils/mapper.ts b/src/wayfinding/utils/mapper.ts index a00949a5..461c934e 100644 --- a/src/wayfinding/utils/mapper.ts +++ b/src/wayfinding/utils/mapper.ts @@ -5,7 +5,12 @@ import type { Location, SDKNavigation, } from "../../sdk/types"; -import type { Destination, NavigateToPoiType, Navigation } from "../types"; +import type { + Destination, + NavigateToPointType, + NavigateToPoiType, + Navigation, +} from "../types"; const mapperWrapper = (type: string, payload: unknown) => JSON.stringify({ type, payload }); @@ -40,6 +45,9 @@ const Mapper = { navigateToPoi: (navigate: NavigateToPoiType) => mapperWrapper(`navigation.start`, { navigationTo: navigate?.navigationTo }), + navigateToPoint: (navigate: NavigateToPointType) => + mapperWrapper(`navigation.start`, navigate), + cancelNavigation: () => mapperWrapper(`navigation.cancel`, {}), selectPoi: (poiId: number | null) =>