Skip to content
This repository was archived by the owner on Oct 7, 2024. It is now read-only.

Commit 0636b3c

Browse files
author
Langston Smith
authored
Refactoring satellite streets opacity zoom fade example (#1274)
1 parent be42fd7 commit 0636b3c

4 files changed

Lines changed: 84 additions & 27 deletions

File tree

MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/styles/SatelliteOpacityOnZoomActivity.java

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.mapbox.mapboxandroiddemo.examples.styles;
22

33
import android.os.Bundle;
4+
import android.view.View;
5+
import android.widget.Toast;
6+
47
import androidx.annotation.NonNull;
58
import androidx.appcompat.app.AppCompatActivity;
69

@@ -12,6 +15,9 @@
1215
import com.mapbox.mapboxsdk.maps.MapboxMap;
1316
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
1417
import com.mapbox.mapboxsdk.maps.Style;
18+
import com.mapbox.mapboxsdk.style.expressions.Expression;
19+
import com.mapbox.mapboxsdk.style.layers.Layer;
20+
import com.mapbox.mapboxsdk.style.layers.PropertyValue;
1521
import com.mapbox.mapboxsdk.style.layers.RasterLayer;
1622
import com.mapbox.mapboxsdk.style.sources.RasterSource;
1723

@@ -29,7 +35,21 @@
2935
public class SatelliteOpacityOnZoomActivity extends AppCompatActivity implements
3036
OnMapReadyCallback {
3137

38+
private static final String SATELLITE_RASTER_SOURCE_ID = "SATELLITE_RASTER_SOURCE_ID";
39+
private static final String SATELLITE_RASTER_LAYER_ID = "SATELLITE_RASTER_LAYER_ID";
3240
private MapView mapView;
41+
private MapboxMap mapboxMap;
42+
private boolean satelliteSetToReveal = false;
43+
44+
private PropertyValue<Expression> expressionRevealingSatellite = rasterOpacity(interpolate(linear(), zoom(),
45+
stop(15, 0),
46+
stop(18, 1)
47+
));
48+
49+
private PropertyValue<Expression> expressionRevealingMapboxStreets = rasterOpacity(interpolate(linear(), zoom(),
50+
stop(12, 1),
51+
stop(16, 0)
52+
));
3353

3454
@Override
3555
protected void onCreate(Bundle savedInstanceState) {
@@ -49,28 +69,51 @@ protected void onCreate(Bundle savedInstanceState) {
4969

5070
@Override
5171
public void onMapReady(@NonNull final MapboxMap mapboxMap) {
52-
5372
mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
5473
@Override
5574
public void onStyleLoaded(@NonNull Style style) {
75+
76+
SatelliteOpacityOnZoomActivity.this.mapboxMap = mapboxMap;
77+
5678
// Create a data source for the satellite raster image and add the source to the map
57-
style.addSource(new RasterSource("SATELLITE_RASTER_SOURCE_ID",
79+
style.addSource(new RasterSource(SATELLITE_RASTER_SOURCE_ID,
5880
"mapbox://mapbox.satellite", 512));
5981

6082
// Create a new map layer for the satellite raster images and add the satellite layer to the map.
6183
// Use runtime styling to adjust the satellite layer's opacity based on the map camera's zoom level
6284
style.addLayer(
63-
new RasterLayer("SATELLITE_RASTER_LAYER_ID", "SATELLITE_RASTER_SOURCE_ID").withProperties(
64-
rasterOpacity(interpolate(linear(), zoom(),
65-
stop(15, 0),
66-
stop(18, 1)
67-
))));
85+
new RasterLayer(SATELLITE_RASTER_LAYER_ID, SATELLITE_RASTER_SOURCE_ID).withProperties(
86+
expressionRevealingSatellite));
6887

6988
// Create a new camera position and animate the map camera to show the fade in/out UI of the satellite layer
7089
mapboxMap.animateCamera(
7190
CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder()
7291
.zoom(19)
7392
.build()), 9000);
93+
94+
findViewById(R.id.swap_streets_and_satellite_order_toggle_fab).setOnClickListener(new View.OnClickListener() {
95+
@Override
96+
public void onClick(View view) {
97+
swapStreetsAndSatelliteOrder();
98+
Toast.makeText(SatelliteOpacityOnZoomActivity.this,
99+
R.string.zoom_in_and_out_instruction, Toast.LENGTH_SHORT).show();
100+
}
101+
});
102+
}
103+
});
104+
}
105+
106+
private void swapStreetsAndSatelliteOrder() {
107+
mapboxMap.getStyle(new Style.OnStyleLoaded() {
108+
@Override
109+
public void onStyleLoaded(@NonNull Style style) {
110+
Layer satelliteLayer = style.getLayer(SATELLITE_RASTER_LAYER_ID);
111+
if (satelliteLayer != null) {
112+
satelliteLayer.setProperties(
113+
satelliteSetToReveal ? expressionRevealingSatellite : expressionRevealingMapboxStreets
114+
);
115+
}
116+
satelliteSetToReveal = !satelliteSetToReveal;
74117
}
75118
});
76119
}

MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/styles/ShowHideLayersActivity.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
public class ShowHideLayersActivity extends AppCompatActivity {
2929

3030
private MapView mapView;
31-
private MapboxMap map;
31+
private MapboxMap mapboxMap;
3232

3333
@Override
3434
protected void onCreate(Bundle savedInstanceState) {
@@ -45,7 +45,7 @@ protected void onCreate(Bundle savedInstanceState) {
4545
mapView.getMapAsync(new OnMapReadyCallback() {
4646
@Override
4747
public void onMapReady(@NonNull MapboxMap mapboxMap) {
48-
map = mapboxMap;
48+
ShowHideLayersActivity.this.mapboxMap = mapboxMap;
4949
mapboxMap.setStyle(Style.LIGHT, new Style.OnStyleLoaded() {
5050
@Override
5151
public void onStyleLoaded(@NonNull Style style) {
@@ -74,6 +74,23 @@ public void onClick(View view) {
7474
});
7575
}
7676

77+
78+
private void toggleLayer() {
79+
mapboxMap.getStyle(new Style.OnStyleLoaded() {
80+
@Override
81+
public void onStyleLoaded(@NonNull Style style) {
82+
Layer layer = style.getLayer("museums");
83+
if (layer != null) {
84+
if (VISIBLE.equals(layer.getVisibility().getValue())) {
85+
layer.setProperties(visibility(NONE));
86+
} else {
87+
layer.setProperties(visibility(VISIBLE));
88+
}
89+
}
90+
}
91+
});
92+
}
93+
7794
@Override
7895
public void onResume() {
7996
super.onResume();
@@ -115,20 +132,4 @@ protected void onSaveInstanceState(Bundle outState) {
115132
super.onSaveInstanceState(outState);
116133
mapView.onSaveInstanceState(outState);
117134
}
118-
119-
private void toggleLayer() {
120-
map.getStyle(new Style.OnStyleLoaded() {
121-
@Override
122-
public void onStyleLoaded(@NonNull Style style) {
123-
Layer layer = style.getLayer("museums");
124-
if (layer != null) {
125-
if (VISIBLE.equals(layer.getVisibility().getValue())) {
126-
layer.setProperties(visibility(NONE));
127-
} else {
128-
layer.setProperties(visibility(VISIBLE));
129-
}
130-
}
131-
}
132-
});
133-
}
134135
}

MapboxAndroidDemo/src/main/res/layout/activity_satellite_opacity_on_zoom.xml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,19 @@
22
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
44
xmlns:tools="http://schemas.android.com/tools"
5+
xmlns:app="http://schemas.android.com/apk/res-auto"
56
android:layout_width="match_parent"
67
android:layout_height="match_parent"
7-
tools:context=".examples.basics.SimpleMapViewActivity">
8+
tools:context=".examples.styles.SatelliteOpacityOnZoomActivity">
9+
10+
<com.google.android.material.floatingactionbutton.FloatingActionButton
11+
android:id="@+id/swap_streets_and_satellite_order_toggle_fab"
12+
android:layout_width="wrap_content"
13+
android:layout_height="wrap_content"
14+
android:layout_gravity="end|bottom"
15+
android:layout_margin="16dp"
16+
app:srcCompat="@drawable/ic_swap_horiz_white_24dp"
17+
tools:backgroundTint="@color/colorAccent" />
818

919
<com.mapbox.mapboxsdk.maps.MapView
1020
android:id="@+id/mapView"
@@ -16,4 +26,4 @@
1626
mapbox:mapbox_cameraZoomMin="6"
1727
/>
1828

19-
</FrameLayout>
29+
</FrameLayout>

MapboxAndroidDemo/src/main/res/values/activity_strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,9 @@
424424
<!-- Variable label placement-->
425425
<string name="zoom_map_in_and_out_circle_to_icon_transition">Zoom in and out to see the circles transition to icons</string>
426426

427+
<!-- Zoom opacity swap-->
428+
<string name="zoom_in_and_out_instruction">Zoom in and out to see the new order of the satellite and Streets styles</string>
429+
427430
<!-- China bounds checker-->
428431
<string name="device_location">Device %1$s in China</string>
429432
<string name="china_style_with_english_labels_warning_toast">This style file isn\'t actually included in the app because of privacy reasons. Add the file to an assets folder. Please email Mapbox at apac-bd@mapbox.com if you need this file and/or have questions.</string>

0 commit comments

Comments
 (0)