|
2 | 2 |
|
3 | 3 | import android.annotation.SuppressLint; |
4 | 4 | import android.content.Context; |
5 | | -import android.graphics.Color; |
6 | 5 | import android.graphics.PointF; |
7 | 6 | import android.location.Location; |
8 | 7 | import android.os.Bundle; |
9 | 8 | import android.os.PersistableBundle; |
10 | 9 | import android.support.annotation.NonNull; |
| 10 | +import android.support.annotation.Nullable; |
11 | 11 | import android.support.v4.app.FragmentActivity; |
12 | 12 |
|
13 | 13 | import com.mapbox.api.directions.v5.models.DirectionsRoute; |
|
23 | 23 | import com.mapbox.mapboxsdk.maps.MapView; |
24 | 24 | import com.mapbox.mapboxsdk.maps.MapboxMap; |
25 | 25 | import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; |
26 | | -import com.mapbox.mapboxsdk.style.layers.LineLayer; |
| 26 | +import com.mapbox.mapboxsdk.style.sources.Source; |
27 | 27 | import com.mapbox.mapboxsdk.style.sources.VectorSource; |
28 | 28 | import com.mapbox.services.android.navigation.ui.v5.NavigationSnapshotReadyCallback; |
29 | 29 | import com.mapbox.services.android.navigation.ui.v5.R; |
|
36 | 36 | import java.util.ArrayList; |
37 | 37 | import java.util.List; |
38 | 38 |
|
39 | | -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineColor; |
40 | | -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineWidth; |
41 | 39 | import static com.mapbox.services.android.navigation.v5.navigation.NavigationConstants.NAVIGATION_MINIMUM_MAP_ZOOM; |
42 | 40 |
|
43 | 41 | /** |
|
51 | 49 | public class NavigationMapboxMap { |
52 | 50 |
|
53 | 51 | 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"; |
59 | 57 | private static final String INCIDENTS_LAYER_ID = "closures"; |
60 | 58 | private static final String TRAFFIC_LAYER_ID = "traffic"; |
61 | 59 | private static final int[] ZERO_MAP_PADDING = {0, 0, 0, 0}; |
@@ -107,6 +105,12 @@ public NavigationMapboxMap(@NonNull MapView mapView, @NonNull MapboxMap mapboxMa |
107 | 105 | this.mapWayName = mapWayName; |
108 | 106 | } |
109 | 107 |
|
| 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 | + |
110 | 114 | /** |
111 | 115 | * Adds a marker icon on the map at the given position. |
112 | 116 | * <p> |
@@ -522,15 +526,33 @@ private void initializeMapLayerInteractor(MapboxMap mapboxMap) { |
522 | 526 | } |
523 | 527 |
|
524 | 528 | 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; |
534 | 556 | } |
535 | 557 |
|
536 | 558 | private void initializeRoute(MapView mapView, MapboxMap map) { |
|
0 commit comments