summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/RuntimeStyleActivity.java
diff options
context:
space:
mode:
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.java150
1 files changed, 80 insertions, 70 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 983f82c53a..af42b7a0fe 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
@@ -4,9 +4,7 @@ import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.RawRes;
-import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
-import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
@@ -16,11 +14,12 @@ 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.FillLayer;
-import com.mapbox.mapboxsdk.style.layers.Function;
import com.mapbox.mapboxsdk.style.layers.Layer;
import com.mapbox.mapboxsdk.style.layers.LineLayer;
-import com.mapbox.mapboxsdk.style.layers.NoSuchLayerException;
import com.mapbox.mapboxsdk.style.layers.Property;
import com.mapbox.mapboxsdk.style.layers.PropertyValue;
import com.mapbox.mapboxsdk.style.layers.RasterLayer;
@@ -45,13 +44,13 @@ 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.layers.Filter.all;
import static com.mapbox.mapboxsdk.style.layers.Filter.eq;
import static com.mapbox.mapboxsdk.style.layers.Filter.gte;
import static com.mapbox.mapboxsdk.style.layers.Filter.lt;
-import static com.mapbox.mapboxsdk.style.layers.Function.Stop;
-import static com.mapbox.mapboxsdk.style.layers.Function.stop;
-import static com.mapbox.mapboxsdk.style.layers.Function.zoom;
import static com.mapbox.mapboxsdk.style.layers.Property.FILL_TRANSLATE_ANCHOR_MAP;
import static com.mapbox.mapboxsdk.style.layers.Property.NONE;
import static com.mapbox.mapboxsdk.style.layers.Property.SYMBOL_PLACEMENT_POINT;
@@ -71,7 +70,7 @@ import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.symbolPlacement;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.visibility;
/**
- * Sample Activity to show off the runtime style api
+ * Test activity showcasing the runtime style API.
*/
public class RuntimeStyleActivity extends AppCompatActivity {
@@ -83,9 +82,7 @@ public class RuntimeStyleActivity extends AppCompatActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_runtime_style);
- setupActionBar();
-
- //Initialize map as normal
+ // Initialize map as normal
mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
@@ -93,10 +90,10 @@ public class RuntimeStyleActivity extends AppCompatActivity {
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(MapboxMap map) {
- //Store for later
+ // Store for later
mapboxMap = map;
- //Center and Zoom (Amsterdam, zoomed to streets)
+ // Center and Zoom (Amsterdam, zoomed to streets)
mapboxMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(52.379189, 4.899431), 14));
}
});
@@ -153,8 +150,11 @@ public class RuntimeStyleActivity extends AppCompatActivity {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
- case android.R.id.home:
- onBackPressed();
+ case R.id.action_list_layers:
+ listLayers();
+ return true;
+ case R.id.action_list_sources:
+ listSources();
return true;
case R.id.action_water_color:
setWaterColor();
@@ -203,6 +203,26 @@ public class RuntimeStyleActivity extends AppCompatActivity {
}
}
+ private void listLayers() {
+ List<Layer> layers = mapboxMap.getLayers();
+ StringBuilder builder = new StringBuilder("Layers:");
+ for (Layer layer : layers) {
+ builder.append("\n");
+ builder.append(layer.getId());
+ }
+ Toast.makeText(this, builder.toString(), Toast.LENGTH_LONG).show();
+ }
+
+ private void listSources() {
+ List<Source> sources = mapboxMap.getSources();
+ StringBuilder builder = new StringBuilder("Sources:");
+ for (Source source : sources) {
+ builder.append("\n");
+ builder.append(source.getId());
+ }
+ Toast.makeText(this, builder.toString(), Toast.LENGTH_LONG).show();
+ }
+
private void setLayerInvisible() {
String[] roadLayers = new String[] {"water"};
for (String roadLayer : roadLayers) {
@@ -214,7 +234,7 @@ public class RuntimeStyleActivity extends AppCompatActivity {
}
private void setRoadSymbolPlacement() {
- //Zoom so that the labels are visible first
+ // Zoom so that the labels are visible first
mapboxMap.animateCamera(CameraUpdateFactory.zoomTo(14), new DefaultCallback() {
@Override
public void onFinish() {
@@ -251,16 +271,12 @@ public class RuntimeStyleActivity extends AppCompatActivity {
}
private void removeBuildings() {
- //Zoom to see buildings first
- try {
- mapboxMap.removeLayer("building");
- } catch (NoSuchLayerException noSuchLayerException) {
- Toast.makeText(RuntimeStyleActivity.this, noSuchLayerException.getMessage(), Toast.LENGTH_SHORT).show();
- }
+ // Zoom to see buildings first
+ mapboxMap.removeLayer("building");
}
private void addParksLayer() {
- //Add a source
+ // Add a source
Source source;
try {
source = new GeoJsonSource("amsterdam-spots", readRawResource(R.raw.amsterdam));
@@ -282,18 +298,18 @@ public class RuntimeStyleActivity extends AppCompatActivity {
fillAntialias(true)
);
- //Only show me parks (except westerpark with stroke-width == 3)
+ // Only show me parks (except westerpark with stroke-width == 3)
layer.setFilter(all(eq("type", "park"), eq("stroke-width", 2)));
- mapboxMap.addLayer(layer, "building");
- //layer.setPaintProperty(fillColor(Color.RED)); //XXX But not after the object is attached
+ mapboxMap.addLayerBelow(layer, "building");
+ // layer.setPaintProperty(fillColor(Color.RED)); // XXX But not after the object is attached
- //Or get the object later and set it. It's all good.
+ // Or get the object later and set it. It's all good.
mapboxMap.getLayer("parksLayer").setProperties(fillColor(Color.RED));
- //You can get a typed layer, if you're sure it's of that type. Use with care
+ // You can get a typed layer, if you're sure it's of that type. Use with care
layer = mapboxMap.getLayerAs("parksLayer");
- //And get some properties
+ // And get some properties
PropertyValue<Boolean> fillAntialias = layer.getFillAntialias();
Timber.d("Fill anti alias: " + fillAntialias.getValue());
layer.setProperties(fillTranslateAnchor(FILL_TRANSLATE_ANCHOR_MAP));
@@ -302,12 +318,12 @@ public class RuntimeStyleActivity extends AppCompatActivity {
PropertyValue<String> visibility = layer.getVisibility();
Timber.d("Visibility: " + visibility.getValue());
- //Get a good look at it all
+ // Get a good look at it all
mapboxMap.animateCamera(CameraUpdateFactory.zoomTo(12));
}
private void addDynamicParksLayer() {
- //Load some data
+ // Load some data
FeatureCollection parks;
try {
String json = readRawResource(R.raw.amsterdam);
@@ -321,7 +337,7 @@ public class RuntimeStyleActivity extends AppCompatActivity {
return;
}
- //Add an empty source
+ // Add an empty source
mapboxMap.addSource(new GeoJsonSource("dynamic-park-source"));
FillLayer layer = new FillLayer("dynamic-parks-layer", "dynamic-park-source");
@@ -332,15 +348,15 @@ public class RuntimeStyleActivity extends AppCompatActivity {
fillAntialias(true)
);
- //Only show me parks
+ // Only show me parks
layer.setFilter(all(eq("type", "park")));
mapboxMap.addLayer(layer);
- //Get a good look at it all
+ // Get a good look at it all
mapboxMap.animateCamera(CameraUpdateFactory.zoomTo(12));
- //Animate the parks source
+ // Animate the parks source
animateParksSource(parks, 0);
}
@@ -354,7 +370,7 @@ public class RuntimeStyleActivity extends AppCompatActivity {
}
Timber.d("Updating parks source");
- //change the source
+ // change the source
int park = counter < parks.getFeatures().size() - 1 ? counter : 0;
GeoJsonSource source = mapboxMap.getSourceAs("dynamic-park-source");
@@ -369,14 +385,14 @@ public class RuntimeStyleActivity extends AppCompatActivity {
features.add(parks.getFeatures().get(park));
source.setGeoJson(FeatureCollection.fromFeatures(features));
- //Re-post
+ // Re-post
animateParksSource(parks, park + 1);
}
}, counter == 0 ? 100 : 1000);
}
private void addTerrainLayer() {
- //Add a source
+ // Add a source
Source source = new VectorSource("my-terrain-source", "mapbox://mapbox.mapbox-terrain-v2");
mapboxMap.addSource(source);
@@ -391,10 +407,10 @@ public class RuntimeStyleActivity extends AppCompatActivity {
mapboxMap.addLayer(layer);
- //Need to get a fresh handle
+ // Need to get a fresh handle
layer = mapboxMap.getLayerAs("terrainLayer");
- //Make sure it's also applied after the fact
+ // Make sure it's also applied after the fact
layer.setMinZoom(10);
layer.setMaxZoom(15);
@@ -404,11 +420,11 @@ public class RuntimeStyleActivity extends AppCompatActivity {
}
private void addSatelliteLayer() {
- //Add a source
+ // Add a source
Source source = new RasterSource("my-raster-source", "mapbox://mapbox.satellite", 512);
mapboxMap.addSource(source);
- //Add a layer
+ // Add a layer
mapboxMap.addLayer(new RasterLayer("satellite-layer", "my-raster-source"));
}
@@ -418,24 +434,29 @@ public class RuntimeStyleActivity extends AppCompatActivity {
return;
}
- //Set a zoom function to update the color of the water
- layer.setProperties(fillColor(zoom(0.8f,
- stop(1, fillColor(Color.GREEN)),
- stop(4, fillColor(Color.BLUE)),
- stop(12, fillColor(Color.RED)),
- stop(20, fillColor(Color.BLACK))
- )));
-
- //do some animations to show it off properly
+ // 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)
+ )
+ ));
+
+ // do some animations to show it off properly
mapboxMap.animateCamera(CameraUpdateFactory.zoomTo(1), 1500);
PropertyValue<String> fillColor = layer.getFillColor();
- Function<String> function = fillColor.getFunction();
+ Function<Float, String> function = (Function<Float, String>) fillColor.getFunction();
if (function != null) {
- Timber.d("Fill color base: " + function.getBase());
- Timber.d("Fill color #stops: " + function.getStops().length);
+ 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 stop : function.getStops()) {
+ for (Stop<Float, String> stop : stops) {
Timber.d("Fill color #stops: " + stop);
}
}
@@ -459,23 +480,12 @@ public class RuntimeStyleActivity extends AppCompatActivity {
return writer.toString();
}
- private void setupActionBar() {
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
-
- ActionBar actionBar = getSupportActionBar();
- if (actionBar != null) {
- actionBar.setDisplayHomeAsUpEnabled(true);
- actionBar.setDisplayShowHomeEnabled(true);
- }
- }
-
private void addCustomTileSource() {
- //Add a source
+ // Add a source
Source source = new VectorSource("custom-tile-source", new TileSet("2.1.0", "https://vector.mapzen.com/osm/all/{z}/{x}/{y}.mvt?api_key=vector-tiles-LM25tq4"));
mapboxMap.addSource(source);
- //Add a layer
+ // Add a layer
mapboxMap.addLayer(
new FillLayer("custom-tile-layers", "custom-tile-source")
.withSourceLayer("water")
@@ -577,12 +587,12 @@ public class RuntimeStyleActivity extends AppCompatActivity {
@Override
public void onCancel() {
- //noop
+ // noop
}
@Override
public void onFinish() {
- //noop
+ // noop
}
}
}