2121
2222/**
2323 * A few utilities to work with RouteLeg objects.
24- *
24+ * <p>
2525 * This is an experimental API. Experimental APIs are quickly evolving and
2626 * might change or be removed in minor versions.
2727 *
@@ -93,7 +93,7 @@ public boolean isInStep(Position position, RouteLeg routeLeg, int stepIndex) thr
9393 * @throws TurfException signals that a Turf exception of some sort has occurred.
9494 * @since 1.3.0
9595 */
96- public double getDistanceToStep (Position position , RouteLeg routeLeg , int stepIndex ) throws ServicesException ,
96+ public static double getDistanceToStep (Position position , RouteLeg routeLeg , int stepIndex ) throws ServicesException ,
9797 TurfException {
9898 Position closestPoint = getSnapToRoute (position , routeLeg , stepIndex );
9999 return TurfMeasurement .distance (
@@ -136,12 +136,31 @@ public static double getDistanceToNextStep(Position position, RouteLeg routeLeg,
136136 * @since 2.0.0
137137 */
138138 public static double getDistanceToNextStep (Position position , RouteLeg routeLeg , int stepIndex , String units )
139- throws ServicesException ,
140- TurfException {
139+ throws ServicesException , TurfException {
140+ return getDistanceToNextStep (position , routeLeg , stepIndex , units , Constants .PRECISION_6 );
141+ }
142+
143+ /**
144+ * Measures the distance from a position to the end of the route step. The position provided is snapped to the route
145+ * before distance is calculated.
146+ *
147+ * @param position you want to measure distance to from route. If using for navigation, this would typically
148+ * be the users current location.
149+ * @param routeLeg a directions route.
150+ * @param stepIndex integer index for step in route.
151+ * @param geometryPrecision either {@link Constants#PRECISION_5} or {@link Constants#PRECISION_6}
152+ * @return double value giving distance in kilometers.
153+ * @throws ServicesException if error occurs Mapbox API related.
154+ * @throws TurfException signals that a Turf exception of some sort has occurred.
155+ * @since 2.1.0
156+ */
157+ public static double getDistanceToNextStep (Position position , RouteLeg routeLeg , int stepIndex , String units ,
158+ int geometryPrecision )
159+ throws ServicesException , TurfException {
141160 LegStep step = validateStep (routeLeg , stepIndex );
142161
143162 // Decode the geometry
144- List <Position > coords = PolylineUtils .decode (step .getGeometry (), Constants . PRECISION_6 );
163+ List <Position > coords = PolylineUtils .decode (step .getGeometry (), geometryPrecision );
145164
146165 LineString slicedLine = TurfMisc .lineSlice (
147166 Point .fromCoordinates (position ),
@@ -160,8 +179,22 @@ public static double getDistanceToNextStep(Position position, RouteLeg routeLeg,
160179 * @since 2.0.0
161180 */
162181 public static double getDistanceToEndOfRoute (Position position , DirectionsRoute route ) throws TurfException {
182+ return getDistanceToEndOfRoute (position , route , Constants .PRECISION_6 );
183+ }
184+
185+ /**
186+ * @param position you want to measure distance to from route. If using for navigation, this would typically
187+ * be the users current location.
188+ * @param route a {@link DirectionsRoute}.
189+ * @param geometryPrecision either {@link Constants#PRECISION_5} or {@link Constants#PRECISION_6}
190+ * @return double value giving distance in kilometers.
191+ * @throws TurfException signals that a Turf exception of some sort has occurred.
192+ * @since 2.1.0
193+ */
194+ public static double getDistanceToEndOfRoute (Position position , DirectionsRoute route , int geometryPrecision )
195+ throws TurfException {
163196 // Decode the geometry
164- List <Position > coords = PolylineUtils .decode (route .getGeometry (), Constants . PRECISION_6 );
197+ List <Position > coords = PolylineUtils .decode (route .getGeometry (), geometryPrecision );
165198
166199 LineString slicedLine = TurfMisc .lineSlice (
167200 Point .fromCoordinates (position ),
@@ -184,11 +217,29 @@ public static double getDistanceToEndOfRoute(Position position, DirectionsRoute
184217 * @since 1.3.0
185218 */
186219 public static Position getSnapToRoute (Position position , RouteLeg routeLeg , int stepIndex )
220+ throws ServicesException , TurfException {
221+ return getSnapToRoute (position , routeLeg , stepIndex , Constants .PRECISION_6 );
222+ }
223+
224+ /**
225+ * Snaps given position to a {@link RouteLeg} step.
226+ *
227+ * @param position that you want to snap to routeLeg. If using for navigation, this would
228+ * typically be the users current location.
229+ * @param routeLeg that you want to snap position to.
230+ * @param stepIndex integer index for step in routeLeg.
231+ * @param geometryPrecision either {@link Constants#PRECISION_5} or {@link Constants#PRECISION_6}
232+ * @return your position snapped to the route.
233+ * @throws ServicesException if error occurs Mapbox API related.
234+ * @throws TurfException signals that a Turf exception of some sort has occurred.
235+ * @since 2.1.0
236+ */
237+ public static Position getSnapToRoute (Position position , RouteLeg routeLeg , int stepIndex , int geometryPrecision )
187238 throws ServicesException , TurfException {
188239 LegStep step = validateStep (routeLeg , stepIndex );
189240
190241 // Decode the geometry
191- List <Position > coords = PolylineUtils .decode (step .getGeometry (), Constants . PRECISION_6 );
242+ List <Position > coords = PolylineUtils .decode (step .getGeometry (), geometryPrecision );
192243
193244 // No need to do the math if the step has one coordinate only
194245 if (coords .size () == 1 ) {
@@ -234,7 +285,7 @@ public boolean isOffRoute(Position position, RouteLeg routeLeg) throws ServicesE
234285 * @throws TurfException signals that a Turf exception of some sort has occurred.
235286 * @since 1.3.0
236287 */
237- public int getClosestStep (Position position , RouteLeg routeLeg ) throws TurfException , ServicesException {
288+ public static int getClosestStep (Position position , RouteLeg routeLeg ) throws TurfException , ServicesException {
238289 double minDistance = Double .MAX_VALUE ;
239290 int closestIndex = 0 ;
240291
@@ -262,18 +313,35 @@ public int getClosestStep(Position position, RouteLeg routeLeg) throws TurfExcep
262313 * @since 2.0.0
263314 */
264315 public static LineString getGeometryRemainingOnRoute (Position position , DirectionsRoute route ) throws TurfException {
316+ return getGeometryRemainingOnRoute (position , route , Constants .PRECISION_6 );
317+ }
318+
319+ /**
320+ * Get the remaining route geometry from the position provided to the end of the directions route. This is useful
321+ * when the user location is traversing along the route and you don't want the past route geometry to show on the map.
322+ *
323+ * @param position the new starting postion of the route geometry. If using for navigation, this would
324+ * typically be the users current location.
325+ * @param route a Directions route.
326+ * @param geometryPrecision either {@link Constants#PRECISION_5} or {@link Constants#PRECISION_6}
327+ * @return the {@link LineString} representing the new route geometry.
328+ * @throws TurfException signals that a Turf exception of some sort has occurred.
329+ * @since 2.1.0
330+ */
331+ public static LineString getGeometryRemainingOnRoute (Position position , DirectionsRoute route , int geometryPrecision )
332+ throws TurfException {
265333 int lastLegIndex = route .getLegs ().size () - 1 ;
266334 int lastStepIndex = route .getLegs ().get (lastLegIndex ).getSteps ().size () - 1 ;
267335
268336 String polyline = route .getLegs ().get (lastLegIndex ).getSteps ().get (lastStepIndex ).getGeometry ();
269- LineString lastStepInLastLegLineString = LineString .fromPolyline (polyline , Constants . PRECISION_6 );
337+ LineString lastStepInLastLegLineString = LineString .fromPolyline (polyline , geometryPrecision );
270338 int lastStepLastIndex = lastStepInLastLegLineString .getCoordinates ().size () - 1 ;
271339 Position lastStepInLastLegPostion = lastStepInLastLegLineString .getCoordinates ().get (lastStepLastIndex );
272340
273341 return TurfMisc .lineSlice (
274342 Point .fromCoordinates (position ),
275343 Point .fromCoordinates (lastStepInLastLegPostion ),
276- LineString .fromPolyline (route .getGeometry (), Constants . PRECISION_6 )
344+ LineString .fromPolyline (route .getGeometry (), geometryPrecision )
277345 );
278346 }
279347
0 commit comments