diff options
Diffstat (limited to 'platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/RuntimeStyleActivity.java')
-rw-r--r-- | platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/RuntimeStyleActivity.java | 235 |
1 files changed, 92 insertions, 143 deletions
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/RuntimeStyleActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/RuntimeStyleActivity.java index f6754af0f9..25d1040bf0 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/RuntimeStyleActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/RuntimeStyleActivity.java @@ -3,7 +3,6 @@ package com.mapbox.mapboxsdk.testapp.activity.style; import android.graphics.Color; import android.os.Bundle; import android.os.Handler; -import android.support.annotation.RawRes; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; @@ -13,10 +12,6 @@ import com.mapbox.mapboxsdk.camera.CameraUpdateFactory; import com.mapbox.mapboxsdk.geometry.LatLng; import com.mapbox.mapboxsdk.maps.MapView; import com.mapbox.mapboxsdk.maps.MapboxMap; -import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; -import com.mapbox.mapboxsdk.style.functions.Function; -import com.mapbox.mapboxsdk.style.functions.stops.ExponentialStops; -import com.mapbox.mapboxsdk.style.functions.stops.Stop; import com.mapbox.mapboxsdk.style.layers.CircleLayer; import com.mapbox.mapboxsdk.style.layers.FillLayer; import com.mapbox.mapboxsdk.style.layers.Layer; @@ -32,25 +27,22 @@ import com.mapbox.mapboxsdk.style.sources.Source; import com.mapbox.mapboxsdk.style.sources.TileSet; import com.mapbox.mapboxsdk.style.sources.VectorSource; import com.mapbox.mapboxsdk.testapp.R; +import com.mapbox.mapboxsdk.testapp.utils.ResourceUtils; import com.mapbox.services.commons.geojson.Feature; import com.mapbox.services.commons.geojson.FeatureCollection; -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; -import java.io.StringWriter; -import java.io.Writer; import java.util.ArrayList; import java.util.Collections; import java.util.List; import timber.log.Timber; -import static com.mapbox.mapboxsdk.style.functions.Function.zoom; -import static com.mapbox.mapboxsdk.style.functions.stops.Stop.stop; -import static com.mapbox.mapboxsdk.style.functions.stops.Stops.exponential; +import static com.mapbox.mapboxsdk.style.expressions.Expression.color; +import static com.mapbox.mapboxsdk.style.expressions.Expression.exponential; +import static com.mapbox.mapboxsdk.style.expressions.Expression.interpolate; +import static com.mapbox.mapboxsdk.style.expressions.Expression.stop; +import static com.mapbox.mapboxsdk.style.expressions.Expression.zoom; import static com.mapbox.mapboxsdk.style.layers.Filter.all; import static com.mapbox.mapboxsdk.style.layers.Filter.eq; import static com.mapbox.mapboxsdk.style.layers.Filter.gte; @@ -91,18 +83,15 @@ public class RuntimeStyleActivity extends AppCompatActivity { mapView.onCreate(savedInstanceState); - mapView.getMapAsync(new OnMapReadyCallback() { - @Override - public void onMapReady(MapboxMap map) { - // Store for later - mapboxMap = map; + mapView.getMapAsync(map -> { + // Store for later + mapboxMap = map; - // Center and Zoom (Amsterdam, zoomed to streets) - mapboxMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(52.379189, 4.899431), 14)); + // Center and Zoom (Amsterdam, zoomed to streets) + mapboxMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(52.379189, 4.899431), 14)); - mapboxMap.setTransitionDuration(250); - mapboxMap.setTransitionDelay(50); - } + mapboxMap.setTransitionDuration(250); + mapboxMap.setTransitionDelay(50); }); } @@ -285,7 +274,7 @@ public class RuntimeStyleActivity extends AppCompatActivity { // Add a source Source source; try { - source = new GeoJsonSource("amsterdam-spots", readRawResource(R.raw.amsterdam)); + source = new GeoJsonSource("amsterdam-spots", ResourceUtils.readRawResource(this, R.raw.amsterdam)); } catch (IOException ioException) { Toast.makeText( RuntimeStyleActivity.this, @@ -317,12 +306,12 @@ public class RuntimeStyleActivity extends AppCompatActivity { layer = mapboxMap.getLayerAs("parksLayer"); // And get some properties PropertyValue<Boolean> fillAntialias = layer.getFillAntialias(); - Timber.d("Fill anti alias: " + fillAntialias.getValue()); + Timber.d("Fill anti alias: %s", fillAntialias.getValue()); layer.setProperties(fillTranslateAnchor(FILL_TRANSLATE_ANCHOR_MAP)); PropertyValue<String> fillTranslateAnchor = layer.getFillTranslateAnchor(); - Timber.d("Fill translate anchor: " + fillTranslateAnchor.getValue()); + Timber.d("Fill translate anchor: %s", fillTranslateAnchor.getValue()); PropertyValue<String> visibility = layer.getVisibility(); - Timber.d("Visibility: " + visibility.getValue()); + Timber.d("Visibility: %s", visibility.getValue()); // Get a good look at it all mapboxMap.animateCamera(CameraUpdateFactory.zoomTo(12)); @@ -332,7 +321,7 @@ public class RuntimeStyleActivity extends AppCompatActivity { // Load some data FeatureCollection parks; try { - String json = readRawResource(R.raw.amsterdam); + String json = ResourceUtils.readRawResource(this, R.raw.amsterdam); parks = FeatureCollection.fromJson(json); } catch (IOException ioException) { Toast.makeText( @@ -368,32 +357,29 @@ public class RuntimeStyleActivity extends AppCompatActivity { private void animateParksSource(final FeatureCollection parks, final int counter) { Handler handler = new Handler(getMainLooper()); - handler.postDelayed(new Runnable() { - @Override - public void run() { - if (mapboxMap == null) { - return; - } + handler.postDelayed(() -> { + if (mapboxMap == null) { + return; + } - Timber.d("Updating parks source"); - // change the source - int park = counter < parks.getFeatures().size() - 1 ? counter : 0; + Timber.d("Updating parks source"); + // change the source + int park = counter < parks.getFeatures().size() - 1 ? counter : 0; - GeoJsonSource source = mapboxMap.getSourceAs("dynamic-park-source"); + GeoJsonSource source = mapboxMap.getSourceAs("dynamic-park-source"); - if (source == null) { - Timber.e("Source not found"); - Toast.makeText(RuntimeStyleActivity.this, "Source not found", Toast.LENGTH_SHORT).show(); - return; - } + if (source == null) { + Timber.e("Source not found"); + Toast.makeText(RuntimeStyleActivity.this, "Source not found", Toast.LENGTH_SHORT).show(); + return; + } - List<Feature> features = new ArrayList<>(); - features.add(parks.getFeatures().get(park)); - source.setGeoJson(FeatureCollection.fromFeatures(features)); + List<Feature> features = new ArrayList<>(); + features.add(parks.getFeatures().get(park)); + source.setGeoJson(FeatureCollection.fromFeatures(features)); - // Re-post - animateParksSource(parks, park + 1); - } + // Re-post + animateParksSource(parks, park + 1); }, counter == 0 ? 100 : 1000); } @@ -459,49 +445,21 @@ public class RuntimeStyleActivity extends AppCompatActivity { } // Set a zoom function to update the color of the water - layer.setProperties(fillColor( - zoom( - exponential( - stop(1, fillColor(Color.GREEN)), - stop(4, fillColor(Color.BLUE)), - stop(12, fillColor(Color.RED)), - stop(20, fillColor(Color.BLACK)) - ).withBase(0.8f) + layer.setProperties( + fillColor( + interpolate( + exponential(0.8f), + zoom(), + stop(1, color(Color.GREEN)), + stop(4, color(Color.BLUE)), + stop(12, color(Color.RED)), + stop(20, color(Color.BLACK)) + ) ) - )); + ); // do some animations to show it off properly mapboxMap.animateCamera(CameraUpdateFactory.zoomTo(1), 1500); - - PropertyValue<String> fillColor = layer.getFillColor(); - Function<Float, String> function = (Function<Float, String>) fillColor.getFunction(); - if (function != null) { - ExponentialStops<Float, String> stops = (ExponentialStops) function.getStops(); - Timber.d("Fill color base: " + stops.getBase()); - Timber.d("Fill color #stops: " + stops.size()); - if (function.getStops() != null) { - for (Stop<Float, String> stop : stops) { - Timber.d("Fill color #stops: " + stop); - } - } - } - } - - private String readRawResource(@RawRes int rawResource) throws IOException { - InputStream is = getResources().openRawResource(rawResource); - Writer writer = new StringWriter(); - char[] buffer = new char[1024]; - try { - Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); - int numRead; - while ((numRead = reader.read(buffer)) != -1) { - writer.write(buffer, 0, numRead); - } - } finally { - is.close(); - } - - return writer.toString(); } private void addCustomTileSource() { @@ -521,28 +479,25 @@ public class RuntimeStyleActivity extends AppCompatActivity { mapboxMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(31, -100), 3)); Handler handler = new Handler(getMainLooper()); - handler.postDelayed(new Runnable() { - @Override - public void run() { - if (mapboxMap == null) { - return; - } + handler.postDelayed(() -> { + if (mapboxMap == null) { + return; + } - Timber.d("Styling filtered fill layer"); + Timber.d("Styling filtered fill layer"); - FillLayer states = (FillLayer) mapboxMap.getLayer("states"); + FillLayer states = (FillLayer) mapboxMap.getLayer("states"); - if (states != null) { - states.setFilter(eq("name", "Texas")); - states.setFillOpacityTransition(new TransitionOptions(2500, 0)); - states.setFillColorTransition(new TransitionOptions(2500, 0)); - states.setProperties( - fillColor(Color.RED), - fillOpacity(0.25f) - ); - } else { - Toast.makeText(RuntimeStyleActivity.this, "No states layer in this style", Toast.LENGTH_SHORT).show(); - } + if (states != null) { + states.setFilter(eq("name", "Texas")); + states.setFillOpacityTransition(new TransitionOptions(2500, 0)); + states.setFillColorTransition(new TransitionOptions(2500, 0)); + states.setProperties( + fillColor(Color.RED), + fillOpacity(0.25f) + ); + } else { + Toast.makeText(RuntimeStyleActivity.this, "No states layer in this style", Toast.LENGTH_SHORT).show(); } }, 2000); } @@ -552,28 +507,25 @@ public class RuntimeStyleActivity extends AppCompatActivity { mapboxMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(40, -97), 5)); Handler handler = new Handler(getMainLooper()); - handler.postDelayed(new Runnable() { - @Override - public void run() { - if (mapboxMap == null) { - return; - } + handler.postDelayed(() -> { + if (mapboxMap == null) { + return; + } - Timber.d("Styling filtered line layer"); + Timber.d("Styling filtered line layer"); - LineLayer counties = (LineLayer) mapboxMap.getLayer("counties"); + LineLayer counties = (LineLayer) mapboxMap.getLayer("counties"); - if (counties != null) { - counties.setFilter(eq("NAME10", "Washington")); + if (counties != null) { + counties.setFilter(eq("NAME10", "Washington")); - counties.setProperties( - lineColor(Color.RED), - lineOpacity(0.75f), - lineWidth(5f) - ); - } else { - Toast.makeText(RuntimeStyleActivity.this, "No counties layer in this style", Toast.LENGTH_SHORT).show(); - } + counties.setProperties( + lineColor(Color.RED), + lineOpacity(0.75f), + lineWidth(5f) + ); + } else { + Toast.makeText(RuntimeStyleActivity.this, "No counties layer in this style", Toast.LENGTH_SHORT).show(); } }, 2000); } @@ -583,27 +535,24 @@ public class RuntimeStyleActivity extends AppCompatActivity { mapboxMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(40, -97), 5)); Handler handler = new Handler(getMainLooper()); - handler.postDelayed(new Runnable() { - @Override - public void run() { - if (mapboxMap == null) { - return; - } + handler.postDelayed(() -> { + if (mapboxMap == null) { + return; + } - Timber.d("Styling numeric fill layer"); + Timber.d("Styling numeric fill layer"); - FillLayer regions = (FillLayer) mapboxMap.getLayer("regions"); + FillLayer regions = (FillLayer) mapboxMap.getLayer("regions"); - if (regions != null) { - regions.setFilter(all(gte("HRRNUM", 200), lt("HRRNUM", 300))); + if (regions != null) { + regions.setFilter(all(gte("HRRNUM", 200), lt("HRRNUM", 300))); - regions.setProperties( - fillColor(Color.BLUE), - fillOpacity(0.5f) - ); - } else { - Toast.makeText(RuntimeStyleActivity.this, "No regions layer in this style", Toast.LENGTH_SHORT).show(); - } + regions.setProperties( + fillColor(Color.BLUE), + fillOpacity(0.5f) + ); + } else { + Toast.makeText(RuntimeStyleActivity.this, "No regions layer in this style", Toast.LENGTH_SHORT).show(); } }, 2000); } |