use of com.mapbox.geojson.Point in project mapbox-navigation-android by mapbox.
the class TriggerTest method setup.
@Before
public void setup() throws IOException {
Gson gson = new GsonBuilder().registerTypeAdapterFactory(DirectionsAdapterFactory.create()).create();
String body = loadJsonFixture(PRECISION_6);
DirectionsResponse response = gson.fromJson(body, DirectionsResponse.class);
DirectionsRoute route = response.routes().get(0);
Location location = new Location("test");
List<Point> coords = PolylineUtils.decode(route.legs().get(0).steps().get(1).geometry(), Constants.PRECISION_6);
location.setLatitude(coords.get(0).latitude());
location.setLongitude(coords.get(0).longitude());
routeProgress = RouteProgress.builder().directionsRoute(route).distanceRemaining(route.distance()).legDistanceRemaining(route.legs().get(0).distance()).stepDistanceRemaining(route.legs().get(0).steps().get(0).distance()).legIndex(0).stepIndex(1).build();
}
use of com.mapbox.geojson.Point in project mapbox-navigation-android by mapbox.
the class SnapToRoute method snapLocationBearing.
/**
* Creates a snapped bearing for the snapped {@link Location}.
* <p>
* This is done by measuring 1 meter ahead of the current step distance traveled and
* creating a {@link Point} with this distance using {@link TurfMeasurement#along(LineString, double, String)}.
* <p>
* If the step distance remaining is zero, the distance ahead is 1 meter into the upcoming step.
* This way, an accurate bearing is upheld transitioning between steps.
*
* @param routeProgress for all current progress values
* @return float bearing snapped to route
*/
private static float snapLocationBearing(RouteProgress routeProgress) {
RouteLegProgress legProgress = routeProgress.currentLegProgress();
RouteStepProgress stepProgress = legProgress.currentStepProgress();
double distanceTraveled = stepProgress.distanceTraveled();
double distanceRemaining = stepProgress.distanceRemaining();
boolean distanceRemainingZero = distanceRemaining == 0;
// Either want to measure our current step distance traveled + 1 or 1 meter into the upcoming step
double distanceAhead = distanceRemainingZero ? 1 : distanceTraveled + 1;
// Create the step linestring from the geometry
LineString upcomingLineString = createUpcomingLineString(legProgress, distanceRemainingZero);
LineString currentLineString = createCurrentLineString(legProgress);
// Measure 1 meter ahead of the users current location, only if the distance remaining isn't zero
Point futurePoint = createFuturePoint(distanceAhead, upcomingLineString, currentLineString);
Point currentPoint = TurfMeasurement.along(currentLineString, distanceTraveled, TurfConstants.UNIT_METERS);
// Get bearing and convert azimuth to degrees
double azimuth = TurfMeasurement.bearing(currentPoint, futurePoint);
return (float) MathUtils.wrap(azimuth, 0, 360);
}
use of com.mapbox.geojson.Point in project mapbox-navigation-android by mapbox.
the class DistanceUtils method calculateAbsoluteDistance.
public static int calculateAbsoluteDistance(Location currentLocation, MetricsRouteProgress metricProgress) {
Point currentPoint = Point.fromLngLat(currentLocation.getLongitude(), currentLocation.getLatitude());
Point finalPoint = metricProgress.getDirectionsRouteDestination();
return (int) TurfMeasurement.distance(currentPoint, finalPoint, TurfConstants.UNIT_METERS);
}
use of com.mapbox.geojson.Point in project mapbox-navigation-android by mapbox.
the class MeasurementUtils method userTrueDistanceFromStep.
/**
* Calculates the distance between the users current raw {@link android.location.Location} object
* to the closest {@link Point} in the {@link LegStep}.
*
* @param usersRawLocation {@link Point} the raw location where the user is currently located
* @param step {@link LegStep} to calculate the closest point on the step to our
* predicted location
* @return double in distance meters
* @since 0.2.0
*/
public static double userTrueDistanceFromStep(Point usersRawLocation, LegStep step) {
// Check that the leg step contains geometry.
if (TextUtils.isEmpty(step.geometry())) {
return 0;
}
// Get the lineString from the step geometry.
LineString lineString = LineString.fromPolyline(step.geometry(), Constants.PRECISION_6);
// the distance is obviously zero, so return 0 to avoid executing additional unnecessary code.
if (lineString.coordinates().isEmpty() || usersRawLocation.equals(lineString.coordinates().get(0))) {
return 0;
}
if (lineString.coordinates().size() == 1) {
return TurfMeasurement.distance(usersRawLocation, lineString.coordinates().get(0), UNIT_METERS);
}
Feature feature = TurfMisc.nearestPointOnLine(usersRawLocation, lineString.coordinates());
Point snappedPoint = (Point) feature.geometry();
if (snappedPoint == null) {
return 0;
}
if (Double.isInfinite(snappedPoint.latitude()) || Double.isInfinite(snappedPoint.longitude())) {
return TurfMeasurement.distance(usersRawLocation, lineString.coordinates().get(0), UNIT_METERS);
}
double distance = TurfMeasurement.distance(usersRawLocation, snappedPoint, UNIT_METERS);
return Double.isNaN(distance) ? 0d : distance;
}
use of com.mapbox.geojson.Point in project mapbox-navigation-android by mapbox.
the class RouteEngine method fetchRoute.
/**
* Calculates a new {@link com.mapbox.api.directions.v5.models.DirectionsRoute} given
* the current {@link Location} and {@link RouteProgress} along the route.
* <p>
* Uses {@link RouteOptions#coordinates()} and {@link RouteProgress#remainingWaypoints()}
* to determine the amount of remaining waypoints there are along the given route.
*
* @param location current location of the device
* @param routeProgress for remaining waypoints along the route
* @since 0.10.0
*/
public void fetchRoute(Location location, RouteProgress routeProgress) {
if (location == null || routeProgress == null) {
return;
}
this.routeProgress = routeProgress;
Point origin = Point.fromLngLat(location.getLongitude(), location.getLatitude());
Double bearing = location.hasBearing() ? Float.valueOf(location.getBearing()).doubleValue() : null;
NavigationRoute.Builder builder = buildRouteRequestFromCurrentLocation(origin, bearing, routeProgress);
if (builder != null) {
builder.language(locale).voiceUnits(unitType);
builder.build().getRoute(this);
}
}
Aggregations