Skip to content

Commit

Permalink
Refactor NavigationMapRoute
Browse files Browse the repository at this point in the history
  • Loading branch information
danesfeder committed Nov 8, 2018
1 parent d975b99 commit 90f28c5
Show file tree
Hide file tree
Showing 12 changed files with 1,118 additions and 896 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ public void onMapLongClick(@NonNull LatLng point) {
} else {
originMarker = null;
destinationMarker = null;
navigationMapRoute.removeRoute();
navigationMapRoute.updateRouteVisibilityTo(false);
navigationMapRoute.updateRouteArrowVisibilityTo(false);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ class ExampleActivity : AppCompatActivity(), ExampleView {
}

override fun showAlternativeRoutes(alternativesVisible: Boolean) {
map?.showAlternativeRoutes(false)
map?.showAlternativeRoutes(alternativesVisible)
}

override fun updateLocationRenderMode(renderMode: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,8 @@ public void showAlternativeRoutes(boolean alternativesVisible) {
* if no route is drawn.
*/
public void removeRoute() {
mapRoute.removeRoute();
mapRoute.updateRouteVisibilityTo(false);
mapRoute.updateRouteArrowVisibilityTo(false);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.mapbox.services.android.navigation.ui.v5.route;

import android.os.AsyncTask;

import com.mapbox.api.directions.v5.models.DirectionsRoute;
import com.mapbox.api.directions.v5.models.RouteLeg;
import com.mapbox.core.constants.Constants;
import com.mapbox.geojson.Feature;
import com.mapbox.geojson.FeatureCollection;
import com.mapbox.geojson.LineString;
import com.mapbox.geojson.Point;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

class FeatureProcessingTask extends AsyncTask<Void, Void, Void> {

private final List<DirectionsRoute> routes;
private final OnRouteFeaturesProcessedCallback callback;
private final List<FeatureCollection> routeFeatureCollections = new ArrayList<>();
private final HashMap<LineString, DirectionsRoute> routeLineStrings = new HashMap<>();

FeatureProcessingTask(List<DirectionsRoute> routes, OnRouteFeaturesProcessedCallback callback) {
this.routes = routes;
this.callback = callback;
}

@Override
protected Void doInBackground(Void... voids) {
for (DirectionsRoute route : routes) {
FeatureCollection routeFeatureCollection = createRouteFeatureCollection(route);
routeFeatureCollections.add(routeFeatureCollection);
}
return null;
}

@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
callback.onRouteFeaturesProcessed(routeFeatureCollections, routeLineStrings);
}

private FeatureCollection createRouteFeatureCollection(DirectionsRoute route) {
final List<Feature> features = new ArrayList<>();

LineString routeGeometry = LineString.fromPolyline(route.geometry(), Constants.PRECISION_6);
Feature routeFeature = Feature.fromGeometry(routeGeometry);
features.add(routeFeature);
routeLineStrings.put(routeGeometry, route);

List<Feature> congestionFeatures = buildCongestionFeaturesFromRoute(route, routeGeometry);
features.addAll(congestionFeatures);
return FeatureCollection.fromFeatures(features);
}

private List<Feature> buildCongestionFeaturesFromRoute(DirectionsRoute route, LineString lineString) {
final List<Feature> features = new ArrayList<>();
for (RouteLeg leg : route.legs()) {
if (leg.annotation() != null && leg.annotation().congestion() != null) {
for (int i = 0; i < leg.annotation().congestion().size(); i++) {
// See https://github.com/mapbox/mapbox-navigation-android/issues/353
if (leg.annotation().congestion().size() + 1 <= lineString.coordinates().size()) {

List<Point> points = new ArrayList<>();
points.add(lineString.coordinates().get(i));
points.add(lineString.coordinates().get(i + 1));

LineString congestionLineString = LineString.fromLngLats(points);
Feature feature = Feature.fromGeometry(congestionLineString);
String congestionValue = leg.annotation().congestion().get(i);
feature.addStringProperty(RouteConstants.CONGESTION_KEY, congestionValue);
features.add(feature);
}
}
} else {
Feature feature = Feature.fromGeometry(lineString);
features.add(feature);
}
}
return features;
}
}
Loading

0 comments on commit 90f28c5

Please sign in to comment.