Search in sources :

Example 16 with Point

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();
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) DirectionsRoute(com.mapbox.api.directions.v5.models.DirectionsRoute) Gson(com.google.gson.Gson) Point(com.mapbox.geojson.Point) DirectionsResponse(com.mapbox.api.directions.v5.models.DirectionsResponse) Location(android.location.Location) Before(org.junit.Before)

Example 17 with Point

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);
}
Also used : RouteStepProgress(com.mapbox.services.android.navigation.v5.routeprogress.RouteStepProgress) LineString(com.mapbox.geojson.LineString) RouteLegProgress(com.mapbox.services.android.navigation.v5.routeprogress.RouteLegProgress) Point(com.mapbox.geojson.Point)

Example 18 with Point

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);
}
Also used : Point(com.mapbox.geojson.Point)

Example 19 with Point

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;
}
Also used : LineString(com.mapbox.geojson.LineString) Point(com.mapbox.geojson.Point) Feature(com.mapbox.geojson.Feature)

Example 20 with Point

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);
    }
}
Also used : Point(com.mapbox.geojson.Point) NavigationRoute(com.mapbox.services.android.navigation.v5.navigation.NavigationRoute)

Aggregations

Point (com.mapbox.geojson.Point)72 Test (org.junit.Test)31 BaseTest (com.mapbox.services.android.navigation.v5.BaseTest)29 Location (android.location.Location)18 RouteProgress (com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress)17 LineString (com.mapbox.geojson.LineString)15 ArrayList (java.util.ArrayList)10 LegStep (com.mapbox.api.directions.v5.models.LegStep)9 LatLng (com.mapbox.mapboxsdk.geometry.LatLng)7 Feature (com.mapbox.geojson.Feature)4 NavigationRoute (com.mapbox.services.android.navigation.v5.navigation.NavigationRoute)4 DirectionsResponse (com.mapbox.api.directions.v5.models.DirectionsResponse)3 DirectionsRoute (com.mapbox.api.directions.v5.models.DirectionsRoute)3 CameraPosition (com.mapbox.mapboxsdk.camera.CameraPosition)3 LatLngBounds (com.mapbox.mapboxsdk.geometry.LatLngBounds)3 Gson (com.google.gson.Gson)2 GsonBuilder (com.google.gson.GsonBuilder)2 RouteOptions (com.mapbox.api.directions.v5.models.RouteOptions)2 MarkerOptions (com.mapbox.mapboxsdk.annotations.MarkerOptions)2 BaseTest (com.mapbox.services.android.navigation.ui.v5.BaseTest)2