Skip to content

Commit b210900

Browse files
committed
Add Streets Source v7 and v8 checks for way name layer
1 parent 5392ebb commit b210900

File tree

3 files changed

+110
-23
lines changed

3 files changed

+110
-23
lines changed

libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/map/MapLayerInteractor.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.mapbox.services.android.navigation.ui.v5.map;
22

3+
import android.graphics.Color;
4+
35
import com.mapbox.mapboxsdk.maps.MapboxMap;
46
import com.mapbox.mapboxsdk.style.layers.Layer;
57
import com.mapbox.mapboxsdk.style.layers.LineLayer;
@@ -9,20 +11,22 @@
911

1012
import static com.mapbox.mapboxsdk.style.layers.Property.NONE;
1113
import static com.mapbox.mapboxsdk.style.layers.Property.VISIBLE;
14+
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineColor;
15+
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineWidth;
1216
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.visibility;
17+
import static com.mapbox.services.android.navigation.ui.v5.map.NavigationMapboxMap.STREETS_LAYER_ID;
1318

1419
class MapLayerInteractor {
1520

16-
private MapboxMap mapboxMap;
21+
private static final float DEFAULT_WIDTH = 20f;
22+
private static final int LAST_INDEX = 0;
23+
24+
private final MapboxMap mapboxMap;
1725

1826
MapLayerInteractor(MapboxMap mapboxMap) {
1927
this.mapboxMap = mapboxMap;
2028
}
2129

22-
Layer retrieveLayerFromId(String layerId) {
23-
return mapboxMap.getStyle().getLayerAs(layerId);
24-
}
25-
2630
void updateLayerVisibility(boolean isVisible, String layerIdentifier) {
2731
// TODO add sourceIdentifier logic when https://github.com/mapbox/mapbox-gl-native/issues/12691 lands
2832
List<Layer> layers = mapboxMap.getStyle().getLayers();
@@ -35,6 +39,16 @@ boolean isLayerVisible(String layerIdentifier) {
3539
return findLayerVisibility(layerIdentifier, layers);
3640
}
3741

42+
void addStreetsLayer(String sourceId, String sourceLayer) {
43+
LineLayer streetsLayer = new LineLayer(STREETS_LAYER_ID, sourceId)
44+
.withProperties(
45+
lineWidth(DEFAULT_WIDTH),
46+
lineColor(Color.WHITE)
47+
)
48+
.withSourceLayer(sourceLayer);
49+
mapboxMap.getStyle().addLayerAt(streetsLayer, LAST_INDEX);
50+
}
51+
3852
private void updateLayerWithVisibility(String layerIdentifier, List<Layer> layers, boolean isVisible) {
3953
for (Layer layer : layers) {
4054
if (isValid(layer)) {

libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/map/NavigationMapboxMap.java

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
import android.annotation.SuppressLint;
44
import android.content.Context;
5-
import android.graphics.Color;
65
import android.graphics.PointF;
76
import android.location.Location;
87
import android.os.Bundle;
98
import android.os.PersistableBundle;
109
import android.support.annotation.NonNull;
10+
import android.support.annotation.Nullable;
1111
import android.support.v4.app.FragmentActivity;
1212

1313
import com.mapbox.api.directions.v5.models.DirectionsRoute;
@@ -23,7 +23,7 @@
2323
import com.mapbox.mapboxsdk.maps.MapView;
2424
import com.mapbox.mapboxsdk.maps.MapboxMap;
2525
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
26-
import com.mapbox.mapboxsdk.style.layers.LineLayer;
26+
import com.mapbox.mapboxsdk.style.sources.Source;
2727
import com.mapbox.mapboxsdk.style.sources.VectorSource;
2828
import com.mapbox.services.android.navigation.ui.v5.NavigationSnapshotReadyCallback;
2929
import com.mapbox.services.android.navigation.ui.v5.R;
@@ -36,8 +36,6 @@
3636
import java.util.ArrayList;
3737
import java.util.List;
3838

39-
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineColor;
40-
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineWidth;
4139
import static com.mapbox.services.android.navigation.v5.navigation.NavigationConstants.NAVIGATION_MINIMUM_MAP_ZOOM;
4240

4341
/**
@@ -51,11 +49,11 @@
5149
public class NavigationMapboxMap {
5250

5351
static final String STREETS_LAYER_ID = "streetsLayer";
54-
private static final String MAPBOX_STREETS_V7 = "mapbox://mapbox.mapbox-streets-v7";
55-
private static final String STREETS_SOURCE_ID = "streetsSource";
56-
private static final String ROAD_LABEL = "road_label";
57-
private static final float DEFAULT_WIDTH = 20f;
58-
private static final int LAST_INDEX = 0;
52+
private static final String MAPBOX_STREETS_V7_URL = "mapbox.mapbox-streets-v7";
53+
private static final String MAPBOX_STREETS_V8_URL = "mapbox.mapbox-streets-v8";
54+
private static final String STREETS_SOURCE_ID = "com.mapbox.services.android.navigation.streets";
55+
private static final String STREETS_V7_ROAD_LABEL = "road_label";
56+
private static final String STREETS_V8_ROAD_LABEL = "road";
5957
private static final String INCIDENTS_LAYER_ID = "closures";
6058
private static final String TRAFFIC_LAYER_ID = "traffic";
6159
private static final int[] ZERO_MAP_PADDING = {0, 0, 0, 0};
@@ -107,6 +105,12 @@ public NavigationMapboxMap(@NonNull MapView mapView, @NonNull MapboxMap mapboxMa
107105
this.mapWayName = mapWayName;
108106
}
109107

108+
// Package private (no modifier) for testing purposes
109+
NavigationMapboxMap(MapboxMap mapboxMap, MapLayerInteractor layerInteractor, MapPaddingAdjustor adjustor) {
110+
this.layerInteractor = layerInteractor;
111+
initializeWayname(mapboxMap, adjustor);
112+
}
113+
110114
/**
111115
* Adds a marker icon on the map at the given position.
112116
* <p>
@@ -522,15 +526,33 @@ private void initializeMapLayerInteractor(MapboxMap mapboxMap) {
522526
}
523527

524528
private void initializeStreetsSource(MapboxMap mapboxMap) {
525-
VectorSource streetSource = new VectorSource(STREETS_SOURCE_ID, MAPBOX_STREETS_V7);
526-
mapboxMap.getStyle().addSource(streetSource);
527-
LineLayer streetsLayer = new LineLayer(STREETS_LAYER_ID, STREETS_SOURCE_ID)
528-
.withProperties(
529-
lineWidth(DEFAULT_WIDTH),
530-
lineColor(Color.WHITE)
531-
)
532-
.withSourceLayer(ROAD_LABEL);
533-
mapboxMap.getStyle().addLayerAt(streetsLayer, LAST_INDEX);
529+
List<Source> sources = mapboxMap.getStyle().getSources();
530+
Source sourceV7 = findSourceByUrl(sources, MAPBOX_STREETS_V7_URL);
531+
Source sourceV8 = findSourceByUrl(sources, MAPBOX_STREETS_V8_URL);
532+
533+
if (sourceV7 != null) {
534+
layerInteractor.addStreetsLayer(sourceV7.getId(), STREETS_V7_ROAD_LABEL);
535+
} else if (sourceV8 != null) {
536+
layerInteractor.addStreetsLayer(sourceV8.getId(), STREETS_V8_ROAD_LABEL);
537+
} else {
538+
VectorSource streetSource = new VectorSource(STREETS_SOURCE_ID, MAPBOX_STREETS_V8_URL);
539+
mapboxMap.getStyle().addSource(streetSource);
540+
layerInteractor.addStreetsLayer(STREETS_SOURCE_ID, STREETS_V8_ROAD_LABEL);
541+
}
542+
}
543+
544+
@Nullable
545+
private Source findSourceByUrl(List<Source> sources, String streetsUrl) {
546+
for (Source source : sources) {
547+
if (source instanceof VectorSource) {
548+
VectorSource vectorSource = (VectorSource) source;
549+
String url = vectorSource.getUrl();
550+
if (url != null && url.contains(streetsUrl)) {
551+
return vectorSource;
552+
}
553+
}
554+
}
555+
return null;
534556
}
535557

536558
private void initializeRoute(MapView mapView, MapboxMap map) {

libandroid-navigation-ui/src/test/java/com/mapbox/services/android/navigation/ui/v5/map/NavigationMapboxMapTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
import com.mapbox.api.directions.v5.models.DirectionsRoute;
44
import com.mapbox.mapboxsdk.location.LocationComponent;
55
import com.mapbox.mapboxsdk.location.modes.RenderMode;
6+
import com.mapbox.mapboxsdk.maps.MapboxMap;
7+
import com.mapbox.mapboxsdk.maps.Style;
8+
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
9+
import com.mapbox.mapboxsdk.style.sources.Source;
10+
import com.mapbox.mapboxsdk.style.sources.VectorSource;
611
import com.mapbox.services.android.navigation.ui.v5.route.NavigationMapRoute;
712
import com.mapbox.services.android.navigation.ui.v5.route.OnRouteSelectionChangeListener;
813

@@ -14,6 +19,7 @@
1419
import static org.mockito.ArgumentMatchers.eq;
1520
import static org.mockito.Mockito.mock;
1621
import static org.mockito.Mockito.verify;
22+
import static org.mockito.Mockito.when;
1723

1824
public class NavigationMapboxMapTest {
1925

@@ -124,4 +130,49 @@ public void removeOnWayNameChangedListener_listenerIsRemovedFromMapWayname() {
124130

125131
verify(mapWayName).removeOnWayNameChangedListener(listener);
126132
}
133+
134+
@Test
135+
public void onInitializeWayName_existingV7StreetSourceIsUsed() {
136+
Style style = mock(Style.class);
137+
String urlV7 = "mapbox://mapbox.mapbox-streets-v7";
138+
List<Source> sources = buildMockSourcesWith(urlV7);
139+
when(style.getSources()).thenReturn(sources);
140+
MapboxMap mapboxMap = mock(MapboxMap.class);
141+
when(mapboxMap.getStyle()).thenReturn(style);
142+
MapLayerInteractor layerInteractor = mock(MapLayerInteractor.class);
143+
MapPaddingAdjustor adjustor = mock(MapPaddingAdjustor.class);
144+
145+
new NavigationMapboxMap(mapboxMap, layerInteractor, adjustor);
146+
147+
verify(layerInteractor).addStreetsLayer("composite", "road_label");
148+
}
149+
150+
@Test
151+
public void onInitializeWayName_exisitingV8StreetSourceIsUsed() {
152+
Style style = mock(Style.class);
153+
String urlV7 = "mapbox://mapbox.mapbox-streets-v8";
154+
List<Source> sources = buildMockSourcesWith(urlV7);
155+
when(style.getSources()).thenReturn(sources);
156+
MapboxMap mapboxMap = mock(MapboxMap.class);
157+
when(mapboxMap.getStyle()).thenReturn(style);
158+
MapLayerInteractor layerInteractor = mock(MapLayerInteractor.class);
159+
MapPaddingAdjustor adjustor = mock(MapPaddingAdjustor.class);
160+
161+
new NavigationMapboxMap(mapboxMap, layerInteractor, adjustor);
162+
163+
verify(layerInteractor).addStreetsLayer("composite", "road");
164+
}
165+
166+
private List<Source> buildMockSourcesWith(String url) {
167+
List<Source> sources = new ArrayList<>();
168+
VectorSource vectorSource1 = mock(VectorSource.class);
169+
VectorSource vectorSource2 = mock(VectorSource.class);
170+
when(vectorSource2.getId()).thenReturn("composite");
171+
when(vectorSource2.getUrl()).thenReturn(url);
172+
GeoJsonSource geoJsonSource = mock(GeoJsonSource.class);
173+
sources.add(vectorSource1);
174+
sources.add(vectorSource2);
175+
sources.add(geoJsonSource);
176+
return sources;
177+
}
127178
}

0 commit comments

Comments
 (0)