Skip to content

Commit

Permalink
Fix NPE with MapRoute click listener (#721)
Browse files Browse the repository at this point in the history
* Fix NPE with MapRoute click listener

* Remove unnecessary comments

* Cleanup
  • Loading branch information
danesfeder authored Feb 26, 2018
1 parent f5d16f2 commit 0082458
Showing 1 changed file with 44 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ public class NavigationMapRoute implements ProgressChangeListener, MapView.OnMap
@DrawableRes
private int destinationWaypointIcon;

private List<DirectionsRoute> directionsRoutes;
private HashMap<LineString, DirectionsRoute> routeLineStrings;
private final MapboxNavigation navigation;
private final MapboxMap mapboxMap;
private List<String> layerIds;
private final HashMap<LineString, DirectionsRoute> routeLineStrings;
private final List<FeatureCollection> featureCollections;
private final List<DirectionsRoute> directionsRoutes;
private final List<String> layerIds;
private final MapView mapView;
private int primaryRouteIndex;
private final List<FeatureCollection> featureCollections;
private float routeScale;
private float alternativeRouteScale;
private String belowLayer;
Expand Down Expand Up @@ -206,19 +206,11 @@ public NavigationMapRoute(@Nullable MapboxNavigation navigation, @NonNull MapVie
this.navigation = navigation;
this.belowLayer = belowLayer;
featureCollections = new ArrayList<>();
alternativesVisible = true;
addListeners();
initialize();
}

/**
* Adds source and layers to the map.
*/
private void initialize() {
layerIds = new ArrayList<>();
directionsRoutes = new ArrayList<>();
routeLineStrings = new HashMap<>();
getAttributes();
placeRouteBelow();
layerIds = new ArrayList<>();
initialize();
addListeners();
}

/**
Expand All @@ -244,12 +236,10 @@ public void addRoute(DirectionsRoute directionsRoute) {
* @since 0.8.0
*/
public void addRoutes(@NonNull @Size(min = 1) List<DirectionsRoute> directionsRoutes) {
this.directionsRoutes = directionsRoutes;
primaryRouteIndex = 0;
clearRoutes();
if (directionsRoutes.size() == 1) {
alternativesVisible = false;
}
this.directionsRoutes.addAll(directionsRoutes);
primaryRouteIndex = 0;
alternativesVisible = directionsRoutes.size() > 1;
generateFeatureCollectionList(directionsRoutes);
drawRoutes();
addDirectionWaypoints();
Expand Down Expand Up @@ -337,12 +327,8 @@ private void drawRoutes() {
}

private void clearRoutes() {
if (!layerIds.isEmpty()) {
for (String id : layerIds) {
mapboxMap.removeLayer(id);
}
}
featureCollections.clear();
removeLayerIds();
clearRouteListData();
}

private void generateFeatureCollectionList(List<DirectionsRoute> directionsRoutes) {
Expand Down Expand Up @@ -451,6 +437,26 @@ private void addRouteLayer(String layerId, String sourceId, int index) {
MapUtils.addLayerToMap(mapboxMap, routeLayer, belowLayer);
}

private void removeLayerIds() {
if (!layerIds.isEmpty()) {
for (String id : layerIds) {
mapboxMap.removeLayer(id);
}
}
}

private void clearRouteListData() {
if (!directionsRoutes.isEmpty()) {
directionsRoutes.clear();
}
if (!routeLineStrings.isEmpty()) {
routeLineStrings.clear();
}
if (!featureCollections.isEmpty()) {
featureCollections.clear();
}
}

/**
* Add the route shield layer to the map either using the custom style values or the default.
*/
Expand Down Expand Up @@ -587,9 +593,12 @@ private static Feature getPointFromLineString(RouteLeg leg, int stepIndex) {
return feature;
}

/**
* Adds the necessary listeners
*/
private void initialize() {
alternativesVisible = true;
getAttributes();
placeRouteBelow();
}

private void addListeners() {
mapboxMap.addOnMapClickListener(this);
if (navigation != null) {
Expand Down Expand Up @@ -669,9 +678,10 @@ private com.mapbox.geojson.Point findPointOnLine(com.mapbox.geojson.Point clickP
private void checkNewRouteFound(int currentRouteIndex) {
if (currentRouteIndex != primaryRouteIndex) {
updateRoute();
if (onRouteSelectionChangeListener != null) {
onRouteSelectionChangeListener.onNewPrimaryRouteSelected(
directionsRoutes.get(primaryRouteIndex));
boolean isValidPrimaryIndex = primaryRouteIndex > 0 && primaryRouteIndex < directionsRoutes.size();
if (isValidPrimaryIndex && onRouteSelectionChangeListener != null) {
DirectionsRoute selectedRoute = directionsRoutes.get(primaryRouteIndex);
onRouteSelectionChangeListener.onNewPrimaryRouteSelected(selectedRoute);
}
}
}
Expand Down Expand Up @@ -720,8 +730,7 @@ public void onMapChanged(int change) {
public void onProgressChange(Location location, RouteProgress routeProgress) {
// Check if the route's the same as the route currently drawn
if (!routeProgress.directionsRoute().equals(directionsRoutes.get(primaryRouteIndex))) {
directionsRoutes.clear();
directionsRoutes.add(routeProgress.directionsRoute());
addRoute(routeProgress.directionsRoute());
drawRoutes();
addDirectionWaypoints();
}
Expand Down

0 comments on commit 0082458

Please sign in to comment.