-
Notifications
You must be signed in to change notification settings - Fork 318
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d975b99
commit 90f28c5
Showing
12 changed files
with
1,118 additions
and
896 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...c/main/java/com/mapbox/services/android/navigation/ui/v5/route/FeatureProcessingTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.