Skip to content

Commit

Permalink
Refactor per review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
danesfeder committed Feb 15, 2018
1 parent 6f86972 commit 19327e0
Showing 1 changed file with 52 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -607,55 +607,46 @@ public void removeRoute() {

@Override
public void onMapClick(@NonNull LatLng point) {
if (invalidMapClick()) {
return;
}

final int currentRouteIndex = primaryRouteIndex;

if (routeLineStrings == null || routeLineStrings.isEmpty() || !alternativesVisible) {
if (findClickedRoute(point)) {
return;
}

checkNewRouteFound(currentRouteIndex);
}

private boolean invalidMapClick() {
return routeLineStrings == null || routeLineStrings.isEmpty() || !alternativesVisible;
}

private boolean findClickedRoute(@NonNull LatLng point) {
HashMap<Double, DirectionsRoute> routeDistancesAwayFromClick = new HashMap<>();

com.mapbox.geojson.Point clickPoint
= com.mapbox.geojson.Point.fromLngLat(point.getLongitude(), point.getLatitude());

// Cache current route index
final int currentRouteIndex = primaryRouteIndex;

if (calculateClickDistancesFromRoutes(routeDistancesAwayFromClick, clickPoint)) {
return;
return true;
}

List<Double> distancesAwayFromClick = new ArrayList<>(routeDistancesAwayFromClick.keySet());
Collections.sort(distancesAwayFromClick);

// Get the route with corresponding shortest distance
DirectionsRoute clickedRoute = routeDistancesAwayFromClick.get(distancesAwayFromClick.get(0));
primaryRouteIndex = directionsRoutes.indexOf(clickedRoute);

// If the current index has changed from the primary, update the route and listener
if (currentRouteIndex != primaryRouteIndex) {
// Update the route and waypoints
updateRoute();
// Update the listener with the new route
if (onRouteSelectionChangeListener != null) {
onRouteSelectionChangeListener.onNewPrimaryRouteSelected(
directionsRoutes.get(primaryRouteIndex));
}
}
return false;
}

private boolean calculateClickDistancesFromRoutes(HashMap<Double, DirectionsRoute> routeDistancesAwayFromClick,
com.mapbox.geojson.Point clickPoint) {
for (LineString lineString : routeLineStrings.keySet()) {

// Convert Positions to Points (will be removed with MAS 3.0)
List<com.mapbox.geojson.Point> linePoints = new ArrayList<>();
List<Position> positions = lineString.getCoordinates();
for (Position pos : positions) {
linePoints.add(com.mapbox.geojson.Point.fromLngLat(pos.getLongitude(), pos.getLatitude()));
}

com.mapbox.geojson.Feature feature = TurfMisc.pointOnLine(clickPoint, linePoints);
com.mapbox.geojson.Point pointOnLine = (com.mapbox.geojson.Point) feature.geometry();
com.mapbox.geojson.Point pointOnLine = findPointOnLine(clickPoint, lineString);

if (pointOnLine == null) {
return true;
Expand All @@ -669,6 +660,27 @@ private boolean calculateClickDistancesFromRoutes(HashMap<Double, DirectionsRout
return false;
}

private com.mapbox.geojson.Point findPointOnLine(com.mapbox.geojson.Point clickPoint, LineString lineString) {
List<com.mapbox.geojson.Point> linePoints = new ArrayList<>();
List<Position> positions = lineString.getCoordinates();
for (Position pos : positions) {
linePoints.add(com.mapbox.geojson.Point.fromLngLat(pos.getLongitude(), pos.getLatitude()));
}

com.mapbox.geojson.Feature feature = TurfMisc.pointOnLine(clickPoint, linePoints);
return (com.mapbox.geojson.Point) feature.geometry();
}

private void checkNewRouteFound(int currentRouteIndex) {
if (currentRouteIndex != primaryRouteIndex) {
updateRoute();
if (onRouteSelectionChangeListener != null) {
onRouteSelectionChangeListener.onNewPrimaryRouteSelected(
directionsRoutes.get(primaryRouteIndex));
}
}
}

private void updateRoute() {
// Update all route geometries to reflect their appropriate colors depending on if they are
// alternative or primary.
Expand Down Expand Up @@ -728,16 +740,26 @@ public void onProgressChange(Location location, RouteProgress routeProgress) {
private FeatureCollection addTrafficToSource(DirectionsRoute route, int index) {
final List<Feature> features = new ArrayList<>();
LineString originalGeometry = LineString.fromPolyline(route.geometry(), Constants.PRECISION_6);
buildRouteFeatureFromGeometry(index, features, originalGeometry);

// Store geometry and corresponding route for click logic
routeLineStrings.put(originalGeometry, route);

LineString lineString = LineString.fromPolyline(route.geometry(), Constants.PRECISION_6);
buildTrafficFeaturesFromRoute(route, index, features, lineString);
return FeatureCollection.fromFeatures(features);
}

private void buildRouteFeatureFromGeometry(int index, List<Feature> features, LineString originalGeometry) {
Feature feat = Feature.fromGeometry(originalGeometry);
feat.addStringProperty(SOURCE_KEY, String.format(Locale.US, ID_FORMAT, GENERIC_ROUTE_SOURCE_ID,
index));
feat.addNumberProperty(INDEX_KEY, index);
features.add(feat);
}

// Store geometry and corresponding route for click logic
routeLineStrings.put(originalGeometry, route);

LineString lineString = LineString.fromPolyline(route.geometry(), Constants.PRECISION_6);
private void buildTrafficFeaturesFromRoute(DirectionsRoute route, int index,
List<Feature> features, LineString lineString) {
for (RouteLeg leg : route.legs()) {
if (leg.annotation() != null && leg.annotation().congestion() != null) {
for (int i = 0; i < leg.annotation().congestion().size(); i++) {
Expand All @@ -761,6 +783,5 @@ private FeatureCollection addTrafficToSource(DirectionsRoute route, int index) {
features.add(feature);
}
}
return FeatureCollection.fromFeatures(features);
}
}

0 comments on commit 19327e0

Please sign in to comment.