1
1
package com .mapbox .mapboxsdk .testapp .activity .maplayout ;
2
2
3
+ import android .content .Context ;
3
4
import android .os .Bundle ;
4
- import android .support .annotation .NonNull ;
5
5
import android .support .design .widget .FloatingActionButton ;
6
+ import android .support .v4 .widget .DrawerLayout ;
7
+ import android .support .v7 .app .ActionBar ;
8
+ import android .support .v7 .app .ActionBarDrawerToggle ;
6
9
import android .support .v7 .app .AppCompatActivity ;
10
+ import android .view .LayoutInflater ;
11
+ import android .view .MenuItem ;
7
12
import android .view .View ;
13
+ import android .view .ViewGroup ;
14
+ import android .widget .AdapterView ;
15
+ import android .widget .BaseAdapter ;
16
+ import android .widget .ListView ;
8
17
import android .widget .TextView ;
9
18
10
19
import com .mapbox .mapboxsdk .camera .CameraPosition ;
11
20
import com .mapbox .mapboxsdk .constants .Style ;
12
21
import com .mapbox .mapboxsdk .maps .MapView ;
13
22
import com .mapbox .mapboxsdk .maps .MapboxMap ;
14
23
import com .mapbox .mapboxsdk .maps .OnMapReadyCallback ;
24
+ import com .mapbox .mapboxsdk .style .layers .Layer ;
25
+ import com .mapbox .mapboxsdk .style .layers .Property ;
15
26
import com .mapbox .mapboxsdk .testapp .R ;
16
27
28
+ import java .util .List ;
29
+
17
30
import timber .log .Timber ;
18
31
32
+ import static com .mapbox .mapboxsdk .style .layers .PropertyFactory .visibility ;
33
+
19
34
/**
20
35
* Test Activity showcasing the different debug modes and allows to cycle between the default map styles.
21
36
*/
22
- public class DebugModeActivity extends AppCompatActivity {
37
+ public class DebugModeActivity extends AppCompatActivity implements OnMapReadyCallback {
23
38
24
39
private MapView mapView ;
25
40
private MapboxMap mapboxMap ;
26
-
41
+ private ActionBarDrawerToggle actionBarDrawerToggle ;
27
42
private int currentStyleIndex = 0 ;
28
43
29
44
private static final String [] STYLES = new String [] {
@@ -32,45 +47,104 @@ public class DebugModeActivity extends AppCompatActivity {
32
47
Style .LIGHT ,
33
48
Style .DARK ,
34
49
Style .SATELLITE ,
35
- Style .SATELLITE_STREETS
50
+ Style .SATELLITE_STREETS ,
51
+ Style .TRAFFIC_DAY ,
52
+ Style .TRAFFIC_NIGHT
36
53
};
37
54
38
55
@ Override
39
56
protected void onCreate (Bundle savedInstanceState ) {
40
57
super .onCreate (savedInstanceState );
41
58
setContentView (R .layout .activity_debug_mode );
59
+ setupToolbar ();
60
+ setupMapView (savedInstanceState );
61
+ setupDebugChangeView ();
62
+ setupStyleChangeView ();
63
+ }
64
+
65
+ private void setupToolbar () {
66
+ ActionBar actionBar = getSupportActionBar ();
67
+ if (actionBar != null ) {
68
+ getSupportActionBar ().setDisplayHomeAsUpEnabled (true );
69
+ getSupportActionBar ().setHomeButtonEnabled (true );
42
70
71
+ DrawerLayout drawerLayout = (DrawerLayout ) findViewById (R .id .drawer_layout );
72
+ actionBarDrawerToggle = new ActionBarDrawerToggle (this ,
73
+ drawerLayout ,
74
+ R .string .navigation_drawer_open ,
75
+ R .string .navigation_drawer_close
76
+ );
77
+ actionBarDrawerToggle .setDrawerIndicatorEnabled (true );
78
+ actionBarDrawerToggle .syncState ();
79
+ }
80
+ }
81
+
82
+ private void setupMapView (Bundle savedInstanceState ) {
43
83
mapView = (MapView ) findViewById (R .id .mapView );
44
84
mapView .addOnMapChangedListener (new MapView .OnMapChangedListener () {
45
85
@ Override
46
86
public void onMapChanged (int change ) {
47
87
if (change == MapView .DID_FINISH_LOADING_STYLE && mapboxMap != null ) {
48
- Timber .e ("New loaded style = %s" , mapboxMap .getStyleJson ());
88
+ Timber .v ("New style loaded with JSON: %s" , mapboxMap .getStyleJson ());
89
+ setupNavigationView (mapboxMap .getLayers ());
49
90
}
50
91
}
51
92
});
52
93
53
94
mapView .setTag (true );
54
95
mapView .setStyleUrl (STYLES [currentStyleIndex ]);
55
96
mapView .onCreate (savedInstanceState );
56
- mapView .getMapAsync (new OnMapReadyCallback () {
97
+ mapView .getMapAsync (this );
98
+ }
99
+
100
+ @ Override
101
+ public void onMapReady (MapboxMap map ) {
102
+ mapboxMap = map ;
103
+ mapboxMap .getUiSettings ().setZoomControlsEnabled (true );
104
+
105
+ setupNavigationView (mapboxMap .getLayers ());
106
+ setupZoomView ();
107
+ }
108
+
109
+ private void setupNavigationView (List <Layer > layerList ) {
110
+ final LayerListAdapter adapter = new LayerListAdapter (this , layerList );
111
+ ListView listView = (ListView ) findViewById (R .id .listView );
112
+ listView .setAdapter (adapter );
113
+ listView .setOnItemClickListener (new AdapterView .OnItemClickListener () {
57
114
@ Override
58
- public void onMapReady (@ NonNull MapboxMap map ) {
59
- mapboxMap = map ;
115
+ public void onItemClick (AdapterView <?> parent , View view , int position , long id ) {
116
+ Layer clickedLayer = adapter .getItem (position );
117
+ toggleLayerVisibility (clickedLayer );
118
+ closeNavigationView ();
119
+ }
120
+ });
121
+ }
60
122
61
- mapboxMap .getUiSettings ().setZoomControlsEnabled (true );
123
+ private void toggleLayerVisibility (Layer layer ) {
124
+ boolean isVisible = layer .getVisibility ().getValue ().equals (Property .VISIBLE );
125
+ layer .setProperties (
126
+ visibility (
127
+ isVisible ? Property .NONE : Property .VISIBLE
128
+ )
129
+ );
130
+ }
62
131
63
- // show current zoom level on screen
64
- final TextView textView = (TextView ) findViewById (R .id .textZoom );
65
- mapboxMap .setOnCameraChangeListener (new MapboxMap .OnCameraChangeListener () {
66
- @ Override
67
- public void onCameraChange (CameraPosition position ) {
68
- textView .setText (String .format (getString (R .string .debug_zoom ), position .zoom ));
69
- }
70
- });
132
+ private void closeNavigationView () {
133
+ DrawerLayout drawerLayout = (DrawerLayout ) findViewById (R .id .drawer_layout );
134
+ drawerLayout .closeDrawers ();
135
+ }
136
+
137
+ private void setupZoomView () {
138
+ final TextView textView = (TextView ) findViewById (R .id .textZoom );
139
+ mapboxMap .setOnCameraChangeListener (new MapboxMap .OnCameraChangeListener () {
140
+ @ Override
141
+ public void onCameraChange (CameraPosition position ) {
142
+ textView .setText (String .format (getString (R .string .debug_zoom ), position .zoom ));
71
143
}
72
144
});
145
+ }
73
146
147
+ private void setupDebugChangeView () {
74
148
FloatingActionButton fabDebug = (FloatingActionButton ) findViewById (R .id .fabDebug );
75
149
fabDebug .setOnClickListener (new View .OnClickListener () {
76
150
@ Override
@@ -81,7 +155,9 @@ public void onClick(View view) {
81
155
}
82
156
}
83
157
});
158
+ }
84
159
160
+ private void setupStyleChangeView () {
85
161
FloatingActionButton fabStyles = (FloatingActionButton ) findViewById (R .id .fabStyles );
86
162
fabStyles .setOnClickListener (new View .OnClickListener () {
87
163
@ Override
@@ -102,6 +178,11 @@ public void onStyleLoaded(String style) {
102
178
});
103
179
}
104
180
181
+ @ Override
182
+ public boolean onOptionsItemSelected (MenuItem item ) {
183
+ return actionBarDrawerToggle .onOptionsItemSelected (item ) || super .onOptionsItemSelected (item );
184
+ }
185
+
105
186
@ Override
106
187
protected void onStart () {
107
188
super .onStart ();
@@ -143,4 +224,58 @@ public void onLowMemory() {
143
224
super .onLowMemory ();
144
225
mapView .onLowMemory ();
145
226
}
227
+
228
+ private static class LayerListAdapter extends BaseAdapter {
229
+
230
+ private LayoutInflater layoutInflater ;
231
+ private List <Layer > layers ;
232
+
233
+ LayerListAdapter (Context context , List <Layer > layers ) {
234
+ this .layoutInflater = LayoutInflater .from (context );
235
+ this .layers = layers ;
236
+ }
237
+
238
+ @ Override
239
+ public int getCount () {
240
+ return layers .size ();
241
+ }
242
+
243
+ @ Override
244
+ public Layer getItem (int position ) {
245
+ return layers .get (position );
246
+ }
247
+
248
+ @ Override
249
+ public long getItemId (int position ) {
250
+ return position ;
251
+ }
252
+
253
+ @ Override
254
+ public View getView (int position , View convertView , ViewGroup parent ) {
255
+ Layer layer = layers .get (position );
256
+ View view = convertView ;
257
+ if (view == null ) {
258
+ view = layoutInflater .inflate (android .R .layout .simple_list_item_2 , parent , false );
259
+ ViewHolder holder = new ViewHolder (
260
+ (TextView ) view .findViewById (android .R .id .text1 ),
261
+ (TextView ) view .findViewById (android .R .id .text2 )
262
+ );
263
+ view .setTag (holder );
264
+ }
265
+ ViewHolder holder = (ViewHolder ) view .getTag ();
266
+ holder .text .setText (layer .getClass ().getSimpleName ());
267
+ holder .subText .setText (layer .getId ());
268
+ return view ;
269
+ }
270
+
271
+ private static class ViewHolder {
272
+ final TextView text ;
273
+ final TextView subText ;
274
+
275
+ ViewHolder (TextView text , TextView subText ) {
276
+ this .text = text ;
277
+ this .subText = subText ;
278
+ }
279
+ }
280
+ }
146
281
}
0 commit comments