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

location bias feature implementation #33334

Merged
merged 8 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
9 changes: 7 additions & 2 deletions src/components/AddressSearch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ const propTypes = {
/** Information about the network */
network: networkPropTypes.isRequired,

/** location bias based on rectangular format */
rojiphil marked this conversation as resolved.
Show resolved Hide resolved
locationBias: PropTypes.string,

...withLocalizePropTypes,
};

Expand Down Expand Up @@ -138,6 +141,7 @@ const defaultProps = {
maxInputLength: undefined,
predefinedPlaces: [],
resultTypes: 'address',
locationBias: undefined,
};

function AddressSearch({
Expand All @@ -162,6 +166,7 @@ function AddressSearch({
shouldSaveDraft,
translate,
value,
locationBias,
}) {
const theme = useTheme();
const styles = useThemeStyles();
Expand All @@ -179,11 +184,11 @@ function AddressSearch({
language: preferredLocale,
types: resultTypes,
components: isLimitedToUSA ? 'country:us' : undefined,
...(locationBias && {locationbias: locationBias}),
}),
[preferredLocale, resultTypes, isLimitedToUSA],
[preferredLocale, resultTypes, isLimitedToUSA, locationBias],
);
const shouldShowCurrentLocationButton = canUseCurrentLocation && searchValue.trim().length === 0 && isFocused;

const saveLocationDetails = (autocompleteData, details) => {
const addressComponents = details.address_components;
if (!addressComponents) {
Expand Down
68 changes: 68 additions & 0 deletions src/pages/iou/request/step/IOURequestStepWaypoint.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {useNavigation} from '@react-navigation/native';
import lodashGet from 'lodash/get';
import lodashIsNil from 'lodash/isNil';
import PropTypes from 'prop-types';
import React, {useMemo, useRef, useState} from 'react';
import {View} from 'react-native';
Expand Down Expand Up @@ -38,6 +39,15 @@ const propTypes = {
/** The optimistic transaction for this request */
transaction: transactionPropTypes,

/* Current location coordinates of the user */
userLocation: PropTypes.shape({
/** Latitude of the location */
latitude: PropTypes.number,

/** Longitude of the location */
longitude: PropTypes.number,
}),

/** Recent waypoints that the user has selected */
recentWaypoints: PropTypes.arrayOf(
PropTypes.shape({
Expand Down Expand Up @@ -65,6 +75,7 @@ const propTypes = {
const defaultProps = {
recentWaypoints: [],
transaction: {},
userLocation: undefined,
};

function IOURequestStepWaypoint({
Expand All @@ -73,6 +84,7 @@ function IOURequestStepWaypoint({
params: {iouType, pageIndex, reportID, transactionID},
},
transaction,
userLocation,
}) {
const styles = useThemeStyles();
const {windowWidth} = useWindowDimensions();
Expand Down Expand Up @@ -100,6 +112,58 @@ function IOURequestStepWaypoint({
}
}, [parsedWaypointIndex, waypointCount]);

// Construct the rectangular boundary based on user location and waypoints
const locationBias = useMemo(() => {
// If there are no filled wayPoints and if user's current location cannot be retrieved,
// it is futile to arrive at a biased location. Let's return
if (filledWaypointCount === 0 && _.isEmpty(userLocation)) {
return null;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@parasharrajat
If there are no waypoints and also user's current location cannot be determined, do you think we should consider the default coordinates in the app i.e. [-122.4021, 37.7911] as set here?
or returning null as it is done currently is good enough?

Copy link
Member

Choose a reason for hiding this comment

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

I think it will be better to skip locationBias in that case.

// Gather the longitudes and latitudes from filled waypoints.
const longitudes = _.filter(
_.map(allWaypoints, (waypoint) => {
if (!waypoint || lodashIsNil(waypoint.lng)) {
return;
}
return waypoint.lng;
}),
(lng) => lng,
);
const latitudes = _.filter(
_.map(allWaypoints, (waypoint) => {
if (!waypoint || lodashIsNil(waypoint.lat)) {
return;
}
return waypoint.lat;
}),
(lat) => lat,
);

// When no filled waypoints are available but the current location of the user is available,
// let us consider the current user's location to construct a rectangular bound
if (filledWaypointCount === 0 && !_.isEmpty(userLocation)) {
longitudes.push(userLocation.longitude);
latitudes.push(userLocation.latitude);
}

// Extend the rectangular bound by 0.5 degree (roughly around 25-30 miles in US)
const minLat = Math.min(...latitudes) - 0.5;
const minLng = Math.min(...longitudes) - 0.5;
const maxLat = Math.max(...latitudes) + 0.5;
const maxLng = Math.max(...longitudes) + 0.5;

// Ensuring coordinates do not go out of range.
const south = minLat > -90 ? minLat : -90;
const west = minLng > -180 ? minLng : -180;
const north = maxLat < 90 ? maxLat : 90;
const east = maxLng < 180 ? maxLng : 180;

// Format: rectangle:south,west|north,east
const rectFormat = `rectangle:${south},${west}|${north},${east}`;
return rectFormat;
}, [userLocation, filledWaypointCount, allWaypoints]);

const waypointAddress = lodashGet(currentWaypoint, 'address', '');
// Hide the menu when there is only start and finish waypoint
const shouldShowThreeDotsButton = waypointCount > 2;
Expand Down Expand Up @@ -219,6 +283,7 @@ function IOURequestStepWaypoint({
<View>
<InputWrapperWithRef
InputComponent={AddressSearch}
locationBias={locationBias}
canUseCurrentLocation
inputID={`waypoint${pageIndex}`}
ref={(e) => (textInput.current = e)}
Expand Down Expand Up @@ -257,6 +322,9 @@ export default compose(
withWritableReportOrNotFound,
withFullTransactionOrNotFound,
withOnyx({
userLocation: {
key: ONYXKEYS.USER_LOCATION,
},
recentWaypoints: {
key: ONYXKEYS.NVP_RECENT_WAYPOINTS,

Expand Down
Loading