diff options
Diffstat (limited to 'platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style')
21 files changed, 0 insertions, 4635 deletions
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/AnimatedImageSourceActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/AnimatedImageSourceActivity.java deleted file mode 100644 index 6c3993d4c6..0000000000 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/AnimatedImageSourceActivity.java +++ /dev/null @@ -1,143 +0,0 @@ -package com.mapbox.mapboxsdk.testapp.activity.style; - -import android.content.Context; -import android.graphics.Bitmap; -import android.graphics.drawable.BitmapDrawable; -import android.graphics.drawable.Drawable; -import android.os.Bundle; -import android.os.Handler; -import android.support.annotation.NonNull; -import android.support.v7.app.AppCompatActivity; - -import com.mapbox.mapboxsdk.Mapbox; -import com.mapbox.mapboxsdk.geometry.LatLng; -import com.mapbox.mapboxsdk.geometry.LatLngQuad; -import com.mapbox.mapboxsdk.maps.MapView; -import com.mapbox.mapboxsdk.maps.MapboxMap; -import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; -import com.mapbox.mapboxsdk.maps.Style; -import com.mapbox.mapboxsdk.style.layers.RasterLayer; -import com.mapbox.mapboxsdk.style.sources.ImageSource; -import com.mapbox.mapboxsdk.testapp.R; -import com.mapbox.mapboxsdk.utils.BitmapUtils; - -/** - * Test activity showing how to use a series of images to create an animation - * with an ImageSource - * <p> - * GL-native equivalent of https://www.mapbox.com/mapbox-gl-js/example/animate-images/ - * </p> - */ -public class AnimatedImageSourceActivity extends AppCompatActivity implements OnMapReadyCallback { - - private static final String ID_IMAGE_SOURCE = "animated_image_source"; - private static final String ID_IMAGE_LAYER = "animated_image_layer"; - - private MapView mapView; - private final Handler handler = new Handler(); - private Runnable runnable; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_animated_image_source); - mapView = findViewById(R.id.mapView); - mapView.onCreate(savedInstanceState); - mapView.getMapAsync(this); - } - - @Override - public void onMapReady(@NonNull final MapboxMap map) { - LatLngQuad quad = new LatLngQuad( - new LatLng(46.437, -80.425), - new LatLng(46.437, -71.516), - new LatLng(37.936, -71.516), - new LatLng(37.936, -80.425)); - - final ImageSource imageSource = new ImageSource(ID_IMAGE_SOURCE, quad, R.drawable.southeast_radar_0); - final RasterLayer layer = new RasterLayer(ID_IMAGE_LAYER, ID_IMAGE_SOURCE); - map.setStyle(new Style.Builder() - .fromUri(Style.MAPBOX_STREETS) - .withSource(imageSource) - .withLayer(layer), style -> { - runnable = new RefreshImageRunnable(imageSource, handler); - handler.postDelayed(runnable, 100); - } - ); - } - - @Override - protected void onStart() { - super.onStart(); - mapView.onStart(); - } - - @Override - public void onResume() { - super.onResume(); - mapView.onResume(); - } - - @Override - public void onPause() { - super.onPause(); - mapView.onPause(); - } - - @Override - protected void onStop() { - super.onStop(); - mapView.onStop(); - handler.removeCallbacks(runnable); - } - - @Override - protected void onDestroy() { - super.onDestroy(); - mapView.onDestroy(); - } - - @Override - protected void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - mapView.onSaveInstanceState(outState); - } - - private static class RefreshImageRunnable implements Runnable { - - private ImageSource imageSource; - private Handler handler; - private Bitmap[] drawables; - private int drawableIndex; - - Bitmap getBitmap(int resourceId) { - Context context = Mapbox.getApplicationContext(); - Drawable drawable = BitmapUtils.getDrawableFromRes(context, resourceId); - if (drawable instanceof BitmapDrawable) { - BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; - return bitmapDrawable.getBitmap(); - } - return null; - } - - RefreshImageRunnable(ImageSource imageSource, Handler handler) { - this.imageSource = imageSource; - this.handler = handler; - drawables = new Bitmap[4]; - drawables[0] = getBitmap(R.drawable.southeast_radar_0); - drawables[1] = getBitmap(R.drawable.southeast_radar_1); - drawables[2] = getBitmap(R.drawable.southeast_radar_2); - drawables[3] = getBitmap(R.drawable.southeast_radar_3); - drawableIndex = 1; - } - - @Override - public void run() { - imageSource.setImage(drawables[drawableIndex++]); - if (drawableIndex > 3) { - drawableIndex = 0; - } - handler.postDelayed(this, 1000); - } - } -} diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/BuildingFillExtrusionActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/BuildingFillExtrusionActivity.java deleted file mode 100644 index 846650f6cb..0000000000 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/BuildingFillExtrusionActivity.java +++ /dev/null @@ -1,151 +0,0 @@ -package com.mapbox.mapboxsdk.testapp.activity.style; - -import android.graphics.Color; -import android.os.Bundle; -import android.support.v7.app.AppCompatActivity; -import android.view.Menu; -import android.view.MenuItem; - -import com.mapbox.mapboxsdk.maps.MapView; -import com.mapbox.mapboxsdk.maps.MapboxMap; -import com.mapbox.mapboxsdk.maps.Style; -import com.mapbox.mapboxsdk.style.expressions.Expression; -import com.mapbox.mapboxsdk.style.layers.FillExtrusionLayer; -import com.mapbox.mapboxsdk.style.layers.Property; -import com.mapbox.mapboxsdk.style.light.Light; -import com.mapbox.mapboxsdk.style.light.Position; -import com.mapbox.mapboxsdk.testapp.R; -import com.mapbox.mapboxsdk.utils.ColorUtils; - -import static com.mapbox.mapboxsdk.style.expressions.Expression.eq; -import static com.mapbox.mapboxsdk.style.expressions.Expression.get; -import static com.mapbox.mapboxsdk.style.expressions.Expression.literal; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillExtrusionBase; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillExtrusionColor; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillExtrusionHeight; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillExtrusionOpacity; - -/** - * Test activity showing 3D buildings with a FillExtrusion Layer - */ -public class BuildingFillExtrusionActivity extends AppCompatActivity { - - private MapView mapView; - private MapboxMap mapboxMap; - private Light light; - - private boolean isMapAnchorLight; - private boolean isLowIntensityLight; - private boolean isRedColor; - private boolean isInitPosition; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_building_layer); - mapView = findViewById(R.id.mapView); - mapView.onCreate(savedInstanceState); - mapView.getMapAsync(map -> { - mapboxMap = map; - mapboxMap.setStyle(Style.MAPBOX_STREETS, style -> { - setupBuildings(style); - setupLight(); - }); - }); - } - - private void setupBuildings(Style style) { - FillExtrusionLayer fillExtrusionLayer = new FillExtrusionLayer("3d-buildings", "composite"); - fillExtrusionLayer.setSourceLayer("building"); - fillExtrusionLayer.setFilter(eq(get("extrude"), literal("true"))); - fillExtrusionLayer.setMinZoom(15); - fillExtrusionLayer.setProperties( - fillExtrusionColor(Color.LTGRAY), - fillExtrusionHeight(Expression.get("height")), - fillExtrusionBase(Expression.get("min_height")), - fillExtrusionOpacity(0.9f) - ); - style.addLayer(fillExtrusionLayer); - } - - private void setupLight() { - light = mapboxMap.getStyle().getLight(); - - findViewById(R.id.fabLightPosition).setOnClickListener(v -> { - isInitPosition = !isInitPosition; - if (isInitPosition) { - light.setPosition(new Position(1.5f, 90, 80)); - } else { - light.setPosition(new Position(1.15f, 210, 30)); - } - }); - - findViewById(R.id.fabLightColor).setOnClickListener(v -> { - isRedColor = !isRedColor; - light.setColor(ColorUtils.colorToRgbaString(isRedColor ? Color.RED : Color.BLUE)); - }); - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.menu_building, menu); - return true; - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - if (light != null) { - int id = item.getItemId(); - if (id == R.id.menu_action_anchor) { - isMapAnchorLight = !isMapAnchorLight; - light.setAnchor(isMapAnchorLight ? Property.ANCHOR_MAP : Property.ANCHOR_VIEWPORT); - } else if (id == R.id.menu_action_intensity) { - isLowIntensityLight = !isLowIntensityLight; - light.setIntensity(isLowIntensityLight ? 0.35f : 1.0f); - } - } - return super.onOptionsItemSelected(item); - } - - @Override - protected void onStart() { - super.onStart(); - mapView.onStart(); - } - - @Override - protected void onResume() { - super.onResume(); - mapView.onResume(); - } - - @Override - protected void onPause() { - super.onPause(); - mapView.onPause(); - } - - @Override - protected void onStop() { - super.onStop(); - mapView.onStop(); - } - - @Override - public void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - mapView.onSaveInstanceState(outState); - } - - @Override - public void onLowMemory() { - super.onLowMemory(); - mapView.onLowMemory(); - } - - @Override - public void onDestroy() { - super.onDestroy(); - mapView.onDestroy(); - } -} diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/CircleLayerActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/CircleLayerActivity.java deleted file mode 100644 index 076cd1d8e3..0000000000 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/CircleLayerActivity.java +++ /dev/null @@ -1,284 +0,0 @@ -package com.mapbox.mapboxsdk.testapp.activity.style; - -import android.graphics.Color; -import android.os.Bundle; -import android.support.design.widget.FloatingActionButton; -import android.support.v4.content.ContextCompat; -import android.support.v4.content.res.ResourcesCompat; -import android.support.v7.app.AppCompatActivity; -import android.view.View; -import com.mapbox.mapboxsdk.maps.MapView; -import com.mapbox.mapboxsdk.maps.MapboxMap; -import com.mapbox.mapboxsdk.maps.Style; -import com.mapbox.mapboxsdk.style.expressions.Expression; -import com.mapbox.mapboxsdk.style.layers.CircleLayer; -import com.mapbox.mapboxsdk.style.layers.SymbolLayer; -import com.mapbox.mapboxsdk.style.sources.GeoJsonOptions; -import com.mapbox.mapboxsdk.style.sources.GeoJsonSource; -import com.mapbox.mapboxsdk.testapp.R; -import timber.log.Timber; - -import java.net.URI; -import java.net.URISyntaxException; - -import static com.mapbox.mapboxsdk.style.expressions.Expression.all; -import static com.mapbox.mapboxsdk.style.expressions.Expression.get; -import static com.mapbox.mapboxsdk.style.expressions.Expression.gt; -import static com.mapbox.mapboxsdk.style.expressions.Expression.gte; -import static com.mapbox.mapboxsdk.style.expressions.Expression.has; -import static com.mapbox.mapboxsdk.style.expressions.Expression.literal; -import static com.mapbox.mapboxsdk.style.expressions.Expression.lt; -import static com.mapbox.mapboxsdk.style.expressions.Expression.toNumber; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circleColor; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circleRadius; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconImage; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textAllowOverlap; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textColor; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textField; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textIgnorePlacement; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textSize; - - -/** - * Test activity showcasing adding a Circle Layer to the Map - * <p> - * Uses bus stop data from Singapore as a source and allows to filter into 1 specific route with a line layer. - * </p> - */ -public class CircleLayerActivity extends AppCompatActivity implements View.OnClickListener { - - public static final String SOURCE_ID = "bus_stop"; - public static final String SOURCE_ID_CLUSTER = "bus_stop_cluster"; - public static final String URL_BUS_ROUTES = "https://raw.githubusercontent.com/cheeaun/busrouter-sg/master/data/2/bus-stops.geojson"; - public static final String LAYER_ID = "stops_layer"; - private MapView mapView; - private MapboxMap mapboxMap; - - private FloatingActionButton styleFab; - private FloatingActionButton routeFab; - - private CircleLayer layer; - private GeoJsonSource source; - - private int currentStyleIndex = 0; - private boolean isLoadingStyle = true; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_circle_layer); - - mapView = findViewById(R.id.mapView); - mapView.onCreate(savedInstanceState); - mapView.getMapAsync(map -> { - mapboxMap = map; - mapboxMap.setStyle(Style.SATELLITE_STREETS); - mapView.addOnDidFinishLoadingStyleListener(() -> { - Style style = mapboxMap.getStyle(); - addBusStopSource(style); - addBusStopCircleLayer(style); - initFloatingActionButtons(); - isLoadingStyle = false; - }); - }); - } - - private void addBusStopSource(Style style) { - try { - source = new GeoJsonSource(SOURCE_ID, new URI(URL_BUS_ROUTES)); - } catch (URISyntaxException exception) { - Timber.e(exception, "That's not an url... "); - } - style.addSource(source); - } - - private void addBusStopCircleLayer(Style style) { - layer = new CircleLayer(LAYER_ID, SOURCE_ID); - layer.setProperties( - circleColor(Color.parseColor("#FF9800")), - circleRadius(2.0f) - ); - style.addLayerBelow(layer, "waterway-label"); - } - - private void initFloatingActionButtons() { - routeFab = findViewById(R.id.fab_route); - routeFab.setColorFilter(ContextCompat.getColor(CircleLayerActivity.this, R.color.primary)); - routeFab.setOnClickListener(CircleLayerActivity.this); - - styleFab = findViewById(R.id.fab_style); - styleFab.setOnClickListener(CircleLayerActivity.this); - } - - @Override - public void onClick(View view) { - if (isLoadingStyle) { - return; - } - - if (view.getId() == R.id.fab_route) { - showBusCluster(); - } else if (view.getId() == R.id.fab_style) { - changeMapStyle(); - } - } - - private void showBusCluster() { - removeFabs(); - removeOldSource(); - addClusteredSource(); - } - - private void removeOldSource() { - mapboxMap.getStyle().removeSource(SOURCE_ID); - mapboxMap.getStyle().removeLayer(LAYER_ID); - } - - private void addClusteredSource() { - try { - mapboxMap.getStyle().addSource( - new GeoJsonSource(SOURCE_ID_CLUSTER, - new URI(URL_BUS_ROUTES), - new GeoJsonOptions() - .withCluster(true) - .withClusterMaxZoom(14) - .withClusterRadius(50) - ) - ); - } catch (URISyntaxException malformedUrlException) { - Timber.e(malformedUrlException, "That's not an url... "); - } - - // Add unclustered layer - int[][] layers = new int[][] { - new int[] {150, ResourcesCompat.getColor(getResources(), R.color.redAccent, getTheme())}, - new int[] {20, ResourcesCompat.getColor(getResources(), R.color.greenAccent, getTheme())}, - new int[] {0, ResourcesCompat.getColor(getResources(), R.color.blueAccent, getTheme())} - }; - - SymbolLayer unclustered = new SymbolLayer("unclustered-points", SOURCE_ID_CLUSTER); - unclustered.setProperties( - iconImage("bus-15") - ); - - mapboxMap.getStyle().addLayer(unclustered); - - for (int i = 0; i < layers.length; i++) { - // Add some nice circles - CircleLayer circles = new CircleLayer("cluster-" + i, SOURCE_ID_CLUSTER); - circles.setProperties( - circleColor(layers[i][1]), - circleRadius(18f) - ); - - Expression pointCount = toNumber(get("point_count")); - circles.setFilter( - i == 0 - ? all(has("point_count"), - gte(pointCount, literal(layers[i][0])) - ) : all(has("point_count"), - gt(pointCount, literal(layers[i][0])), - lt(pointCount, literal(layers[i - 1][0])) - ) - ); - mapboxMap.getStyle().addLayer(circles); - } - - // Add the count labels - SymbolLayer count = new SymbolLayer("count", SOURCE_ID_CLUSTER); - count.setProperties( - textField(Expression.toString(get("point_count"))), - textSize(12f), - textColor(Color.WHITE), - textIgnorePlacement(true), - textAllowOverlap(true) - ); - mapboxMap.getStyle().addLayer(count); - } - - private void removeFabs() { - routeFab.setVisibility(View.GONE); - styleFab.setVisibility(View.GONE); - } - - private void changeMapStyle() { - isLoadingStyle = true; - removeBusStop(); - loadNewStyle(); - } - - private void removeBusStop() { - mapboxMap.getStyle().removeLayer(layer); - mapboxMap.getStyle().removeSource(source); - } - - private void loadNewStyle() { - mapboxMap.setStyle(new Style.Builder().fromUri(getNextStyle())); - } - - private void addBusStop() { - mapboxMap.getStyle().addLayer(layer); - mapboxMap.getStyle().addSource(source); - } - - private String getNextStyle() { - currentStyleIndex++; - if (currentStyleIndex == Data.STYLES.length) { - currentStyleIndex = 0; - } - return Data.STYLES[currentStyleIndex]; - } - - @Override - protected void onStart() { - super.onStart(); - mapView.onStart(); - } - - @Override - protected void onResume() { - super.onResume(); - mapView.onResume(); - } - - @Override - protected void onPause() { - super.onPause(); - mapView.onPause(); - } - - @Override - protected void onStop() { - super.onStop(); - mapView.onStop(); - } - - @Override - public void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - mapView.onSaveInstanceState(outState); - } - - @Override - public void onLowMemory() { - super.onLowMemory(); - mapView.onLowMemory(); - } - - @Override - public void onDestroy() { - super.onDestroy(); - mapView.onDestroy(); - } - - private static class Data { - private static final String[] STYLES = new String[] { - Style.MAPBOX_STREETS, - Style.OUTDOORS, - Style.LIGHT, - Style.DARK, - Style.SATELLITE, - Style.SATELLITE_STREETS - }; - } -} diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/CustomSpriteActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/CustomSpriteActivity.java deleted file mode 100644 index 2b2b90b606..0000000000 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/CustomSpriteActivity.java +++ /dev/null @@ -1,144 +0,0 @@ -package com.mapbox.mapboxsdk.testapp.activity.style; - -import android.graphics.BitmapFactory; -import android.os.Bundle; -import android.support.design.widget.FloatingActionButton; -import android.support.v4.content.ContextCompat; -import android.support.v7.app.AppCompatActivity; -import android.view.View; - -import com.mapbox.geojson.Feature; -import com.mapbox.geojson.FeatureCollection; -import com.mapbox.geojson.Point; -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.Style; -import com.mapbox.mapboxsdk.style.layers.Layer; -import com.mapbox.mapboxsdk.style.layers.SymbolLayer; -import com.mapbox.mapboxsdk.style.sources.GeoJsonSource; -import com.mapbox.mapboxsdk.testapp.R; - - -import timber.log.Timber; - -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconAllowOverlap; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconIgnorePlacement; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconImage; - -/** - * Test activity showcasing adding a sprite image and use it in a Symbol Layer - */ -public class CustomSpriteActivity extends AppCompatActivity { - - private static final String CUSTOM_ICON = "custom-icon"; - - private GeoJsonSource source; - private MapboxMap mapboxMap; - private MapView mapView; - private Layer layer; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_add_sprite); - - mapView = findViewById(R.id.mapView); - mapView.onCreate(savedInstanceState); - mapView.getMapAsync(map -> { - mapboxMap = map; - - map.setStyle(Style.MAPBOX_STREETS, style -> { - final FloatingActionButton fab = findViewById(R.id.fab); - fab.setColorFilter(ContextCompat.getColor(CustomSpriteActivity.this, R.color.primary)); - fab.setOnClickListener(new View.OnClickListener() { - - private Point point; - - @Override - public void onClick(View view) { - if (point == null) { - Timber.i("First click -> Car"); - // Add an icon to reference later - style.addImage(CUSTOM_ICON, BitmapFactory.decodeResource(getResources(), R.drawable.ic_car_top)); - - // Add a source with a geojson point - point = Point.fromLngLat(13.400972d, 52.519003d); - source = new GeoJsonSource( - "point", - FeatureCollection.fromFeatures(new Feature[] {Feature.fromGeometry(point)}) - ); - mapboxMap.getStyle().addSource(source); - - // Add a symbol layer that references that point source - layer = new SymbolLayer("layer", "point"); - layer.setProperties( - // Set the id of the sprite to use - iconImage(CUSTOM_ICON), - iconAllowOverlap(true), - iconIgnorePlacement(true) - ); - - // lets add a circle below labels! - mapboxMap.getStyle().addLayerBelow(layer, "waterway-label"); - - fab.setImageResource(R.drawable.ic_directions_car_black); - } else { - // Update point - point = Point.fromLngLat(point.longitude() + 0.001, - point.latitude() + 0.001); - source.setGeoJson(FeatureCollection.fromFeatures(new Feature[] {Feature.fromGeometry(point)})); - - // Move the camera as well - mapboxMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng( - point.latitude(), point.longitude()))); - } - } - }); - }); - }); - } - - @Override - protected void onStart() { - super.onStart(); - mapView.onStart(); - } - - @Override - protected void onResume() { - super.onResume(); - mapView.onResume(); - } - - @Override - protected void onPause() { - super.onPause(); - mapView.onPause(); - } - - @Override - protected void onStop() { - super.onStop(); - mapView.onStop(); - } - - @Override - public void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - mapView.onSaveInstanceState(outState); - } - - @Override - public void onLowMemory() { - super.onLowMemory(); - mapView.onLowMemory(); - } - - @Override - public void onDestroy() { - super.onDestroy(); - mapView.onDestroy(); - } -} diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/DataDrivenStyleActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/DataDrivenStyleActivity.java deleted file mode 100644 index f566944542..0000000000 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/DataDrivenStyleActivity.java +++ /dev/null @@ -1,472 +0,0 @@ -package com.mapbox.mapboxsdk.testapp.activity.style; - -import android.graphics.Color; -import android.os.Bundle; -import android.support.v7.app.AppCompatActivity; -import android.view.Menu; -import android.view.MenuItem; -import android.widget.TextView; -import android.widget.Toast; - -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.Style; -import com.mapbox.mapboxsdk.style.layers.FillLayer; -import com.mapbox.mapboxsdk.style.sources.GeoJsonSource; -import com.mapbox.mapboxsdk.style.sources.Source; -import com.mapbox.mapboxsdk.testapp.R; -import com.mapbox.mapboxsdk.testapp.utils.IdleZoomListener; -import com.mapbox.mapboxsdk.testapp.utils.ResourceUtils; - -import java.io.IOException; - -import timber.log.Timber; - -import static com.mapbox.mapboxsdk.style.expressions.Expression.exponential; -import static com.mapbox.mapboxsdk.style.expressions.Expression.get; -import static com.mapbox.mapboxsdk.style.expressions.Expression.interpolate; -import static com.mapbox.mapboxsdk.style.expressions.Expression.linear; -import static com.mapbox.mapboxsdk.style.expressions.Expression.literal; -import static com.mapbox.mapboxsdk.style.expressions.Expression.match; -import static com.mapbox.mapboxsdk.style.expressions.Expression.rgb; -import static com.mapbox.mapboxsdk.style.expressions.Expression.rgba; -import static com.mapbox.mapboxsdk.style.expressions.Expression.step; -import static com.mapbox.mapboxsdk.style.expressions.Expression.stop; -import static com.mapbox.mapboxsdk.style.expressions.Expression.zoom; -import static com.mapbox.mapboxsdk.style.expressions.Expression.color; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillAntialias; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillColor; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillOpacity; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillOutlineColor; - -/** - * Test activity showcasing the data driven runtime style API. - */ -public class DataDrivenStyleActivity extends AppCompatActivity { - - public static final String AMSTERDAM_PARKS_LAYER = "amsterdam-parks-layer"; - private MapView mapView; - private MapboxMap mapboxMap; - private IdleZoomListener idleListener; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_data_driven_style); - - // Initialize map as normal - mapView = findViewById(R.id.mapView); - mapView.onCreate(savedInstanceState); - - mapView.getMapAsync(map -> { - // Store for later - mapboxMap = map; - - mapboxMap.setStyle(Style.MAPBOX_STREETS, style -> { - // Add a parks layer - addParksLayer(); - - // Add debug overlay - setupDebugZoomView(); - }); - - // Center and Zoom (Amsterdam, zoomed to streets) - mapboxMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(52.379189, 4.899431), 14)); - }); - } - - private void setupDebugZoomView() { - final TextView textView = (TextView) findViewById(R.id.textZoom); - mapboxMap.addOnCameraIdleListener(idleListener = new IdleZoomListener(mapboxMap, textView)); - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.menu_data_driven_style, menu); - return true; - } - - @Override - protected void onStart() { - super.onStart(); - mapView.onStart(); - } - - @Override - protected void onResume() { - super.onResume(); - mapView.onResume(); - } - - @Override - protected void onPause() { - super.onPause(); - mapView.onPause(); - } - - @Override - protected void onStop() { - super.onStop(); - mapView.onStop(); - } - - @Override - protected void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - mapView.onSaveInstanceState(outState); - } - - @Override - protected void onDestroy() { - super.onDestroy(); - if (mapboxMap != null && idleListener != null) { - mapboxMap.removeOnCameraIdleListener(idleListener); - } - mapView.onDestroy(); - } - - @Override - public void onLowMemory() { - super.onLowMemory(); - mapView.onLowMemory(); - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case R.id.action_add_exponential_zoom_function: - addExponentialZoomFunction(); - return true; - case R.id.action_add_interval_zoom_function: - addIntervalZoomFunction(); - return true; - case R.id.action_add_categorical_source_function: - addCategoricalSourceFunction(); - return true; - case R.id.action_add_exponential_source_function: - addExponentialSourceFunction(); - return true; - case R.id.action_add_identity_source_function: - addIdentitySourceFunction(); - return true; - case R.id.action_add_interval_source_function: - addIntervalSourceFunction(); - return true; - case R.id.action_add_composite_categorical_function: - addCompositeCategoricalFunction(); - return true; - case R.id.action_add_composite_exponential_function: - addCompositeExponentialFunction(); - return true; - case R.id.action_add_composite_interval_function: - addCompositeIntervalFunction(); - return true; - default: - return super.onOptionsItemSelected(item); - } - } - - private void addExponentialZoomFunction() { - Timber.i("Add exponential zoom function"); - FillLayer layer = mapboxMap.getStyle().getLayerAs("water"); - assert layer != null; - layer.setProperties( - fillColor( - interpolate( - exponential(0.5f), zoom(), - stop(1, color(Color.RED)), - stop(5, color(Color.BLUE)), - stop(10, color(Color.GREEN)) - ) - ) - ); - - Timber.i("Fill color: %s", layer.getFillColor()); - } - - private void addIntervalZoomFunction() { - Timber.i("Add interval zoom function"); - FillLayer layer = mapboxMap.getStyle().getLayerAs("water"); - assert layer != null; - layer.setProperties( - fillColor( - step(zoom(), - rgba(0.0f, 255.0f, 255.0f, 1.0f), - stop(1, rgba(255.0f, 0.0f, 0.0f, 1.0f)), - stop(5, rgba(0.0f, 0.0f, 255.0f, 1.0f)), - stop(10, rgba(0.0f, 255.0f, 0.0f, 1.0f)) - ) - ) - ); - - Timber.i("Fill color: %s", layer.getFillColor()); - } - - private void addExponentialSourceFunction() { - Timber.i("Add exponential source function"); - FillLayer layer = mapboxMap.getStyle().getLayerAs(AMSTERDAM_PARKS_LAYER); - assert layer != null; - layer.setProperties( - fillColor( - interpolate( - exponential(0.5f), - get("stroke-width"), - stop(1f, rgba(255.0f, 0.0f, 0.0f, 1.0f)), - stop(5f, rgba(0.0f, 0.0f, 255.0f, 1.0f)), - stop(10f, rgba(0.0f, 255.0f, 0.0f, 1.0f)) - ) - ) - ); - - Timber.i("Fill color: %s", layer.getFillColor()); - } - - private void addCategoricalSourceFunction() { - Timber.i("Add categorical source function"); - FillLayer layer = mapboxMap.getStyle().getLayerAs(AMSTERDAM_PARKS_LAYER); - assert layer != null; - layer.setProperties( - fillColor( - match( - get("name"), - literal("Westerpark"), rgba(255.0f, 0.0f, 0.0f, 1.0f), - literal("Jordaan"), rgba(0.0f, 0.0f, 255.0f, 1.0f), - literal("Prinseneiland"), rgba(0.0f, 255.0f, 0.0f, 1.0f), - rgba(0.0f, 255.0f, 255.0f, 1.0f) - ) - ) - ); - - Timber.i("Fill color: %s", layer.getFillColor()); - } - - private void addIdentitySourceFunction() { - Timber.i("Add identity source function"); - FillLayer layer = mapboxMap.getStyle().getLayerAs(AMSTERDAM_PARKS_LAYER); - assert layer != null; - layer.setProperties( - fillOpacity( - get("fill-opacity") - ) - ); - - Timber.i("Fill opacity: %s", layer.getFillOpacity()); - } - - private void addIntervalSourceFunction() { - Timber.i("Add interval source function"); - FillLayer layer = mapboxMap.getStyle().getLayerAs(AMSTERDAM_PARKS_LAYER); - assert layer != null; - layer.setProperties( - fillColor( - step( - get("stroke-width"), - rgba(0.0f, 255.0f, 255.0f, 1.0f), - stop(1f, rgba(255.0f, 0.0f, 0.0f, 1.0f)), - stop(2f, rgba(0.0f, 0.0f, 255.0f, 1.0f)), - stop(3f, rgba(0.0f, 255.0f, 0.0f, 1.0f)) - ) - ) - ); - - Timber.i("Fill color: %s", layer.getFillColor()); - } - - private void addCompositeExponentialFunction() { - Timber.i("Add composite exponential function"); - FillLayer layer = mapboxMap.getStyle().getLayerAs(AMSTERDAM_PARKS_LAYER); - assert layer != null; - layer.setProperties( - fillColor( - interpolate( - exponential(1f), - zoom(), - stop(12, step( - get("stroke-width"), - rgba(255.0f, 255.0f, 255.0f, 1.0f), - stop(1f, rgba(255.0f, 0.0f, 0.0f, 1.0f)), - stop(2f, rgba(0.0f, 0.0f, 0.0f, 1.0f)), - stop(3f, rgba(0.0f, 0.0f, 255.0f, 1.0f)) - )), - stop(15, step( - get("stroke-width"), - rgba(255.0f, 255.0f, 255.0f, 1.0f), - stop(1f, rgba(255.0f, 255.0f, 0.0f, 1.0f)), - stop(2f, rgba(211.0f, 211.0f, 211.0f, 1.0f)), - stop(3f, rgba(0.0f, 255.0f, 255.0f, 1.0f)) - )), - stop(18, step( - get("stroke-width"), - rgba(255.0f, 255.0f, 255.0f, 1.0f), - stop(1f, rgba(0.0f, 0.0f, 0.0f, 1.0f)), - stop(2f, rgba(128.0f, 128.0f, 128.0f, 1.0f)), - stop(3f, rgba(0.0f, 255.0f, 0.0f, 1.0f))) - ) - ) - ) - ); - - Timber.i("Fill color: %s", layer.getFillColor()); - } - - private void addCompositeIntervalFunction() { - Timber.i("Add composite interval function"); - FillLayer layer = mapboxMap.getStyle().getLayerAs(AMSTERDAM_PARKS_LAYER); - assert layer != null; - layer.setProperties( - fillColor( - interpolate( - linear(), - zoom(), - stop(12, step( - get("stroke-width"), - rgba(255.0f, 255.0f, 255.0f, 1.0f), - stop(1f, rgba(255.0f, 0.0f, 0.0f, 1.0f)), - stop(2f, rgba(0.0f, 0.0f, 0.0f, 1.0f)), - stop(3f, rgba(0.0f, 0.0f, 255.0f, 1.0f)) - )), - stop(15, step( - get("stroke-width"), - rgba(255.0f, 255.0f, 255.0f, 1.0f), - stop(1f, rgba(255.0f, 255.0f, 0.0f, 1.0f)), - stop(2f, rgba(211.0f, 211.0f, 211.0f, 1.0f)), - stop(3f, rgba(0.0f, 255.0f, 255.0f, 1.0f)) - )), - stop(18, step( - get("stroke-width"), - rgba(255.0f, 255.0f, 255.0f, 1.0f), - stop(1f, rgba(0.0f, 0.0f, 0.0f, 1.0f)), - stop(2f, rgba(128.0f, 128.0f, 128.0f, 1.0f)), - stop(3f, rgba(0.0f, 255.0f, 0.0f, 1.0f)) - )) - ) - ) - ); - - Timber.i("Fill color: %s", layer.getFillColor()); - } - - private void addCompositeCategoricalFunction() { - Timber.i("Add composite categorical function"); - FillLayer layer = mapboxMap.getStyle().getLayerAs(AMSTERDAM_PARKS_LAYER); - assert layer != null; - layer.setProperties( - fillColor( - step(zoom(), - rgba(255.0f, 255.0f, 255.0f, 1.0f), - stop(7f, match( - get("name"), - literal("Westerpark"), rgba(255.0f, 0.0f, 0.0f, 1.0f), - rgba(255.0f, 255.0f, 255.0f, 1.0f) - )), - stop(8f, match( - get("name"), - literal("Westerpark"), rgba(0.0f, 0.0f, 255.0f, 1.0f), - rgba(255.0f, 255.0f, 255.0f, 1.0f) - )), - stop(9f, match( - get("name"), - literal("Westerpark"), rgba(255.0f, 0.0f, 0.0f, 1.0f), - rgba(255.0f, 255.0f, 255.0f, 1.0f) - )), - stop(10f, match( - get("name"), - literal("Westerpark"), rgba(0.0f, 0.0f, 255.0f, 1.0f), - rgba(255.0f, 255.0f, 255.0f, 1.0f) - )), - stop(11f, match( - get("name"), - literal("Westerpark"), rgba(255.0f, 0.0f, 0.0f, 1.0f), - rgba(255.0f, 255.0f, 255.0f, 1.0f) - )), - stop(12f, match( - get("name"), - literal("Westerpark"), rgba(0.0f, 0.0f, 255.0f, 1.0f), - rgba(255.0f, 255.0f, 255.0f, 1.0f) - )), - stop(13f, match( - get("name"), - literal("Westerpark"), rgba(255.0f, 0.0f, 0.0f, 1.0f), - rgba(255.0f, 255.0f, 255.0f, 1.0f) - )), - stop(14f, match( - get("name"), - literal("Westerpark"), rgba(0.0f, 0.0f, 255.0f, 1.0f), - literal("Jordaan"), rgba(0.0f, 255.0f, 0.0f, 1.0f), - literal("PrinsenEiland"), rgba(0.0f, 0.0f, 0.0f, 1.0f), - rgba(255.0f, 255.0f, 255.0f, 1.0f) - )), - stop(15f, match( - get("name"), - literal("Westerpark"), rgba(255.0f, 0.0f, 0.0f, 1.0f), - rgba(255.0f, 255.0f, 255.0f, 1.0f) - )), - stop(16f, match( - get("name"), - literal("Westerpark"), rgba(0.0f, 0.0f, 255.0f, 1.0f), - rgba(255.0f, 255.0f, 255.0f, 1.0f) - )), - stop(17f, match( - get("name"), - literal("Westerpark"), rgba(255.0f, 0.0f, 0.0f, 1.0f), - rgba(255.0f, 255.0f, 255.0f, 1.0f) - )), - stop(18f, match( - get("name"), - literal("Westerpark"), rgba(0.0f, 0.0f, 255.0f, 1.0f), - literal("Jordaan"), rgba(0.0f, 255.0f, 255.0f, 1.0f), - rgba(255.0f, 255.0f, 255.0f, 1.0f) - )), - stop(19f, match( - get("name"), - literal("Westerpark"), rgba(255.0f, 0.0f, 0.0f, 1.0f), - rgba(255.0f, 255.0f, 255.0f, 1.0f) - )), - stop(20f, match( - get("name"), - literal("Westerpark"), rgba(0.0f, 0.0f, 255.0f, 1.0f), - rgba(255.0f, 255.0f, 255.0f, 1.0f) - )), - stop(21f, match( - get("name"), - literal("Westerpark"), rgba(255.0f, 0.0f, 0.0f, 1.0f), - rgba(255.0f, 255.0f, 255.0f, 1.0f) - )), - stop(22f, match( - get("name"), - literal("Westerpark"), rgba(0.0f, 0.0f, 255.0f, 1.0f), - rgba(255.0f, 255.0f, 255.0f, 1.0f) - )) - ) - ) - ); - - Timber.i("Fill color: %s", layer.getFillColor()); - } - - private void addParksLayer() { - // Add a source - Source source; - try { - source = new GeoJsonSource("amsterdam-parks-source", ResourceUtils.readRawResource(this, R.raw.amsterdam)); - mapboxMap.getStyle().addSource(source); - } catch (IOException ioException) { - Toast.makeText( - DataDrivenStyleActivity.this, - "Couldn't add source: " + ioException.getMessage(), - Toast.LENGTH_SHORT).show(); - return; - } - - // Add a fill layer - mapboxMap.getStyle().addLayer(new FillLayer(AMSTERDAM_PARKS_LAYER, source.getId()) - .withProperties( - fillColor(color(Color.GREEN)), - fillOutlineColor(rgb(0, 0, 255)), - fillAntialias(true) - ) - ); - } -} diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/DraggableMarkerActivity.kt b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/DraggableMarkerActivity.kt deleted file mode 100644 index 249c4e6ec8..0000000000 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/DraggableMarkerActivity.kt +++ /dev/null @@ -1,331 +0,0 @@ -package com.mapbox.mapboxsdk.testapp.activity.style - -import android.graphics.PointF -import android.os.Bundle -import android.support.design.widget.Snackbar -import android.support.v7.app.AppCompatActivity -import android.view.MotionEvent -import android.view.View -import com.mapbox.android.gestures.AndroidGesturesManager -import com.mapbox.android.gestures.MoveGestureDetector -import com.mapbox.geojson.Feature -import com.mapbox.geojson.FeatureCollection -import com.mapbox.geojson.Point -import com.mapbox.mapboxsdk.annotations.IconFactory -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.Style -import com.mapbox.mapboxsdk.style.layers.PropertyFactory.* -import com.mapbox.mapboxsdk.style.layers.SymbolLayer -import com.mapbox.mapboxsdk.style.sources.GeoJsonSource -import com.mapbox.mapboxsdk.testapp.R -import kotlinx.android.synthetic.main.activity_draggable_marker.* - -/** - * An Activity that showcases how to make symbols draggable. - */ -class DraggableMarkerActivity : AppCompatActivity() { - companion object { - private const val sourceId = "source_draggable" - private const val layerId = "layer_draggable" - private const val markerImageId = "marker_icon_draggable" - - private var latestId: Long = 0 - fun generateMarkerId(): String { - if (latestId == Long.MAX_VALUE) { - throw RuntimeException("You've added too many markers.") - } - return latestId++.toString() - } - } - - private val actionBarHeight: Int by lazy { - supportActionBar?.height ?: 0 - } - - // View property is required for activity sanity tests - // we perform reflection on this requires using findViewById - private lateinit var mapView: MapView - private lateinit var mapboxMap: MapboxMap - private val featureCollection = FeatureCollection.fromFeatures(mutableListOf()) - private val source = GeoJsonSource(sourceId, featureCollection) - private val layer = SymbolLayer(layerId, sourceId) - .withProperties( - iconImage(markerImageId), - iconAllowOverlap(true), - iconIgnorePlacement(true)) - - private var draggableSymbolsManager: DraggableSymbolsManager? = null - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - setContentView(R.layout.activity_draggable_marker) - - mapView = findViewById(R.id.mapView) - mapView.onCreate(savedInstanceState) - mapView.getMapAsync { mapboxMap -> - this.mapboxMap = mapboxMap - - mapboxMap.setStyle( - Style.Builder() - .fromUri(Style.MAPBOX_STREETS) - .withImage(markerImageId, IconFactory.getInstance(this).defaultMarker().bitmap) - .withSource(source) - .withLayer(layer) - ) - - // Add initial markers - addMarker(LatLng(52.407210, 16.924324)) - addMarker(LatLng(41.382679, 2.181555)) - addMarker(LatLng(51.514886, -0.112589)) - - // Initial camera position - mapboxMap.moveCamera(CameraUpdateFactory.newLatLngZoom( - LatLng(45.0, 8.0), 3.0 - )) - - mapboxMap.addOnMapClickListener { - // Adding a marker on map click - val features = mapboxMap.queryRenderedSymbols(it, layerId) - if (features.isEmpty()) { - addMarker(it) - } else { - // Displaying marker info on marker click - Snackbar.make( - mapView, - "Marker's position: %.4f, %.4f".format(it.latitude, it.longitude), - Snackbar.LENGTH_LONG) - .show() - } - - false - } - - draggableSymbolsManager = DraggableSymbolsManager( - mapView, mapboxMap, featureCollection, source, layerId, actionBarHeight, 0) - - // Adding symbol drag listeners - draggableSymbolsManager?.addOnSymbolDragListener(object : DraggableSymbolsManager.OnSymbolDragListener { - override fun onSymbolDragStarted(id: String) { - draggedMarkerPositionTv.visibility = View.VISIBLE - Snackbar.make( - mapView, - "Marker drag started (%s)".format(id), - Snackbar.LENGTH_SHORT) - .show() - } - - override fun onSymbolDrag(id: String) { - val point = featureCollection.features()?.find { - it.id() == id - }?.geometry() as Point - draggedMarkerPositionTv.text = "Dragged marker's position: %.4f, %.4f".format(point.latitude(), point.longitude()) - } - - override fun onSymbolDragFinished(id: String) { - draggedMarkerPositionTv.visibility = View.GONE - Snackbar.make( - mapView, - "Marker drag finished (%s)".format(id), - Snackbar.LENGTH_SHORT) - .show() - } - }) - } - } - - private fun addMarker(latLng: LatLng) { - featureCollection.features()?.add( - Feature.fromGeometry(Point.fromLngLat(latLng.longitude, latLng.latitude), null, generateMarkerId())) - source.setGeoJson(featureCollection) - } - - override fun dispatchTouchEvent(ev: MotionEvent?): Boolean { - // Dispatching parent's touch events to the manager - draggableSymbolsManager?.onParentTouchEvent(ev) - return super.dispatchTouchEvent(ev) - } - - /** - * A manager, that allows dragging symbols after they are long clicked. - * Since this manager lives outside of the Maps SDK, we need to intercept parent's motion events - * and pass them with [DraggableSymbolsManager.onParentTouchEvent]. - * If we were to try and overwrite [AppCompatActivity.onTouchEvent], those events would've been - * consumed by the map. - * - * We also need to setup a [DraggableSymbolsManager.androidGesturesManager], - * because after disabling map's gestures and starting the drag process - * we still need to listen for move gesture events which map won't be able to provide anymore. - * - * @param mapView the mapView - * @param mapboxMap the mapboxMap - * @param symbolsCollection the collection that contains all the symbols that we want to be draggable - * @param symbolsSource the source that contains the [symbolsCollection] - * @param symbolsLayerId the ID of the layer that the symbols are displayed on - * @param touchAreaShiftX X-axis padding that is applied to the parent's window motion event, - * as that window can be bigger than the [mapView]. - * @param touchAreaShiftY Y-axis padding that is applied to the parent's window motion event, - * as that window can be bigger than the [mapView]. - * @param touchAreaMaxX maximum value of X-axis motion event - * @param touchAreaMaxY maximum value of Y-axis motion event - */ - class DraggableSymbolsManager( - mapView: MapView, - private val mapboxMap: MapboxMap, - private val symbolsCollection: FeatureCollection, - private val symbolsSource: GeoJsonSource, - private val symbolsLayerId: String, - private val touchAreaShiftY: Int = 0, - private val touchAreaShiftX: Int = 0, - private val touchAreaMaxX: Int = mapView.width, - private val touchAreaMaxY: Int = mapView.height - ) { - - private val androidGesturesManager: AndroidGesturesManager = AndroidGesturesManager(mapView.context, false) - private var draggedSymbolId: String? = null - private val onSymbolDragListeners: MutableList<OnSymbolDragListener> = mutableListOf<OnSymbolDragListener>() - - init { - mapboxMap.addOnMapLongClickListener { - // Starting the drag process on long click - draggedSymbolId = mapboxMap.queryRenderedSymbols(it, symbolsLayerId).firstOrNull()?.id()?.also { id -> - mapboxMap.uiSettings.setAllGesturesEnabled(false) - mapboxMap.gesturesManager.moveGestureDetector.interrupt() - notifyOnSymbolDragListeners { - onSymbolDragStarted(id) - } - } - false - } - - androidGesturesManager.setMoveGestureListener(MyMoveGestureListener()) - } - - inner class MyMoveGestureListener : MoveGestureDetector.OnMoveGestureListener { - override fun onMoveBegin(detector: MoveGestureDetector): Boolean { - return true - } - - override fun onMove(detector: MoveGestureDetector, distanceX: Float, distanceY: Float): Boolean { - if (detector.pointersCount > 1) { - // Stopping the drag when we don't work with a simple, on-pointer move anymore - stopDragging() - return true - } - - // Updating symbol's position - draggedSymbolId?.also { draggedSymbolId -> - val moveObject = detector.getMoveObject(0) - val point = PointF(moveObject.currentX - touchAreaShiftX, moveObject.currentY - touchAreaShiftY) - - if (point.x < 0 || point.y < 0 || point.x > touchAreaMaxX || point.y > touchAreaMaxY) { - stopDragging() - } - - val latLng = mapboxMap.projection.fromScreenLocation(point) - - symbolsCollection.features()?.indexOfFirst { - it.id() == draggedSymbolId - }?.also { index -> - symbolsCollection.features()?.get(index)?.also { oldFeature -> - val properties = oldFeature.properties() - val newFeature = Feature.fromGeometry( - Point.fromLngLat(latLng.longitude, latLng.latitude), - properties, - draggedSymbolId - ) - symbolsCollection.features()?.set(index, newFeature) - symbolsSource.setGeoJson(symbolsCollection) - notifyOnSymbolDragListeners { - onSymbolDrag(draggedSymbolId) - } - return true - } - } - } - - return false - } - - override fun onMoveEnd(detector: MoveGestureDetector, velocityX: Float, velocityY: Float) { - // Stopping the drag when move ends - stopDragging() - } - } - - private fun stopDragging() { - mapboxMap.uiSettings.setAllGesturesEnabled(true) - draggedSymbolId?.let { - notifyOnSymbolDragListeners { - onSymbolDragFinished(it) - } - } - draggedSymbolId = null - } - - fun onParentTouchEvent(ev: MotionEvent?) { - androidGesturesManager.onTouchEvent(ev) - } - - private fun notifyOnSymbolDragListeners(action: OnSymbolDragListener.() -> Unit) { - onSymbolDragListeners.forEach(action) - } - - fun addOnSymbolDragListener(listener: OnSymbolDragListener) { - onSymbolDragListeners.add(listener) - } - - fun removeOnSymbolDragListener(listener: OnSymbolDragListener) { - onSymbolDragListeners.remove(listener) - } - - interface OnSymbolDragListener { - fun onSymbolDragStarted(id: String) - fun onSymbolDrag(id: String) - fun onSymbolDragFinished(id: String) - } - } - - override fun onStart() { - super.onStart() - mapView.onStart() - } - - override fun onResume() { - super.onResume() - mapView.onResume() - } - - override fun onPause() { - super.onPause() - mapView.onPause() - } - - override fun onStop() { - super.onStop() - mapView.onStop() - } - - override fun onLowMemory() { - super.onLowMemory() - mapView.onLowMemory() - } - - override fun onDestroy() { - super.onDestroy() - mapView.onDestroy() - } - - override fun onSaveInstanceState(outState: Bundle?) { - super.onSaveInstanceState(outState) - outState?.let { - mapView.onSaveInstanceState(it) - } - } -} - -private fun MapboxMap.queryRenderedSymbols(latLng: LatLng, layerId: String): List<Feature> { - return this.queryRenderedFeatures(this.projection.toScreenLocation(latLng), layerId) -}
\ No newline at end of file diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/FillExtrusionActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/FillExtrusionActivity.java deleted file mode 100644 index ad953ef6b3..0000000000 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/FillExtrusionActivity.java +++ /dev/null @@ -1,122 +0,0 @@ -package com.mapbox.mapboxsdk.testapp.activity.style; - -import android.graphics.Color; -import android.os.Bundle; -import android.support.v7.app.AppCompatActivity; - -import com.mapbox.geojson.Point; -import com.mapbox.mapboxsdk.camera.CameraPosition; -import com.mapbox.mapboxsdk.camera.CameraUpdateFactory; -import com.mapbox.mapboxsdk.geometry.LatLng; -import com.mapbox.mapboxsdk.maps.MapView; -import com.mapbox.mapboxsdk.maps.Style; -import com.mapbox.mapboxsdk.style.layers.FillExtrusionLayer; -import com.mapbox.mapboxsdk.style.sources.GeoJsonSource; -import com.mapbox.mapboxsdk.testapp.R; -import com.mapbox.geojson.Polygon; - -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillExtrusionColor; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillExtrusionHeight; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillExtrusionOpacity; - -/** - * Test activity showcasing fill extrusions - */ -public class FillExtrusionActivity extends AppCompatActivity { - - private MapView mapView; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_fill_extrusion_layer); - - mapView = findViewById(R.id.mapView); - mapView.onCreate(savedInstanceState); - mapView.getMapAsync(mapboxMap -> { - mapboxMap.setStyle(Style.MAPBOX_STREETS, style -> { - List<List<Point>> lngLats = Collections.singletonList( - Arrays.asList( - Point.fromLngLat(5.12112557888031, 52.09071040847704), - Point.fromLngLat(5.121227502822875, 52.09053901776669), - Point.fromLngLat(5.121484994888306, 52.090601641371805), - Point.fromLngLat(5.1213884353637695, 52.090766439912635), - Point.fromLngLat(5.12112557888031, 52.09071040847704) - ) - ); - - Polygon domTower = Polygon.fromLngLats(lngLats); - GeoJsonSource source = new GeoJsonSource("extrusion-source", domTower); - style.addSource(source); - - style.addLayer( - new FillExtrusionLayer("extrusion-layer", source.getId()) - .withProperties( - fillExtrusionHeight(40f), - fillExtrusionOpacity(0.5f), - fillExtrusionColor(Color.RED) - ) - ); - - mapboxMap.animateCamera( - CameraUpdateFactory.newCameraPosition( - new CameraPosition.Builder() - .target(new LatLng(52.09071040847704, 5.12112557888031)) - .tilt(45.0) - .zoom(18) - .build() - ), - 10000 - ); - }); - }); - } - - - @Override - protected void onStart() { - super.onStart(); - mapView.onStart(); - } - - @Override - protected void onResume() { - super.onResume(); - mapView.onResume(); - } - - @Override - protected void onPause() { - super.onPause(); - mapView.onPause(); - } - - @Override - protected void onStop() { - super.onStop(); - mapView.onStop(); - } - - @Override - public void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - mapView.onSaveInstanceState(outState); - } - - @Override - public void onLowMemory() { - super.onLowMemory(); - mapView.onLowMemory(); - } - - @Override - public void onDestroy() { - super.onDestroy(); - mapView.onDestroy(); - } - -} diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/FillExtrusionStyleTestActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/FillExtrusionStyleTestActivity.java deleted file mode 100644 index 7f6fe4d0af..0000000000 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/FillExtrusionStyleTestActivity.java +++ /dev/null @@ -1,78 +0,0 @@ -package com.mapbox.mapboxsdk.testapp.activity.style; - -import android.os.Bundle; -import android.support.v7.app.AppCompatActivity; -import com.mapbox.mapboxsdk.maps.MapView; -import com.mapbox.mapboxsdk.maps.MapboxMap; -import com.mapbox.mapboxsdk.maps.Style; -import com.mapbox.mapboxsdk.testapp.R; - -/** - * Test activity used for instrumentation tests of fill extrusion. - */ -public class FillExtrusionStyleTestActivity extends AppCompatActivity { - - public MapView mapView; - private MapboxMap mapboxMap; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_extrusion_test); - - // Initialize map as normal - mapView = (MapView) findViewById(R.id.mapView); - mapView.onCreate(savedInstanceState); - mapView.getMapAsync(mapboxMap -> - mapboxMap.setStyle(new Style.Builder().fromUri(Style.MAPBOX_STREETS), - style -> FillExtrusionStyleTestActivity.this.mapboxMap = mapboxMap - ) - ); - } - - public MapboxMap getMapboxMap() { - return mapboxMap; - } - - @Override - protected void onStart() { - super.onStart(); - mapView.onStart(); - } - - @Override - protected void onResume() { - super.onResume(); - mapView.onResume(); - } - - @Override - protected void onPause() { - super.onPause(); - mapView.onPause(); - } - - @Override - protected void onStop() { - super.onStop(); - mapView.onStop(); - } - - @Override - protected void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - mapView.onSaveInstanceState(outState); - } - - @Override - protected void onDestroy() { - super.onDestroy(); - mapView.onDestroy(); - } - - @Override - public void onLowMemory() { - super.onLowMemory(); - mapView.onLowMemory(); - } -} diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/GeoJsonClusteringActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/GeoJsonClusteringActivity.java deleted file mode 100644 index cb2701d436..0000000000 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/GeoJsonClusteringActivity.java +++ /dev/null @@ -1,281 +0,0 @@ -package com.mapbox.mapboxsdk.testapp.activity.style; - -import android.graphics.Color; -import android.graphics.Point; -import android.graphics.PointF; -import android.os.Bundle; -import android.support.v4.content.res.ResourcesCompat; -import android.support.v7.app.AppCompatActivity; -import android.view.MenuItem; -import android.widget.Toast; -import com.mapbox.geojson.Feature; -import com.mapbox.geojson.FeatureCollection; -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.Style; -import com.mapbox.mapboxsdk.style.expressions.Expression; -import com.mapbox.mapboxsdk.style.layers.CircleLayer; -import com.mapbox.mapboxsdk.style.layers.SymbolLayer; -import com.mapbox.mapboxsdk.style.sources.GeoJsonOptions; -import com.mapbox.mapboxsdk.style.sources.GeoJsonSource; -import com.mapbox.mapboxsdk.testapp.R; -import com.mapbox.mapboxsdk.utils.BitmapUtils; -import timber.log.Timber; - -import java.net.URI; -import java.net.URISyntaxException; -import java.util.List; -import java.util.Objects; - -import static com.mapbox.mapboxsdk.style.expressions.Expression.all; -import static com.mapbox.mapboxsdk.style.expressions.Expression.accumulated; -import static com.mapbox.mapboxsdk.style.expressions.Expression.concat; -import static com.mapbox.mapboxsdk.style.expressions.Expression.division; -import static com.mapbox.mapboxsdk.style.expressions.Expression.exponential; -import static com.mapbox.mapboxsdk.style.expressions.Expression.get; -import static com.mapbox.mapboxsdk.style.expressions.Expression.gt; -import static com.mapbox.mapboxsdk.style.expressions.Expression.gte; -import static com.mapbox.mapboxsdk.style.expressions.Expression.has; -import static com.mapbox.mapboxsdk.style.expressions.Expression.interpolate; -import static com.mapbox.mapboxsdk.style.expressions.Expression.literal; -import static com.mapbox.mapboxsdk.style.expressions.Expression.lt; -import static com.mapbox.mapboxsdk.style.expressions.Expression.max; -import static com.mapbox.mapboxsdk.style.expressions.Expression.neq; -import static com.mapbox.mapboxsdk.style.expressions.Expression.rgb; -import static com.mapbox.mapboxsdk.style.expressions.Expression.stop; -import static com.mapbox.mapboxsdk.style.expressions.Expression.toNumber; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circleColor; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circleRadius; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconColor; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconImage; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconSize; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textAllowOverlap; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textColor; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textField; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textIgnorePlacement; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textSize; - -/** - * Test activity showcasing using a geojson source and visualise that source as a cluster by using filters. - */ -public class GeoJsonClusteringActivity extends AppCompatActivity { - - private static final double CAMERA_ZOOM_DELTA = 0.01; - private MapView mapView; - private MapboxMap mapboxMap; - - private GeoJsonSource clusterSource; - private int clickOptionCounter; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_geojson_clustering); - - // Initialize map as normal - mapView = findViewById(R.id.mapView); - // noinspection ConstantConditions - mapView.onCreate(savedInstanceState); - - mapView.getMapAsync(map -> { - mapboxMap = map; - mapboxMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(37.7749, 122.4194), 0)); - - final int[][] clusterLayers = new int[][] { - new int[] {150, ResourcesCompat.getColor(getResources(), R.color.redAccent, getTheme())}, - new int[] {20, ResourcesCompat.getColor(getResources(), R.color.greenAccent, getTheme())}, - new int[] {0, ResourcesCompat.getColor(getResources(), R.color.blueAccent, getTheme())} - }; - - try { - mapboxMap.setStyle(new Style.Builder() - .fromUri(Style.LIGHT) - .withSource(clusterSource = createClusterSource()) - .withLayer(createSymbolLayer()) - .withLayer(createClusterLevelLayer(0, clusterLayers)) - .withLayer(createClusterLevelLayer(1, clusterLayers)) - .withLayer(createClusterLevelLayer(2, clusterLayers)) - .withLayer(createClusterTextLayer()) - .withImage("icon-id", Objects.requireNonNull( - BitmapUtils.getBitmapFromDrawable(getResources().getDrawable(R.drawable.ic_hearing_black_24dp))), true - ) - ); - } catch (URISyntaxException exception) { - Timber.e(exception); - } - - mapboxMap.addOnMapClickListener(latLng -> { - PointF point = mapboxMap.getProjection().toScreenLocation(latLng); - List<Feature> features = mapboxMap.queryRenderedFeatures(point, "cluster-0", "cluster-1", "cluster-2"); - if (!features.isEmpty()) { - onClusterClick(features.get(0), new Point((int) point.x, (int) point.y)); - } - return true; - }); - }); - - findViewById(R.id.fab).setOnClickListener(v -> { - updateClickOptionCounter(); - notifyClickOptionUpdate(); - }); - } - - private void onClusterClick(Feature cluster, Point clickPoint) { - if (clickOptionCounter == 0) { - double nextZoomLevel = clusterSource.getClusterExpansionZoom(cluster); - double zoomDelta = nextZoomLevel - mapboxMap.getCameraPosition().zoom; - mapboxMap.animateCamera(CameraUpdateFactory.zoomBy(zoomDelta + CAMERA_ZOOM_DELTA, clickPoint)); - Toast.makeText(this, "Zooming to " + nextZoomLevel, Toast.LENGTH_SHORT).show(); - } else if (clickOptionCounter == 1) { - FeatureCollection collection = clusterSource.getClusterChildren(cluster); - Toast.makeText(this, "Children: " + collection.toJson(), Toast.LENGTH_SHORT).show(); - } else { - FeatureCollection collection = clusterSource.getClusterLeaves(cluster, 2, 1); - Toast.makeText(this, "Leaves: " + collection.toJson(), Toast.LENGTH_SHORT).show(); - } - } - - private GeoJsonSource createClusterSource() throws URISyntaxException { - return new GeoJsonSource("earthquakes", new URI("asset://earthquakes.geojson"), new GeoJsonOptions() - .withCluster(true) - .withClusterMaxZoom(14) - .withClusterRadius(50) - .withClusterProperty("max", max(accumulated(), get("max")), get("mag")) - .withClusterProperty("sum", literal("+"), get("mag")) - .withClusterProperty("felt", literal("any"), neq(get("felt"), literal("null"))) - ); - } - - private SymbolLayer createSymbolLayer() { - return new SymbolLayer("unclustered-points", "earthquakes") - .withProperties( - iconImage("icon-id"), - iconSize( - division( - get("mag"), literal(4.0f) - ) - ), - iconColor( - interpolate(exponential(1), get("mag"), - stop(2.0, rgb(0, 255, 0)), - stop(4.5, rgb(0, 0, 255)), - stop(7.0, rgb(255, 0, 0)) - ) - ) - ) - .withFilter(has("mag")); - } - - private CircleLayer createClusterLevelLayer(int level, int[][] layerColors) { - CircleLayer circles = new CircleLayer("cluster-" + level, "earthquakes"); - circles.setProperties( - circleColor(layerColors[level][1]), - circleRadius(18f) - ); - - Expression pointCount = toNumber(get("point_count")); - circles.setFilter( - level == 0 - ? all(has("point_count"), - gte(pointCount, literal(layerColors[level][0])) - ) : all(has("point_count"), - gt(pointCount, literal(layerColors[level][0])), - lt(pointCount, literal(layerColors[level - 1][0])) - ) - ); - return circles; - } - - private SymbolLayer createClusterTextLayer() { - return new SymbolLayer("property", "earthquakes") - .withProperties( - textField(concat(get("point_count"), literal(", "), get("max"))), - textSize(12f), - textColor(Color.WHITE), - textIgnorePlacement(true), - textAllowOverlap(true) - ); - } - - @Override - protected void onStart() { - super.onStart(); - mapView.onStart(); - } - - @Override - protected void onResume() { - super.onResume(); - mapView.onResume(); - } - - @Override - protected void onPause() { - super.onPause(); - mapView.onPause(); - } - - @Override - protected void onStop() { - super.onStop(); - mapView.onStop(); - } - - @Override - protected void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - mapView.onSaveInstanceState(outState); - } - - @Override - protected void onDestroy() { - super.onDestroy(); - mapView.onDestroy(); - } - - @Override - public void onLowMemory() { - super.onLowMemory(); - mapView.onLowMemory(); - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: - onBackPressed(); - return true; - default: - return super.onOptionsItemSelected(item); - } - } - - private void updateClickOptionCounter() { - if (clickOptionCounter == 2) { - clickOptionCounter = 0; - } else { - clickOptionCounter++; - } - } - - private void notifyClickOptionUpdate() { - if (clickOptionCounter == 0) { - Toast.makeText( - GeoJsonClusteringActivity.this, - "Clicking a cluster will zoom to the level where it dissolves", - Toast.LENGTH_SHORT).show(); - } else if (clickOptionCounter == 1) { - Toast.makeText( - GeoJsonClusteringActivity.this, - "Clicking a cluster will show the details of the cluster children", - Toast.LENGTH_SHORT).show(); - } else { - Toast.makeText( - GeoJsonClusteringActivity.this, - "Clicking a cluster will show the details of the cluster leaves with an offset and limit", - Toast.LENGTH_SHORT).show(); - } - } -} diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/GradientLineActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/GradientLineActivity.java deleted file mode 100644 index 9c7c7560ca..0000000000 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/GradientLineActivity.java +++ /dev/null @@ -1,117 +0,0 @@ -package com.mapbox.mapboxsdk.testapp.activity.style; - -import android.graphics.Color; -import android.os.Bundle; -import android.support.annotation.NonNull; -import android.support.v7.app.AppCompatActivity; -import com.mapbox.mapboxsdk.maps.MapView; -import com.mapbox.mapboxsdk.maps.MapboxMap; -import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; -import com.mapbox.mapboxsdk.maps.Style; -import com.mapbox.mapboxsdk.style.layers.LineLayer; -import com.mapbox.mapboxsdk.style.sources.GeoJsonOptions; -import com.mapbox.mapboxsdk.style.sources.GeoJsonSource; -import com.mapbox.mapboxsdk.testapp.R; -import com.mapbox.mapboxsdk.testapp.utils.ResourceUtils; -import timber.log.Timber; - -import java.io.IOException; - -import static com.mapbox.mapboxsdk.style.expressions.Expression.interpolate; -import static com.mapbox.mapboxsdk.style.expressions.Expression.lineProgress; -import static com.mapbox.mapboxsdk.style.expressions.Expression.linear; -import static com.mapbox.mapboxsdk.style.expressions.Expression.rgb; -import static com.mapbox.mapboxsdk.style.expressions.Expression.stop; -import static com.mapbox.mapboxsdk.style.layers.Property.LINE_CAP_ROUND; -import static com.mapbox.mapboxsdk.style.layers.Property.LINE_JOIN_ROUND; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineCap; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineColor; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineGradient; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineJoin; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineWidth; - -/** - * Activity showcasing applying a gradient coloring to a line layer. - */ -public class GradientLineActivity extends AppCompatActivity implements OnMapReadyCallback { - - public static final String LINE_SOURCE = "gradient"; - private MapView mapView; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_gradient_line); - - mapView = findViewById(R.id.mapView); - mapView.onCreate(savedInstanceState); - mapView.getMapAsync(this); - } - - @Override - public void onMapReady(@NonNull MapboxMap mapboxMap) { - try { - String geoJson = ResourceUtils.readRawResource(GradientLineActivity.this, R.raw.test_line_gradient_feature); - mapboxMap.setStyle(new Style.Builder() - .withSource(new GeoJsonSource(LINE_SOURCE, geoJson, new GeoJsonOptions().withLineMetrics(true))) - .withLayer(new LineLayer("gradient", LINE_SOURCE) - .withProperties( - lineGradient(interpolate( - linear(), lineProgress(), - stop(0f, rgb(0, 0, 255)), - stop(0.5f, rgb(0, 255, 0)), - stop(1f, rgb(255, 0, 0))) - ), - lineColor(Color.RED), - lineWidth(10.0f), - lineCap(LINE_CAP_ROUND), - lineJoin(LINE_JOIN_ROUND) - )) - ); - } catch (IOException exception) { - Timber.e(exception); - } - } - - @Override - protected void onStart() { - super.onStart(); - mapView.onStart(); - } - - @Override - protected void onResume() { - super.onResume(); - mapView.onResume(); - } - - @Override - protected void onPause() { - super.onPause(); - mapView.onPause(); - } - - @Override - protected void onStop() { - super.onStop(); - mapView.onStop(); - } - - @Override - public void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - mapView.onSaveInstanceState(outState); - } - - @Override - public void onLowMemory() { - super.onLowMemory(); - mapView.onLowMemory(); - } - - @Override - public void onDestroy() { - super.onDestroy(); - mapView.onDestroy(); - } -} diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/GridSourceActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/GridSourceActivity.java deleted file mode 100644 index 037437f5cc..0000000000 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/GridSourceActivity.java +++ /dev/null @@ -1,154 +0,0 @@ -package com.mapbox.mapboxsdk.testapp.activity.style; - -import android.graphics.Color; -import android.os.Bundle; -import android.support.annotation.NonNull; -import android.support.v7.app.AppCompatActivity; - -import com.mapbox.geojson.Feature; -import com.mapbox.geojson.FeatureCollection; -import com.mapbox.geojson.MultiLineString; -import com.mapbox.geojson.Point; -import com.mapbox.mapboxsdk.geometry.LatLngBounds; -import com.mapbox.mapboxsdk.maps.MapView; -import com.mapbox.mapboxsdk.maps.MapboxMap; -import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; -import com.mapbox.mapboxsdk.maps.Style; -import com.mapbox.mapboxsdk.style.layers.LineLayer; -import com.mapbox.mapboxsdk.style.sources.CustomGeometrySource; -import com.mapbox.mapboxsdk.style.sources.GeometryTileProvider; -import com.mapbox.mapboxsdk.testapp.R; - - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineColor; - -/** - * Test activity showcasing using CustomGeometrySource to create a grid overlay on the map. - */ -public class GridSourceActivity extends AppCompatActivity implements OnMapReadyCallback { - - public static final String ID_GRID_SOURCE = "grid_source"; - public static final String ID_GRID_LAYER = "grid_layer"; - - private MapView mapView; - - // public for testing purposes - public CustomGeometrySource source; - public LineLayer layer; - - /** - * Implementation of GeometryTileProvider that returns features representing a zoom-dependent - * grid. - */ - static class GridProvider implements GeometryTileProvider { - public FeatureCollection getFeaturesForBounds(LatLngBounds bounds, int zoom) { - List<Feature> features = new ArrayList<>(); - double gridSpacing; - if (zoom >= 13) { - gridSpacing = 0.01; - } else if (zoom >= 11) { - gridSpacing = 0.05; - } else if (zoom == 10) { - gridSpacing = .1; - } else if (zoom == 9) { - gridSpacing = 0.25; - } else if (zoom == 8) { - gridSpacing = 0.5; - } else if (zoom >= 6) { - gridSpacing = 1; - } else if (zoom == 5) { - gridSpacing = 2; - } else if (zoom >= 4) { - gridSpacing = 5; - } else if (zoom == 2) { - gridSpacing = 10; - } else { - gridSpacing = 20; - } - - List gridLines = new ArrayList(); - for (double y = Math.ceil(bounds.getLatNorth() / gridSpacing) * gridSpacing; - y >= Math.floor(bounds.getLatSouth() / gridSpacing) * gridSpacing; y -= gridSpacing) { - gridLines.add(Arrays.asList(Point.fromLngLat(bounds.getLonWest(), y), - Point.fromLngLat(bounds.getLonEast(), y))); - } - features.add(Feature.fromGeometry(MultiLineString.fromLngLats(gridLines))); - - gridLines = new ArrayList(); - for (double x = Math.floor(bounds.getLonWest() / gridSpacing) * gridSpacing; - x <= Math.ceil(bounds.getLonEast() / gridSpacing) * gridSpacing; x += gridSpacing) { - gridLines.add(Arrays.asList(Point.fromLngLat(x, bounds.getLatSouth()), - Point.fromLngLat(x, bounds.getLatNorth()))); - } - features.add(Feature.fromGeometry(MultiLineString.fromLngLats(gridLines))); - - return FeatureCollection.fromFeatures(features); - } - } - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_grid_source); - - mapView = findViewById(R.id.mapView); - mapView.onCreate(savedInstanceState); - mapView.getMapAsync(this); - } - - @Override - public void onMapReady(@NonNull final MapboxMap map) { - source = new CustomGeometrySource(ID_GRID_SOURCE, new GridProvider()); - layer = new LineLayer(ID_GRID_LAYER, ID_GRID_SOURCE); - layer.setProperties( - lineColor(Color.parseColor("#000000")) - ); - - map.setStyle(new Style.Builder() - .fromUri(Style.MAPBOX_STREETS) - .withLayer(layer) - .withSource(source) - ); - } - - @Override - protected void onStart() { - super.onStart(); - mapView.onStart(); - } - - @Override - public void onResume() { - super.onResume(); - mapView.onResume(); - } - - @Override - public void onPause() { - super.onPause(); - mapView.onPause(); - } - - @Override - protected void onStop() { - super.onStop(); - mapView.onStop(); - } - - @Override - protected void onDestroy() { - super.onDestroy(); - mapView.onDestroy(); - } - - @Override - protected void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - mapView.onSaveInstanceState(outState); - } - -} diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/HeatmapLayerActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/HeatmapLayerActivity.java deleted file mode 100644 index 57d68f7f0c..0000000000 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/HeatmapLayerActivity.java +++ /dev/null @@ -1,228 +0,0 @@ -package com.mapbox.mapboxsdk.testapp.activity.style; - -import android.os.Bundle; -import android.support.v7.app.AppCompatActivity; -import com.mapbox.mapboxsdk.maps.MapView; -import com.mapbox.mapboxsdk.maps.MapboxMap; -import com.mapbox.mapboxsdk.maps.Style; -import com.mapbox.mapboxsdk.style.layers.CircleLayer; -import com.mapbox.mapboxsdk.style.layers.HeatmapLayer; -import com.mapbox.mapboxsdk.style.sources.GeoJsonSource; -import com.mapbox.mapboxsdk.testapp.R; -import timber.log.Timber; - -import java.net.URI; -import java.net.URISyntaxException; - -import static com.mapbox.mapboxsdk.style.expressions.Expression.get; -import static com.mapbox.mapboxsdk.style.expressions.Expression.heatmapDensity; -import static com.mapbox.mapboxsdk.style.expressions.Expression.interpolate; -import static com.mapbox.mapboxsdk.style.expressions.Expression.linear; -import static com.mapbox.mapboxsdk.style.expressions.Expression.literal; -import static com.mapbox.mapboxsdk.style.expressions.Expression.rgb; -import static com.mapbox.mapboxsdk.style.expressions.Expression.rgba; -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.PropertyFactory.circleColor; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circleOpacity; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circleRadius; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circleStrokeColor; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circleStrokeWidth; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.heatmapColor; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.heatmapIntensity; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.heatmapOpacity; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.heatmapRadius; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.heatmapWeight; - -/** - * Test activity showcasing the heatmap layer api. - */ -public class HeatmapLayerActivity extends AppCompatActivity { - - private static final String EARTHQUAKE_SOURCE_URL = "https://www.mapbox.com/mapbox-gl-js/assets/earthquakes.geojson"; - private static final String EARTHQUAKE_SOURCE_ID = "earthquakes"; - private static final String HEATMAP_LAYER_ID = "earthquakes-heat"; - private static final String HEATMAP_LAYER_SOURCE = "earthquakes"; - private static final String CIRCLE_LAYER_ID = "earthquakes-circle"; - - private MapView mapView; - private MapboxMap mapboxMap; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_heatmaplayer); - mapView = findViewById(R.id.mapView); - mapView.onCreate(savedInstanceState); - mapView.getMapAsync(map -> { - mapboxMap = map; - - try { - mapboxMap.setStyle(new Style.Builder() - .fromUri(Style.DARK) - .withSource(createEarthquakeSource()) - .withLayerAbove(createHeatmapLayer(), "waterway-label") - .withLayerBelow(createCircleLayer(), HEATMAP_LAYER_ID) - ); - } catch (URISyntaxException exception) { - Timber.e(exception); - } - }); - } - - private GeoJsonSource createEarthquakeSource() throws URISyntaxException { - return new GeoJsonSource(EARTHQUAKE_SOURCE_ID, new URI(EARTHQUAKE_SOURCE_URL)); - } - - private HeatmapLayer createHeatmapLayer() { - HeatmapLayer layer = new HeatmapLayer(HEATMAP_LAYER_ID, EARTHQUAKE_SOURCE_ID); - layer.setMaxZoom(9); - layer.setSourceLayer(HEATMAP_LAYER_SOURCE); - layer.setProperties( - - // Color ramp for heatmap. Domain is 0 (low) to 1 (high). - // Begin color ramp at 0-stop with a 0-transparancy color - // to create a blur-like effect. - heatmapColor( - interpolate( - linear(), heatmapDensity(), - literal(0), rgba(33, 102, 172, 0), - literal(0.2), rgb(103, 169, 207), - literal(0.4), rgb(209, 229, 240), - literal(0.6), rgb(253, 219, 199), - literal(0.8), rgb(239, 138, 98), - literal(1), rgb(178, 24, 43) - ) - ), - - // Increase the heatmap weight based on frequency and property magnitude - heatmapWeight( - interpolate( - linear(), get("mag"), - stop(0, 0), - stop(6, 1) - ) - ), - - // Increase the heatmap color weight weight by zoom level - // heatmap-intensity is a multiplier on top of heatmap-weight - heatmapIntensity( - interpolate( - linear(), zoom(), - stop(0, 1), - stop(9, 3) - ) - ), - - // Adjust the heatmap radius by zoom level - heatmapRadius( - interpolate( - linear(), zoom(), - stop(0, 2), - stop(9, 20) - ) - ), - - // Transition from heatmap to circle layer by zoom level - heatmapOpacity( - interpolate( - linear(), zoom(), - stop(7, 1), - stop(9, 0) - ) - ) - ); - return layer; - } - - private CircleLayer createCircleLayer() { - CircleLayer circleLayer = new CircleLayer(CIRCLE_LAYER_ID, EARTHQUAKE_SOURCE_ID); - circleLayer.setProperties( - - // Size circle radius by earthquake magnitude and zoom level - circleRadius( - interpolate( - linear(), zoom(), - literal(7), interpolate( - linear(), get("mag"), - stop(1, 1), - stop(6, 4) - ), - literal(16), interpolate( - linear(), get("mag"), - stop(1, 5), - stop(6, 50) - ) - ) - ), - - // Color circle by earthquake magnitude - circleColor( - interpolate( - linear(), get("mag"), - literal(1), rgba(33, 102, 172, 0), - literal(2), rgb(103, 169, 207), - literal(3), rgb(209, 229, 240), - literal(4), rgb(253, 219, 199), - literal(5), rgb(239, 138, 98), - literal(6), rgb(178, 24, 43) - ) - ), - - // Transition from heatmap to circle layer by zoom level - circleOpacity( - interpolate( - linear(), zoom(), - stop(7, 0), - stop(8, 1) - ) - ), - circleStrokeColor("white"), - circleStrokeWidth(1.0f) - ); - - return circleLayer; - } - - @Override - protected void onStart() { - super.onStart(); - mapView.onStart(); - } - - @Override - protected void onResume() { - super.onResume(); - mapView.onResume(); - } - - @Override - protected void onPause() { - super.onPause(); - mapView.onPause(); - } - - @Override - protected void onStop() { - super.onStop(); - mapView.onStop(); - } - - @Override - public void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - mapView.onSaveInstanceState(outState); - } - - @Override - public void onLowMemory() { - super.onLowMemory(); - mapView.onLowMemory(); - } - - @Override - public void onDestroy() { - super.onDestroy(); - mapView.onDestroy(); - } -} diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/HillshadeLayerActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/HillshadeLayerActivity.java deleted file mode 100644 index 0ed3a5d130..0000000000 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/HillshadeLayerActivity.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.mapbox.mapboxsdk.testapp.activity.style; - -import android.os.Bundle; -import android.support.v7.app.AppCompatActivity; - -import com.mapbox.mapboxsdk.maps.MapView; -import com.mapbox.mapboxsdk.maps.MapboxMap; -import com.mapbox.mapboxsdk.maps.Style; -import com.mapbox.mapboxsdk.style.layers.HillshadeLayer; -import com.mapbox.mapboxsdk.style.sources.RasterDemSource; -import com.mapbox.mapboxsdk.testapp.R; - -/** - * Test activity showcasing using HillshadeLayer. - */ -public class HillshadeLayerActivity extends AppCompatActivity { - - private static final String LAYER_ID = "hillshade-layer"; - private static final String LAYER_BELOW_ID = "waterway-river-canal"; - private static final String SOURCE_ID = "hillshade-source"; - private static final String SOURCE_URL = "mapbox://mapbox.terrain-rgb"; - - private MapView mapView; - private MapboxMap mapboxMap; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_fill_extrusion_layer); - - mapView = findViewById(R.id.mapView); - mapView.onCreate(savedInstanceState); - mapView.getMapAsync(map -> { - mapboxMap = map; - - RasterDemSource rasterDemSource = new RasterDemSource(SOURCE_ID, SOURCE_URL); - HillshadeLayer hillshadeLayer = new HillshadeLayer(LAYER_ID, SOURCE_ID); - mapboxMap.setStyle(new Style.Builder() - .fromUri(Style.MAPBOX_STREETS) - .withLayerBelow(hillshadeLayer, LAYER_BELOW_ID) - .withSource(rasterDemSource) - ); - }); - } - - @Override - protected void onStart() { - super.onStart(); - mapView.onStart(); - } - - @Override - protected void onResume() { - super.onResume(); - mapView.onResume(); - } - - @Override - protected void onPause() { - super.onPause(); - mapView.onPause(); - } - - @Override - protected void onStop() { - super.onStop(); - mapView.onStop(); - } - - @Override - public void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - mapView.onSaveInstanceState(outState); - } - - @Override - public void onLowMemory() { - super.onLowMemory(); - mapView.onLowMemory(); - } - - @Override - public void onDestroy() { - super.onDestroy(); - mapView.onDestroy(); - } -}
\ No newline at end of file diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/NoStyleActivity.kt b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/NoStyleActivity.kt deleted file mode 100644 index ebc59e6536..0000000000 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/NoStyleActivity.kt +++ /dev/null @@ -1,84 +0,0 @@ -package com.mapbox.mapboxsdk.testapp.activity.style - -import android.graphics.drawable.Drawable -import android.os.Bundle -import android.support.v4.content.res.ResourcesCompat -import android.support.v7.app.AppCompatActivity -import com.mapbox.mapboxsdk.camera.CameraUpdateFactory -import com.mapbox.mapboxsdk.geometry.LatLng -import com.mapbox.mapboxsdk.maps.Style -import com.mapbox.mapboxsdk.style.layers.PropertyFactory.* -import com.mapbox.mapboxsdk.style.layers.SymbolLayer -import com.mapbox.mapboxsdk.style.sources.GeoJsonSource -import com.mapbox.mapboxsdk.testapp.R -import java.net.URI -import kotlinx.android.synthetic.main.activity_map_simple.* - -/** - * Activity showcasing how to load symbols on a map without a Style URI or Style JSON. - */ -class NoStyleActivity : AppCompatActivity() { - - private val imageIcon: Drawable by lazy { - ResourcesCompat.getDrawable(resources, R.drawable.ic_add_white, theme)!! - } - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - setContentView(R.layout.activity_map_simple) - mapView.getMapAsync { map -> - map.moveCamera(CameraUpdateFactory.newLatLngZoom(cameraTarget, cameraZoom)) - map.setStyle( - Style.Builder() - .withImage(imageId, imageIcon) - .withSource(GeoJsonSource(sourceId, URI("asset://points-sf.geojson"))) - .withLayer(SymbolLayer(layerId, sourceId).withProperties(iconImage(imageId))) - ) - } - } - - override fun onStart() { - super.onStart() - mapView.onStart() - } - - override fun onResume() { - super.onResume() - mapView.onResume() - } - - override fun onPause() { - super.onPause() - mapView.onPause() - } - - override fun onStop() { - super.onStop() - mapView.onStop() - } - - override fun onLowMemory() { - super.onLowMemory() - mapView.onLowMemory() - } - - override fun onDestroy() { - super.onDestroy() - mapView.onDestroy() - } - - override fun onSaveInstanceState(outState: Bundle?) { - super.onSaveInstanceState(outState) - outState?.let { - mapView.onSaveInstanceState(it) - } - } - - companion object { - const val layerId = "custom-layer-id" - const val sourceId = "custom-source-id" - const val imageId = "image-id" - const val cameraZoom = 10.0 - val cameraTarget = LatLng(37.758912, -122.442578) - } -}
\ No newline at end of file diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/RealTimeGeoJsonActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/RealTimeGeoJsonActivity.java deleted file mode 100644 index 4802787ab1..0000000000 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/RealTimeGeoJsonActivity.java +++ /dev/null @@ -1,129 +0,0 @@ -package com.mapbox.mapboxsdk.testapp.activity.style; - -import android.os.Bundle; -import android.os.Handler; -import android.support.annotation.NonNull; -import android.support.v7.app.AppCompatActivity; -import com.mapbox.mapboxsdk.maps.MapView; -import com.mapbox.mapboxsdk.maps.MapboxMap; -import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; -import com.mapbox.mapboxsdk.maps.Style; -import com.mapbox.mapboxsdk.style.layers.SymbolLayer; -import com.mapbox.mapboxsdk.style.sources.GeoJsonSource; -import com.mapbox.mapboxsdk.testapp.R; -import timber.log.Timber; - -import java.net.URI; -import java.net.URISyntaxException; - -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconImage; - -/** - * Test activity showcasing using realtime GeoJSON to move a symbol on your map - * <p> - * GL-native equivalent of https://www.mapbox.com/mapbox-gl-js/example/live-geojson/ - * </p> - */ -public class RealTimeGeoJsonActivity extends AppCompatActivity implements OnMapReadyCallback { - - private static final String ID_GEOJSON_LAYER = "wanderdrone"; - private static final String ID_GEOJSON_SOURCE = ID_GEOJSON_LAYER; - private static final String URL_GEOJSON_SOURCE = "https://wanderdrone.appspot.com/"; - - private MapView mapView; - private MapboxMap mapboxMap; - - private Handler handler; - private Runnable runnable; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_default); - - mapView = (MapView) findViewById(R.id.mapView); - mapView.onCreate(savedInstanceState); - mapView.getMapAsync(this); - } - - @Override - public void onMapReady(@NonNull final MapboxMap map) { - mapboxMap = map; - - mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() { - @Override - public void onStyleLoaded(@NonNull Style style) { - // add source - try { - style.addSource(new GeoJsonSource(ID_GEOJSON_SOURCE, new URI(URL_GEOJSON_SOURCE))); - } catch (URISyntaxException malformedUriException) { - Timber.e(malformedUriException, "Invalid URL"); - } - - // add layer - SymbolLayer layer = new SymbolLayer(ID_GEOJSON_LAYER, ID_GEOJSON_SOURCE); - layer.setProperties(iconImage("rocket-15")); - style.addLayer(layer); - - // loop refresh geojson - handler = new Handler(); - runnable = new RefreshGeoJsonRunnable(mapboxMap, handler); - handler.postDelayed(runnable, 2000); - } - }); - } - - @Override - protected void onStart() { - super.onStart(); - mapView.onStart(); - } - - @Override - public void onResume() { - super.onResume(); - mapView.onResume(); - } - - @Override - public void onPause() { - super.onPause(); - mapView.onPause(); - } - - @Override - protected void onStop() { - super.onStop(); - mapView.onStop(); - handler.removeCallbacks(runnable); - } - - @Override - protected void onDestroy() { - super.onDestroy(); - mapView.onDestroy(); - } - - @Override - protected void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - mapView.onSaveInstanceState(outState); - } - - private static class RefreshGeoJsonRunnable implements Runnable { - - private MapboxMap mapboxMap; - private Handler handler; - - RefreshGeoJsonRunnable(MapboxMap mapboxMap, Handler handler) { - this.mapboxMap = mapboxMap; - this.handler = handler; - } - - @Override - public void run() { - ((GeoJsonSource) mapboxMap.getStyle().getSource(ID_GEOJSON_SOURCE)).setUrl(URL_GEOJSON_SOURCE); - handler.postDelayed(this, 2000); - } - } -} 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 deleted file mode 100644 index 09bb00b84d..0000000000 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/RuntimeStyleActivity.java +++ /dev/null @@ -1,607 +0,0 @@ -package com.mapbox.mapboxsdk.testapp.activity.style; - -import android.graphics.Color; -import android.os.Bundle; -import android.os.Handler; -import android.support.v7.app.AppCompatActivity; -import android.view.Menu; -import android.view.MenuItem; -import android.widget.Toast; -import com.mapbox.geojson.Feature; -import com.mapbox.geojson.FeatureCollection; -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.Style; -import com.mapbox.mapboxsdk.style.layers.CircleLayer; -import com.mapbox.mapboxsdk.style.layers.FillLayer; -import com.mapbox.mapboxsdk.style.layers.Layer; -import com.mapbox.mapboxsdk.style.layers.LineLayer; -import com.mapbox.mapboxsdk.style.layers.Property; -import com.mapbox.mapboxsdk.style.layers.PropertyValue; -import com.mapbox.mapboxsdk.style.layers.RasterLayer; -import com.mapbox.mapboxsdk.style.layers.SymbolLayer; -import com.mapbox.mapboxsdk.style.layers.TransitionOptions; -import com.mapbox.mapboxsdk.style.sources.GeoJsonSource; -import com.mapbox.mapboxsdk.style.sources.RasterSource; -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 timber.log.Timber; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -import static com.mapbox.mapboxsdk.style.expressions.Expression.all; -import static com.mapbox.mapboxsdk.style.expressions.Expression.color; -import static com.mapbox.mapboxsdk.style.expressions.Expression.eq; -import static com.mapbox.mapboxsdk.style.expressions.Expression.exponential; -import static com.mapbox.mapboxsdk.style.expressions.Expression.get; -import static com.mapbox.mapboxsdk.style.expressions.Expression.gte; -import static com.mapbox.mapboxsdk.style.expressions.Expression.interpolate; -import static com.mapbox.mapboxsdk.style.expressions.Expression.literal; -import static com.mapbox.mapboxsdk.style.expressions.Expression.lt; -import static com.mapbox.mapboxsdk.style.expressions.Expression.stop; -import static com.mapbox.mapboxsdk.style.expressions.Expression.toNumber; -import static com.mapbox.mapboxsdk.style.expressions.Expression.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; -import static com.mapbox.mapboxsdk.style.layers.Property.VISIBLE; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.backgroundOpacity; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillAntialias; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillColor; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillOpacity; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillOutlineColor; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillTranslateAnchor; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineCap; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineColor; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineJoin; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineOpacity; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineWidth; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.symbolPlacement; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.visibility; - -/** - * Test activity showcasing the runtime style API. - */ -public class RuntimeStyleActivity extends AppCompatActivity { - - private MapView mapView; - private MapboxMap mapboxMap; - private boolean styleLoaded; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_runtime_style); - - // Initialize map as normal - mapView = findViewById(R.id.mapView); - mapView.onCreate(savedInstanceState); - - 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)); - - mapboxMap.setStyle( - new Style.Builder() - .fromUri(Style.MAPBOX_STREETS) - // set custom transition - .withTransition(new TransitionOptions(250, 50)), style -> styleLoaded = true - ); - }); - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.menu_runtime_style, menu); - return true; - } - - @Override - protected void onStart() { - super.onStart(); - mapView.onStart(); - } - - @Override - protected void onResume() { - super.onResume(); - mapView.onResume(); - } - - @Override - protected void onPause() { - super.onPause(); - mapView.onPause(); - } - - @Override - protected void onStop() { - super.onStop(); - mapView.onStop(); - } - - @Override - protected void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - mapView.onSaveInstanceState(outState); - } - - @Override - protected void onDestroy() { - super.onDestroy(); - mapView.onDestroy(); - } - - @Override - public void onLowMemory() { - super.onLowMemory(); - mapView.onLowMemory(); - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - if (!styleLoaded) { - return false; - } - - switch (item.getItemId()) { - 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(); - return true; - case R.id.action_background_opacity: - setBackgroundOpacity(); - return true; - case R.id.action_road_avoid_edges: - setRoadSymbolPlacement(); - return true; - case R.id.action_layer_visibility: - setLayerInvisible(); - return true; - case R.id.action_remove_layer: - removeBuildings(); - return true; - case R.id.action_add_parks_layer: - addParksLayer(); - return true; - case R.id.action_add_dynamic_parks_layer: - addDynamicParksLayer(); - return true; - case R.id.action_add_terrain_layer: - addTerrainLayer(); - return true; - case R.id.action_add_satellite_layer: - addSatelliteLayer(); - return true; - case R.id.action_update_water_color_on_zoom: - updateWaterColorOnZoom(); - return true; - case R.id.action_add_custom_tiles: - addCustomTileSource(); - return true; - case R.id.action_fill_filter: - styleFillFilterLayer(); - return true; - case R.id.action_line_filter: - styleLineFilterLayer(); - return true; - case R.id.action_numeric_filter: - styleNumericFillLayer(); - return true; - case R.id.action_bring_water_to_front: - bringWaterToFront(); - return true; - default: - return super.onOptionsItemSelected(item); - } - } - - private void listLayers() { - List<Layer> layers = mapboxMap.getStyle().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.getStyle().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) { - Layer layer = mapboxMap.getStyle().getLayer(roadLayer); - if (layer != null) { - layer.setProperties(visibility(NONE)); - } - } - } - - private void setRoadSymbolPlacement() { - // Zoom so that the labels are visible first - mapboxMap.animateCamera(CameraUpdateFactory.zoomTo(14), new DefaultCallback() { - @Override - public void onFinish() { - String[] roadLayers = new String[] {"road-label-small", "road-label-medium", "road-label-large"}; - for (String roadLayer : roadLayers) { - Layer layer = mapboxMap.getStyle().getLayer(roadLayer); - if (layer != null) { - layer.setProperties(symbolPlacement(SYMBOL_PLACEMENT_POINT)); - } - } - } - }); - } - - private void setBackgroundOpacity() { - Layer background = mapboxMap.getStyle().getLayer("background"); - if (background != null) { - background.setProperties(backgroundOpacity(0.2f)); - } - } - - private void setWaterColor() { - FillLayer water = mapboxMap.getStyle().getLayerAs("water"); - if (water != null) { - water.setFillColorTransition(new TransitionOptions(7500, 1000)); - water.setProperties( - visibility(VISIBLE), - fillColor(Color.RED) - ); - } else { - Toast.makeText(RuntimeStyleActivity.this, "No water layer in this style", Toast.LENGTH_SHORT).show(); - } - } - - private void removeBuildings() { - // Zoom to see buildings first - mapboxMap.getStyle().removeLayer("building"); - } - - private void addParksLayer() { - // Add a source - Source source; - try { - source = new GeoJsonSource("amsterdam-spots", ResourceUtils.readRawResource(this, R.raw.amsterdam)); - } catch (IOException ioException) { - Toast.makeText( - RuntimeStyleActivity.this, - "Couldn't add source: " + ioException.getMessage(), - Toast.LENGTH_SHORT).show(); - return; - } - - mapboxMap.getStyle().addSource(source); - - FillLayer layer = new FillLayer("parksLayer", "amsterdam-spots"); - layer.setProperties( - fillColor(Color.RED), - fillOutlineColor(Color.BLUE), - fillOpacity(0.3f), - fillAntialias(true) - ); - - // Only show me parks (except westerpark with stroke-width == 3) - layer.setFilter(all(eq(get("type"), literal("park")), eq(get("stroke-width"), literal(3)))); - - mapboxMap.getStyle().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. - mapboxMap.getStyle().getLayer("parksLayer").setProperties(fillColor(Color.RED)); - - // You can get a typed layer, if you're sure it's of that type. Use with care - layer = mapboxMap.getStyle().getLayerAs("parksLayer"); - // And get some properties - PropertyValue<Boolean> fillAntialias = layer.getFillAntialias(); - 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: %s", fillTranslateAnchor.getValue()); - PropertyValue<String> visibility = layer.getVisibility(); - Timber.d("Visibility: %s", visibility.getValue()); - - // Get a good look at it all - mapboxMap.animateCamera(CameraUpdateFactory.zoomTo(12)); - } - - private void addDynamicParksLayer() { - // Load some data - FeatureCollection parks; - try { - String json = ResourceUtils.readRawResource(this, R.raw.amsterdam); - parks = FeatureCollection.fromJson(json); - } catch (IOException ioException) { - Toast.makeText( - RuntimeStyleActivity.this, - "Couldn't add source: " + ioException.getMessage(), - Toast.LENGTH_SHORT - ).show(); - return; - } - - // Add an empty source - mapboxMap.getStyle().addSource(new GeoJsonSource("dynamic-park-source")); - - FillLayer layer = new FillLayer("dynamic-parks-layer", "dynamic-park-source"); - layer.setProperties( - fillColor(Color.GREEN), - fillOutlineColor(Color.GREEN), - fillOpacity(0.8f), - fillAntialias(true) - ); - - // Only show me parks - layer.setFilter(all(eq(get("type"), literal("park")))); - - mapboxMap.getStyle().addLayer(layer); - - // Get a good look at it all - mapboxMap.animateCamera(CameraUpdateFactory.zoomTo(12)); - - // Animate the parks source - animateParksSource(parks, 0); - } - - private void animateParksSource(final FeatureCollection parks, final int counter) { - Handler handler = new Handler(getMainLooper()); - handler.postDelayed(() -> { - if (mapboxMap == null) { - return; - } - - Timber.d("Updating parks source"); - // change the source - int park = counter < parks.features().size() - 1 ? counter : 0; - - GeoJsonSource source = mapboxMap.getStyle().getSourceAs("dynamic-park-source"); - - 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.features().get(park)); - source.setGeoJson(FeatureCollection.fromFeatures(features)); - - // Re-post - animateParksSource(parks, park + 1); - }, counter == 0 ? 100 : 1000); - } - - private void addTerrainLayer() { - // Add a source - Source source = new VectorSource("my-terrain-source", "mapbox://mapbox.mapbox-terrain-v2"); - mapboxMap.getStyle().addSource(source); - - LineLayer layer = new LineLayer("terrainLayer", "my-terrain-source"); - layer.setSourceLayer("contour"); - layer.setProperties( - lineJoin(Property.LINE_JOIN_ROUND), - lineCap(Property.LINE_CAP_ROUND), - lineColor(Color.RED), - lineWidth(20f) - ); - - // adding layers below "road" layers - List<Layer> layers = mapboxMap.getStyle().getLayers(); - Layer latestLayer = null; - Collections.reverse(layers); - for (Layer currentLayer : layers) { - if (currentLayer instanceof FillLayer && ((FillLayer) currentLayer).getSourceLayer().equals("road")) { - latestLayer = currentLayer; - } else if (currentLayer instanceof CircleLayer && ((CircleLayer) currentLayer).getSourceLayer().equals("road")) { - latestLayer = currentLayer; - } else if (currentLayer instanceof SymbolLayer && ((SymbolLayer) currentLayer).getSourceLayer().equals("road")) { - latestLayer = currentLayer; - } else if (currentLayer instanceof LineLayer && ((LineLayer) currentLayer).getSourceLayer().equals("road")) { - latestLayer = currentLayer; - } - } - - if (latestLayer != null) { - mapboxMap.getStyle().addLayerBelow(layer, latestLayer.getId()); - } - - // Need to get a fresh handle - layer = mapboxMap.getStyle().getLayerAs("terrainLayer"); - - // Make sure it's also applied after the fact - layer.setMinZoom(10); - layer.setMaxZoom(15); - - layer = (LineLayer) mapboxMap.getStyle().getLayer("terrainLayer"); - Toast.makeText(this, String.format( - "Set min/max zoom to %s - %s", layer.getMinZoom(), layer.getMaxZoom()), Toast.LENGTH_SHORT).show(); - } - - private void addSatelliteLayer() { - // Add a source - Source source = new RasterSource("my-raster-source", "mapbox://mapbox.satellite", 512); - mapboxMap.getStyle().addSource(source); - - // Add a layer - mapboxMap.getStyle().addLayer(new RasterLayer("satellite-layer", "my-raster-source")); - } - - private void updateWaterColorOnZoom() { - FillLayer layer = mapboxMap.getStyle().getLayerAs("water"); - if (layer == null) { - return; - } - - // Set a zoom function to update the color of the water - 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); - } - - private void addCustomTileSource() { - // Add a source - TileSet tileSet = new TileSet("2.1.0", "https://d25uarhxywzl1j.cloudfront.net/v0.1/{z}/{x}/{y}.mvt"); - tileSet.setMinZoom(0); - tileSet.setMaxZoom(14); - Source source = new VectorSource("custom-tile-source", tileSet); - mapboxMap.getStyle().addSource(source); - - // Add a layer - LineLayer lineLayer = new LineLayer("custom-tile-layers", "custom-tile-source"); - lineLayer.setSourceLayer("mapillary-sequences"); - lineLayer.setProperties( - lineCap(Property.LINE_CAP_ROUND), - lineJoin(Property.LINE_JOIN_ROUND), - lineOpacity(0.6f), - lineWidth(2.0f), - lineColor(Color.GREEN) - ); - mapboxMap.getStyle().addLayer(lineLayer); - } - - private void styleFillFilterLayer() { - mapboxMap.setStyle(new Style.Builder().fromUri("asset://fill_filter_style.json")); - mapboxMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(31, -100), 3)); - - Handler handler = new Handler(getMainLooper()); - handler.postDelayed(() -> { - if (mapboxMap == null) { - return; - } - - Timber.d("Styling filtered fill layer"); - - FillLayer states = (FillLayer) mapboxMap.getStyle().getLayer("states"); - - if (states != null) { - states.setFilter(eq(get("name"), literal("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); - } - - private void styleLineFilterLayer() { - mapboxMap.setStyle(new Style.Builder().fromUri("asset://line_filter_style.json")); - mapboxMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(40, -97), 5)); - - Handler handler = new Handler(getMainLooper()); - handler.postDelayed(() -> { - if (mapboxMap == null) { - return; - } - - Timber.d("Styling filtered line layer"); - - LineLayer counties = (LineLayer) mapboxMap.getStyle().getLayer("counties"); - - if (counties != null) { - counties.setFilter(eq(get("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(); - } - }, 2000); - } - - private void styleNumericFillLayer() { - mapboxMap.setStyle(new Style.Builder().fromUri("asset://numeric_filter_style.json")); - mapboxMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(40, -97), 5)); - - Handler handler = new Handler(getMainLooper()); - handler.postDelayed(() -> { - if (mapboxMap == null) { - return; - } - - Timber.d("Styling numeric fill layer"); - - FillLayer regions = (FillLayer) mapboxMap.getStyle().getLayer("regions"); - - if (regions != null) { - regions.setFilter(all( - gte(toNumber(get("HRRNUM")), literal(200)), - lt(toNumber(get("HRRNUM")), literal(300))) - ); - - regions.setProperties( - fillColor(Color.BLUE), - fillOpacity(0.5f) - ); - } else { - Toast.makeText(RuntimeStyleActivity.this, "No regions layer in this style", Toast.LENGTH_SHORT).show(); - } - }, 2000); - } - - private void bringWaterToFront() { - Layer water = mapboxMap.getStyle().getLayer("water"); - if (water != null) { - mapboxMap.getStyle().removeLayer(water); - mapboxMap.getStyle().addLayerAt(water, mapboxMap.getStyle().getLayers().size() - 1); - } else { - Toast.makeText(this, "No water layer in this style", Toast.LENGTH_SHORT).show(); - } - } - - private static class DefaultCallback implements MapboxMap.CancelableCallback { - - @Override - public void onCancel() { - // noop - } - - @Override - public void onFinish() { - // noop - } - } -} diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/RuntimeStyleTimingTestActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/RuntimeStyleTimingTestActivity.java deleted file mode 100644 index 375e236669..0000000000 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/RuntimeStyleTimingTestActivity.java +++ /dev/null @@ -1,99 +0,0 @@ -package com.mapbox.mapboxsdk.testapp.activity.style; - -import android.graphics.Color; -import android.os.Bundle; -import android.support.v7.app.AppCompatActivity; - -import com.mapbox.mapboxsdk.maps.MapView; -import com.mapbox.mapboxsdk.maps.MapboxMap; -import com.mapbox.mapboxsdk.maps.Style; -import com.mapbox.mapboxsdk.style.layers.CircleLayer; -import com.mapbox.mapboxsdk.style.sources.VectorSource; -import com.mapbox.mapboxsdk.testapp.R; - -import static com.mapbox.mapboxsdk.style.layers.Property.VISIBLE; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circleColor; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circleRadius; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.visibility; - -/** - * Test activity for unit test execution - */ -public class RuntimeStyleTimingTestActivity extends AppCompatActivity { - - public MapView mapView; - private MapboxMap mapboxMap; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_runtime_style); - - // Initialize map as normal - mapView = (MapView) findViewById(R.id.mapView); - mapView.onCreate(savedInstanceState); - mapView.getMapAsync(mapboxMap -> { - RuntimeStyleTimingTestActivity.this.mapboxMap = mapboxMap; - - CircleLayer museumsLayer = new CircleLayer("museums", "museums_source"); - museumsLayer.setSourceLayer("museum-cusco"); - museumsLayer.setProperties( - visibility(VISIBLE), - circleRadius(8f), - circleColor(Color.argb(1, 55, 148, 179)) - ); - - VectorSource museums = new VectorSource("museums_source", "mapbox://mapbox.2opop9hr"); - mapboxMap.setStyle(new Style.Builder() - .fromUri(Style.MAPBOX_STREETS) - .withSource(museums) - .withLayer(museumsLayer)); - }); - } - - public MapboxMap getMapboxMap() { - return mapboxMap; - } - - @Override - protected void onStart() { - super.onStart(); - mapView.onStart(); - } - - @Override - protected void onResume() { - super.onResume(); - mapView.onResume(); - } - - @Override - protected void onPause() { - super.onPause(); - mapView.onPause(); - } - - @Override - protected void onStop() { - super.onStop(); - mapView.onStop(); - } - - @Override - protected void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - mapView.onSaveInstanceState(outState); - } - - @Override - protected void onDestroy() { - super.onDestroy(); - mapView.onDestroy(); - } - - @Override - public void onLowMemory() { - super.onLowMemory(); - mapView.onLowMemory(); - } -} diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/StyleFileActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/StyleFileActivity.java deleted file mode 100644 index 5e3ed12489..0000000000 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/StyleFileActivity.java +++ /dev/null @@ -1,177 +0,0 @@ -package com.mapbox.mapboxsdk.testapp.activity.style; - -import android.content.Context; -import android.os.AsyncTask; -import android.os.Bundle; -import android.support.design.widget.FloatingActionButton; -import android.support.v4.content.ContextCompat; -import android.support.v7.app.AppCompatActivity; -import android.widget.Toast; -import com.mapbox.mapboxsdk.maps.MapView; -import com.mapbox.mapboxsdk.maps.MapboxMap; -import com.mapbox.mapboxsdk.maps.Style; -import com.mapbox.mapboxsdk.testapp.R; -import com.mapbox.mapboxsdk.testapp.utils.ResourceUtils; -import timber.log.Timber; - -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.lang.ref.WeakReference; - -/** - * Test activity showcasing how to use a file:// resource for the style.json and how to use MapboxMap#setStyleJson. - */ -public class StyleFileActivity extends AppCompatActivity { - - private MapboxMap mapboxMap; - private MapView mapView; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_style_file); - - mapView = findViewById(R.id.mapView); - mapView.onCreate(savedInstanceState); - mapView.getMapAsync(map -> { - mapboxMap = map; - mapboxMap.setStyle(Style.MAPBOX_STREETS, style -> { - FloatingActionButton fab = findViewById(R.id.fab_file); - fab.setColorFilter(ContextCompat.getColor(StyleFileActivity.this, R.color.primary)); - fab.setOnClickListener(view -> new CreateStyleFileTask(view.getContext(), mapboxMap).execute()); - - FloatingActionButton fabStyleJson = findViewById(R.id.fab_style_json); - fabStyleJson.setColorFilter(ContextCompat.getColor(StyleFileActivity.this, R.color.primary)); - fabStyleJson.setOnClickListener(view -> new LoadStyleFileTask(view.getContext(), mapboxMap).execute()); - }); - }); - } - - /** - * Task to read a style file from the raw folder - */ - private static class LoadStyleFileTask extends AsyncTask<Void, Void, String> { - private WeakReference<Context> context; - private WeakReference<MapboxMap> mapboxMap; - - LoadStyleFileTask(Context context, MapboxMap mapboxMap) { - this.context = new WeakReference<>(context); - this.mapboxMap = new WeakReference<>(mapboxMap); - } - - @Override - protected String doInBackground(Void... voids) { - String styleJson = ""; - try { - styleJson = ResourceUtils.readRawResource(context.get(), R.raw.sat_style); - } catch (Exception exception) { - Timber.e(exception, "Can't load local file style"); - } - return styleJson; - } - - @Override - protected void onPostExecute(String json) { - super.onPostExecute(json); - Timber.d("Read json, %s", json); - MapboxMap mapboxMap = this.mapboxMap.get(); - if (mapboxMap != null) { - mapboxMap.setStyle(new Style.Builder().fromJson(json)); - } - } - } - - /** - * Task to write a style file to local disk and load it in the map view - */ - private static class CreateStyleFileTask extends AsyncTask<Void, Integer, Long> { - private File cacheStyleFile; - private WeakReference<Context> context; - private WeakReference<MapboxMap> mapboxMap; - - CreateStyleFileTask(Context context, MapboxMap mapboxMap) { - this.context = new WeakReference<>(context); - this.mapboxMap = new WeakReference<>(mapboxMap); - } - - @Override - protected Long doInBackground(Void... params) { - try { - cacheStyleFile = File.createTempFile("my-", ".style.json"); - cacheStyleFile.createNewFile(); - Timber.i("Writing style file to: %s", cacheStyleFile.getAbsolutePath()); - Context context = this.context.get(); - if (context != null) { - writeToFile(cacheStyleFile, ResourceUtils.readRawResource(context, R.raw.local_style)); - } - } catch (Exception exception) { - Toast.makeText(context.get(), "Could not create style file in cache dir", Toast.LENGTH_SHORT).show(); - } - return 1L; - } - - protected void onPostExecute(Long result) { - // Actual file:// usage - MapboxMap mapboxMap = this.mapboxMap.get(); - if (mapboxMap != null) { - mapboxMap.setStyle(new Style.Builder().fromUri("file://" + cacheStyleFile.getAbsolutePath())); - } - } - - private void writeToFile(File file, String contents) throws IOException { - BufferedWriter writer = null; - try { - writer = new BufferedWriter(new FileWriter(file)); - writer.write(contents); - } finally { - if (writer != null) { - writer.close(); - } - } - } - } - - @Override - protected void onStart() { - super.onStart(); - mapView.onStart(); - } - - @Override - protected void onResume() { - super.onResume(); - mapView.onResume(); - } - - @Override - protected void onPause() { - super.onPause(); - mapView.onPause(); - } - - @Override - protected void onStop() { - super.onStop(); - mapView.onStop(); - } - - @Override - public void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - mapView.onSaveInstanceState(outState); - } - - @Override - public void onLowMemory() { - super.onLowMemory(); - mapView.onLowMemory(); - } - - @Override - public void onDestroy() { - super.onDestroy(); - mapView.onDestroy(); - } -} diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/SymbolGeneratorActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/SymbolGeneratorActivity.java deleted file mode 100644 index e80d6fe3e1..0000000000 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/SymbolGeneratorActivity.java +++ /dev/null @@ -1,373 +0,0 @@ -package com.mapbox.mapboxsdk.testapp.activity.style; - -import android.content.Context; -import android.graphics.Bitmap; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.PointF; -import android.os.AsyncTask; -import android.os.Bundle; -import android.support.annotation.NonNull; -import android.support.v7.app.AppCompatActivity; -import android.view.Menu; -import android.view.MenuItem; -import android.view.View; -import android.widget.TextView; -import android.widget.Toast; - -import com.mapbox.geojson.Feature; -import com.mapbox.geojson.FeatureCollection; -import com.mapbox.mapboxsdk.maps.MapView; -import com.mapbox.mapboxsdk.maps.MapboxMap; -import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; -import com.mapbox.mapboxsdk.maps.Style; -import com.mapbox.mapboxsdk.style.expressions.Expression; -import com.mapbox.mapboxsdk.style.layers.SymbolLayer; -import com.mapbox.mapboxsdk.style.sources.GeoJsonSource; -import com.mapbox.mapboxsdk.style.sources.Source; -import com.mapbox.mapboxsdk.testapp.R; -import com.mapbox.mapboxsdk.testapp.utils.ResourceUtils; - -import java.io.IOException; -import java.lang.ref.WeakReference; -import java.util.HashMap; -import java.util.List; - -import timber.log.Timber; - -import static com.mapbox.mapboxsdk.style.expressions.Expression.concat; -import static com.mapbox.mapboxsdk.style.expressions.Expression.division; -import static com.mapbox.mapboxsdk.style.expressions.Expression.downcase; -import static com.mapbox.mapboxsdk.style.expressions.Expression.eq; -import static com.mapbox.mapboxsdk.style.expressions.Expression.get; -import static com.mapbox.mapboxsdk.style.expressions.Expression.literal; -import static com.mapbox.mapboxsdk.style.expressions.Expression.match; -import static com.mapbox.mapboxsdk.style.expressions.Expression.number; -import static com.mapbox.mapboxsdk.style.expressions.Expression.pi; -import static com.mapbox.mapboxsdk.style.expressions.Expression.product; -import static com.mapbox.mapboxsdk.style.expressions.Expression.rgba; -import static com.mapbox.mapboxsdk.style.expressions.Expression.step; -import static com.mapbox.mapboxsdk.style.expressions.Expression.stop; -import static com.mapbox.mapboxsdk.style.expressions.Expression.string; -import static com.mapbox.mapboxsdk.style.expressions.Expression.upcase; -import static com.mapbox.mapboxsdk.style.expressions.Expression.zoom; -import static com.mapbox.mapboxsdk.style.layers.Property.ICON_ANCHOR_BOTTOM; -import static com.mapbox.mapboxsdk.style.layers.Property.TEXT_ANCHOR_TOP; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconAllowOverlap; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconAnchor; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconImage; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconOffset; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconOpacity; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconSize; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textAnchor; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textColor; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textField; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textSize; - -/** - * Test activity showcasing using a symbol generator that generates Bitmaps from Android SDK Views. - */ -public class SymbolGeneratorActivity extends AppCompatActivity implements OnMapReadyCallback { - - private static final String SOURCE_ID = "com.mapbox.mapboxsdk.style.layers.symbol.source.id"; - private static final String LAYER_ID = "com.mapbox.mapboxsdk.style.layers.symbol.layer.id"; - private static final String FEATURE_ID = "brk_name"; - private static final String FEATURE_RANK = "scalerank"; - private static final String FEATURE_NAME = "name_sort"; - private static final String FEATURE_TYPE = "type"; - private static final String FEATURE_REGION = "continent"; - - private MapView mapView; - private MapboxMap mapboxMap; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_symbol_generator); - - mapView = (MapView) findViewById(R.id.mapView); - mapView.onCreate(savedInstanceState); - mapView.getMapAsync(this); - } - - @Override - public void onMapReady(@NonNull final MapboxMap map) { - mapboxMap = map; - map.setStyle(Style.OUTDOORS, style -> { - addSymbolClickListener(); - new LoadDataTask(SymbolGeneratorActivity.this).execute(); - }); - } - - private void addSymbolClickListener() { - mapboxMap.addOnMapClickListener(point -> { - PointF screenPoint = mapboxMap.getProjection().toScreenLocation(point); - List<Feature> features = mapboxMap.queryRenderedFeatures(screenPoint, LAYER_ID); - if (!features.isEmpty()) { - Feature feature = features.get(0); - // validate symbol flicker regression for #13407 - SymbolLayer layer = mapboxMap.getStyle().getLayerAs(LAYER_ID); - layer.setProperties(iconOpacity(match( - get(FEATURE_ID), literal(1.0f), - stop(feature.getStringProperty(FEATURE_ID), 0.3f) - ))); - Timber.v("Feature was clicked with data: %s", feature.toJson()); - Toast.makeText( - SymbolGeneratorActivity.this, - "hello from: " + feature.getStringProperty(FEATURE_NAME), - Toast.LENGTH_LONG).show(); - } - - return false; - }); - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.menu_generator_symbol, menu); - return super.onCreateOptionsMenu(menu); - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - if (item.getItemId() == R.id.menu_action_icon_overlap) { - SymbolLayer layer = mapboxMap.getStyle().getLayerAs(LAYER_ID); - layer.setProperties(iconAllowOverlap(!layer.getIconAllowOverlap().getValue())); - return true; - } else if (item.getItemId() == R.id.menu_action_filter) { - SymbolLayer layer = mapboxMap.getStyle().getLayerAs(LAYER_ID); - layer.setFilter(eq(get(FEATURE_RANK), literal(1))); - Timber.e("Filter that was set: %s", layer.getFilter()); - return true; - } - return super.onOptionsItemSelected(item); - } - - @Override - protected void onStart() { - super.onStart(); - mapView.onStart(); - } - - @Override - protected void onResume() { - super.onResume(); - mapView.onResume(); - } - - @Override - protected void onPause() { - super.onPause(); - mapView.onPause(); - } - - @Override - protected void onStop() { - super.onStop(); - mapView.onStop(); - } - - @Override - public void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - mapView.onSaveInstanceState(outState); - } - - @Override - public void onLowMemory() { - super.onLowMemory(); - mapView.onLowMemory(); - } - - @Override - public void onDestroy() { - super.onDestroy(); - mapView.onDestroy(); - } - - /** - * Utility class to generate Bitmaps for Symbol. - * <p> - * Bitmaps can be added to the map with {@link com.mapbox.mapboxsdk.maps.MapboxMap#addImage(String, Bitmap)} - * </p> - */ - private static class SymbolGenerator { - - /** - * Generate a Bitmap from an Android SDK View. - * - * @param view the View to be drawn to a Bitmap - * @return the generated bitmap - */ - public static Bitmap generate(@NonNull View view) { - int measureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); - view.measure(measureSpec, measureSpec); - - int measuredWidth = view.getMeasuredWidth(); - int measuredHeight = view.getMeasuredHeight(); - - view.layout(0, 0, measuredWidth, measuredHeight); - Bitmap bitmap = Bitmap.createBitmap(measuredWidth, measuredHeight, Bitmap.Config.ARGB_8888); - bitmap.eraseColor(Color.TRANSPARENT); - Canvas canvas = new Canvas(bitmap); - view.draw(canvas); - return bitmap; - } - } - - private static class LoadDataTask extends AsyncTask<Void, Void, FeatureCollection> { - - private WeakReference<SymbolGeneratorActivity> activity; - - LoadDataTask(SymbolGeneratorActivity activity) { - this.activity = new WeakReference<>(activity); - } - - @Override - protected FeatureCollection doInBackground(Void... params) { - Context context = activity.get(); - if (context != null) { - try { - // read local geojson from raw folder - String tinyCountriesJson = ResourceUtils.readRawResource(context, R.raw.tiny_countries); - return FeatureCollection.fromJson(tinyCountriesJson); - - } catch (IOException exception) { - Timber.e(exception); - } - } - return null; - } - - @Override - protected void onPostExecute(FeatureCollection featureCollection) { - super.onPostExecute(featureCollection); - SymbolGeneratorActivity activity = this.activity.get(); - if (featureCollection == null || activity == null) { - return; - } - - activity.onDataLoaded(featureCollection); - } - } - - public void onDataLoaded(@NonNull FeatureCollection featureCollection) { - if (mapView.isDestroyed()) { - return; - } - - // create expressions - Expression iconImageExpression = string(get(literal(FEATURE_ID))); - Expression iconSizeExpression = division(number(get(literal(FEATURE_RANK))), literal(2.0f)); - Expression textSizeExpression = product(get(literal(FEATURE_RANK)), pi()); - Expression textFieldExpression = concat(upcase(literal("a ")), upcase(string(get(literal(FEATURE_TYPE)))), - downcase(literal(" IN ")), string(get(literal(FEATURE_REGION))) - ); - Expression textColorExpression = match(get(literal(FEATURE_RANK)), - literal(1), rgba(255, 0, 0, 1.0f), - literal(2), rgba(0, 0, 255.0f, 1.0f), - rgba(0.0f, 255.0f, 0.0f, 1.0f) - ); - - rgba( - division(literal(255), get(FEATURE_RANK)), - literal(0.0f), - literal(0.0f), - literal(1.0f) - ); - - // create symbol layer - SymbolLayer symbolLayer = new SymbolLayer(LAYER_ID, SOURCE_ID) - .withProperties( - // icon configuration - iconImage(iconImageExpression), - iconAllowOverlap(false), - iconSize(iconSizeExpression), - iconAnchor(ICON_ANCHOR_BOTTOM), - iconOffset(step(zoom(), literal(new float[] {0f, 0f}), - stop(1, new Float[] {0f, 0f}), - stop(10, new Float[] {0f, -35f}) - )), - - // text field configuration - textField(textFieldExpression), - textSize(textSizeExpression), - textAnchor(TEXT_ANCHOR_TOP), - textColor(textColorExpression) - ); - - // add a geojson source to the map - Source source = new GeoJsonSource(SOURCE_ID, featureCollection); - mapboxMap.getStyle().addSource(source); - - // add symbol layer - mapboxMap.getStyle().addLayer(symbolLayer); - - // get expressions - Expression iconImageExpressionResult = symbolLayer.getIconImage().getExpression(); - Expression iconSizeExpressionResult = symbolLayer.getIconSize().getExpression(); - Expression textSizeExpressionResult = symbolLayer.getTextSize().getExpression(); - Expression textFieldExpressionResult = symbolLayer.getTextField().getExpression(); - Expression textColorExpressionResult = symbolLayer.getTextColor().getExpression(); - - // log expressions - Timber.e(iconImageExpressionResult.toString()); - Timber.e(iconSizeExpressionResult.toString()); - Timber.e(textSizeExpressionResult.toString()); - Timber.e(textFieldExpressionResult.toString()); - Timber.e(textColorExpressionResult.toString()); - - // reset expressions - symbolLayer.setProperties( - iconImage(iconImageExpressionResult), - iconSize(iconSizeExpressionResult), - textSize(textSizeExpressionResult), - textField(textFieldExpressionResult), - textColor(textColorExpressionResult) - ); - - new GenerateSymbolTask(mapboxMap, this).execute(featureCollection); - } - - private static class GenerateSymbolTask extends AsyncTask<FeatureCollection, Void, HashMap<String, Bitmap>> { - - private MapboxMap mapboxMap; - private WeakReference<Context> context; - - GenerateSymbolTask(MapboxMap mapboxMap, Context context) { - this.mapboxMap = mapboxMap; - this.context = new WeakReference<>(context); - } - - @SuppressWarnings("WrongThread") - @Override - protected HashMap<String, Bitmap> doInBackground(FeatureCollection... params) { - HashMap<String, Bitmap> imagesMap = new HashMap<>(); - Context context = this.context.get(); - List<Feature> features = params[0].features(); - if (context != null && features != null) { - for (Feature feature : features) { - String countryName = feature.getStringProperty(FEATURE_ID); - TextView textView = new TextView(context); - textView.setBackgroundColor(context.getResources().getColor(R.color.blueAccent)); - textView.setPadding(10, 5, 10, 5); - textView.setTextColor(Color.WHITE); - textView.setText(countryName); - imagesMap.put(countryName, SymbolGenerator.generate(textView)); - } - } - return imagesMap; - } - - @Override - protected void onPostExecute(HashMap<String, Bitmap> bitmapHashMap) { - super.onPostExecute(bitmapHashMap); - mapboxMap.getStyle(new Style.OnStyleLoaded() { - @Override - public void onStyleLoaded(@NonNull Style style) { - style.addImagesAsync(bitmapHashMap); - } - }); - } - } -}
\ No newline at end of file diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/SymbolLayerActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/SymbolLayerActivity.java deleted file mode 100644 index ee562ad6e8..0000000000 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/SymbolLayerActivity.java +++ /dev/null @@ -1,375 +0,0 @@ -package com.mapbox.mapboxsdk.testapp.activity.style; - -import android.graphics.Bitmap; -import android.graphics.Color; -import android.graphics.PointF; -import android.os.Bundle; -import android.support.annotation.NonNull; -import android.support.v7.app.AppCompatActivity; -import android.view.Menu; -import android.view.MenuItem; -import android.view.ViewGroup; -import com.google.gson.JsonObject; -import com.google.gson.JsonPrimitive; -import com.mapbox.geojson.Feature; -import com.mapbox.geojson.FeatureCollection; -import com.mapbox.geojson.Point; -import com.mapbox.mapboxsdk.camera.CameraPosition; -import com.mapbox.mapboxsdk.geometry.LatLng; -import com.mapbox.mapboxsdk.maps.MapView; -import com.mapbox.mapboxsdk.maps.MapboxMap; -import com.mapbox.mapboxsdk.maps.MapboxMapOptions; -import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; -import com.mapbox.mapboxsdk.maps.Style; -import com.mapbox.mapboxsdk.style.expressions.Expression; -import com.mapbox.mapboxsdk.style.layers.Property; -import com.mapbox.mapboxsdk.style.layers.SymbolLayer; -import com.mapbox.mapboxsdk.style.sources.GeoJsonSource; -import com.mapbox.mapboxsdk.style.sources.Source; -import com.mapbox.mapboxsdk.testapp.R; -import com.mapbox.mapboxsdk.utils.BitmapUtils; -import timber.log.Timber; - -import java.util.Arrays; -import java.util.List; -import java.util.Objects; -import java.util.Random; - -import static com.mapbox.mapboxsdk.style.expressions.Expression.FormatOption.formatFontScale; -import static com.mapbox.mapboxsdk.style.expressions.Expression.FormatOption.formatTextColor; -import static com.mapbox.mapboxsdk.style.expressions.Expression.FormatOption.formatTextFont; -import static com.mapbox.mapboxsdk.style.expressions.Expression.NumberFormatOption.currency; -import static com.mapbox.mapboxsdk.style.expressions.Expression.NumberFormatOption.locale; -import static com.mapbox.mapboxsdk.style.expressions.Expression.concat; -import static com.mapbox.mapboxsdk.style.expressions.Expression.format; -import static com.mapbox.mapboxsdk.style.expressions.Expression.formatEntry; -import static com.mapbox.mapboxsdk.style.expressions.Expression.get; -import static com.mapbox.mapboxsdk.style.expressions.Expression.literal; -import static com.mapbox.mapboxsdk.style.expressions.Expression.match; -import static com.mapbox.mapboxsdk.style.expressions.Expression.numberFormat; -import static com.mapbox.mapboxsdk.style.expressions.Expression.rgb; -import static com.mapbox.mapboxsdk.style.expressions.Expression.stop; -import static com.mapbox.mapboxsdk.style.expressions.Expression.switchCase; -import static com.mapbox.mapboxsdk.style.expressions.Expression.toBool; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconAllowOverlap; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconAnchor; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconColor; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconIgnorePlacement; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconImage; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconOpacity; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconSize; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textAllowOverlap; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textAnchor; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textColor; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textField; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textFont; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textIgnorePlacement; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textRotationAlignment; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textSize; - -/** - * Test activity showcasing runtime manipulation of symbol layers. - * <p> - * Showcases the ability to offline render a symbol layer by using a packaged style and fonts from the assets folder. - * </p> - */ -public class SymbolLayerActivity extends AppCompatActivity implements MapboxMap.OnMapClickListener, OnMapReadyCallback { - - private static final String ID_FEATURE_PROPERTY = "id"; - private static final String SELECTED_FEATURE_PROPERTY = "selected"; - private static final String TITLE_FEATURE_PROPERTY = "title"; - - private static final String[] NORMAL_FONT_STACK = new String[] {"DIN Offc Pro Regular", "Arial Unicode MS Regular"}; - private static final String[] BOLD_FONT_STACK = new String[] {"DIN Offc Pro Bold", "Arial Unicode MS Regular"}; - - // layer & source constants - private static final String MARKER_SOURCE = "marker-source"; - private static final String MARKER_LAYER = "marker-layer"; - private static final String MAPBOX_SIGN_SOURCE = "mapbox-sign-source"; - private static final String MAPBOX_SIGN_LAYER = "mapbox-sign-layer"; - private static final String NUMBER_FORMAT_SOURCE = "mapbox-number-source"; - private static final String NUMBER_FORMAT_LAYER = "mapbox-number-layer"; - - private static final Expression TEXT_FIELD_EXPRESSION = - switchCase(toBool(get(SELECTED_FEATURE_PROPERTY)), - format( - formatEntry( - get(TITLE_FEATURE_PROPERTY), - formatTextFont(BOLD_FONT_STACK) - ), - formatEntry("\nis fun!", formatFontScale(0.75)) - ), - format( - formatEntry("This is", formatFontScale(0.75)), - formatEntry( - concat(literal("\n"), get(TITLE_FEATURE_PROPERTY)), - formatFontScale(1.25), - formatTextFont(BOLD_FONT_STACK) - ) - ) - ); - - private final Random random = new Random(); - private GeoJsonSource markerSource; - private FeatureCollection markerCollection; - private SymbolLayer markerSymbolLayer; - private SymbolLayer mapboxSignSymbolLayer; - private SymbolLayer numberFormatSymbolLayer; - private MapboxMap mapboxMap; - private MapView mapView; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_symbollayer); - - // Create map configuration - MapboxMapOptions mapboxMapOptions = MapboxMapOptions.createFromAttributes(this); - mapboxMapOptions.camera(new CameraPosition.Builder().target( - new LatLng(52.35273, 4.91638)) - .zoom(13) - .build() - ); - - // Create map programmatically, add to view hierarchy - mapView = new MapView(this, mapboxMapOptions); - mapView.getMapAsync(this); - mapView.onCreate(savedInstanceState); - ((ViewGroup) findViewById(R.id.container)).addView(mapView); - - // Use OnStyleImageMissing API to lazily load an icon - mapView.addOnStyleImageMissingListener(id -> { - Style style = mapboxMap.getStyle(); - if (style != null) { - Timber.e("Adding image with id: %s", id); - Bitmap androidIcon = BitmapUtils.getBitmapFromDrawable(getResources().getDrawable(R.drawable.ic_android_2)); - style.addImage(id, Objects.requireNonNull(androidIcon)); - } - }); - } - - @Override - public void onMapReady(@NonNull MapboxMap mapboxMap) { - this.mapboxMap = mapboxMap; - Bitmap carBitmap = BitmapUtils.getBitmapFromDrawable( - getResources().getDrawable(R.drawable.ic_directions_car_black)); - - // marker source - markerCollection = FeatureCollection.fromFeatures(new Feature[] { - Feature.fromGeometry(Point.fromLngLat(4.91638, 52.35673), featureProperties("1", "Android")), - Feature.fromGeometry(Point.fromLngLat(4.91638, 52.34673), featureProperties("2", "Car")) - }); - markerSource = new GeoJsonSource(MARKER_SOURCE, markerCollection); - - // marker layer - markerSymbolLayer = new SymbolLayer(MARKER_LAYER, MARKER_SOURCE) - .withProperties( - iconImage(get(TITLE_FEATURE_PROPERTY)), - iconIgnorePlacement(true), - iconAllowOverlap(true), - iconSize(switchCase(toBool(get(SELECTED_FEATURE_PROPERTY)), literal(1.5f), literal(1.0f))), - iconAnchor(Property.ICON_ANCHOR_BOTTOM), - iconColor(Color.BLUE), - textField(TEXT_FIELD_EXPRESSION), - textFont(NORMAL_FONT_STACK), - textColor(Color.BLUE), - textAllowOverlap(true), - textIgnorePlacement(true), - textAnchor(Property.TEXT_ANCHOR_TOP), - textSize(10f) - ); - - // mapbox sign layer - Source mapboxSignSource = new GeoJsonSource(MAPBOX_SIGN_SOURCE, Point.fromLngLat(4.91638, 52.3510)); - mapboxSignSymbolLayer = new SymbolLayer(MAPBOX_SIGN_LAYER, MAPBOX_SIGN_SOURCE); - shuffleMapboxSign(); - - // number format layer - Source numberFormatSource = new GeoJsonSource(NUMBER_FORMAT_SOURCE, Point.fromLngLat(4.92756, 52.3516)); - numberFormatSymbolLayer = new SymbolLayer(NUMBER_FORMAT_LAYER, NUMBER_FORMAT_SOURCE); - numberFormatSymbolLayer.setProperties( - textField( - numberFormat(123.456789, locale("nl-NL"), currency("EUR")) - ) - ); - - mapboxMap.setStyle(new Style.Builder() - .fromUri("asset://streets.json") - .withImage("Car", Objects.requireNonNull(carBitmap), false) - .withSources(markerSource, mapboxSignSource, numberFormatSource) - .withLayers(markerSymbolLayer, mapboxSignSymbolLayer, numberFormatSymbolLayer) - ); - - // Set a click-listener so we can manipulate the map - mapboxMap.addOnMapClickListener(SymbolLayerActivity.this); - } - - @Override - public boolean onMapClick(@NonNull LatLng point) { - // Query which features are clicked - PointF screenLoc = mapboxMap.getProjection().toScreenLocation(point); - List<Feature> markerFeatures = mapboxMap.queryRenderedFeatures(screenLoc, MARKER_LAYER); - if (!markerFeatures.isEmpty()) { - for (Feature feature : Objects.requireNonNull(markerCollection.features())) { - if (feature.getStringProperty(ID_FEATURE_PROPERTY) - .equals(markerFeatures.get(0).getStringProperty(ID_FEATURE_PROPERTY))) { - - // use DDS - boolean selected = feature.getBooleanProperty(SELECTED_FEATURE_PROPERTY); - feature.addBooleanProperty(SELECTED_FEATURE_PROPERTY, !selected); - - // validate symbol flicker regression for #13407 - markerSymbolLayer.setProperties(iconOpacity(match( - get(ID_FEATURE_PROPERTY), literal(1.0f), - stop(feature.getStringProperty("id"), selected ? 0.3f : 1.0f) - ))); - } - } - markerSource.setGeoJson(markerCollection); - } else { - List<Feature> mapboxSignFeatures = mapboxMap.queryRenderedFeatures(screenLoc, MAPBOX_SIGN_LAYER); - if (!mapboxSignFeatures.isEmpty()) { - shuffleMapboxSign(); - } - } - - return false; - } - - private void toggleTextSize() { - if (markerSymbolLayer != null) { - Number size = markerSymbolLayer.getTextSize().getValue(); - if (size != null) { - markerSymbolLayer.setProperties((float) size > 10 ? textSize(10f) : textSize(20f)); - } - } - } - - private void toggleTextField() { - if (markerSymbolLayer != null) { - if (TEXT_FIELD_EXPRESSION.equals(markerSymbolLayer.getTextField().getExpression())) { - markerSymbolLayer.setProperties(textField("āA")); - } else { - markerSymbolLayer.setProperties(textField(TEXT_FIELD_EXPRESSION)); - } - } - } - - private void toggleTextFont() { - if (markerSymbolLayer != null) { - if (Arrays.equals(markerSymbolLayer.getTextFont().getValue(), NORMAL_FONT_STACK)) { - markerSymbolLayer.setProperties(textFont(BOLD_FONT_STACK)); - } else { - markerSymbolLayer.setProperties(textFont(NORMAL_FONT_STACK)); - } - } - } - - private void shuffleMapboxSign() { - if (mapboxSignSymbolLayer != null) { - mapboxSignSymbolLayer.setProperties( - textField( - format( - formatEntry("M", formatFontScale(2)), - getRandomColorEntryForString("a"), - getRandomColorEntryForString("p"), - getRandomColorEntryForString("b"), - getRandomColorEntryForString("o"), - getRandomColorEntryForString("x") - ) - ), - textColor(Color.BLACK), - textFont(BOLD_FONT_STACK), - textSize(25f), - textRotationAlignment(Property.TEXT_ROTATION_ALIGNMENT_MAP) - ); - } - } - - private Expression.FormatEntry getRandomColorEntryForString(@NonNull String string) { - return formatEntry(string, - formatTextColor( - rgb( - random.nextInt(256), - random.nextInt(256), - random.nextInt(256) - ) - )); - } - - private JsonObject featureProperties(@NonNull String id, @NonNull String title) { - JsonObject object = new JsonObject(); - object.add(ID_FEATURE_PROPERTY, new JsonPrimitive(id)); - object.add(TITLE_FEATURE_PROPERTY, new JsonPrimitive(title)); - object.add(SELECTED_FEATURE_PROPERTY, new JsonPrimitive(false)); - return object; - } - - @Override - protected void onStart() { - super.onStart(); - mapView.onStart(); - } - - @Override - protected void onResume() { - super.onResume(); - mapView.onResume(); - } - - @Override - protected void onPause() { - super.onPause(); - mapView.onPause(); - } - - @Override - protected void onStop() { - super.onStop(); - mapView.onStop(); - } - - @Override - public void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - mapView.onSaveInstanceState(outState); - } - - @Override - public void onLowMemory() { - super.onLowMemory(); - mapView.onLowMemory(); - } - - @Override - public void onDestroy() { - super.onDestroy(); - if (mapboxMap != null) { - mapboxMap.removeOnMapClickListener(this); - } - mapView.onDestroy(); - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.menu_symbol_layer, menu); - return true; - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case R.id.action_toggle_text_size: - toggleTextSize(); - return true; - case R.id.action_toggle_text_field: - toggleTextField(); - return true; - case R.id.action_toggle_text_font: - toggleTextFont(); - return true; - default: - return super.onOptionsItemSelected(item); - } - } -} diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/ZoomFunctionSymbolLayerActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/ZoomFunctionSymbolLayerActivity.java deleted file mode 100644 index 5710ae2604..0000000000 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/ZoomFunctionSymbolLayerActivity.java +++ /dev/null @@ -1,199 +0,0 @@ -package com.mapbox.mapboxsdk.testapp.activity.style; - -import android.graphics.PointF; -import android.os.Bundle; -import android.support.annotation.NonNull; -import android.support.v7.app.AppCompatActivity; -import android.view.Menu; -import android.view.MenuItem; -import com.google.gson.JsonObject; -import com.mapbox.geojson.Feature; -import com.mapbox.geojson.FeatureCollection; -import com.mapbox.geojson.Point; -import com.mapbox.mapboxsdk.geometry.LatLng; -import com.mapbox.mapboxsdk.maps.MapView; -import com.mapbox.mapboxsdk.maps.MapboxMap; -import com.mapbox.mapboxsdk.maps.Style; -import com.mapbox.mapboxsdk.style.layers.Property; -import com.mapbox.mapboxsdk.style.layers.SymbolLayer; -import com.mapbox.mapboxsdk.style.sources.GeoJsonSource; -import com.mapbox.mapboxsdk.testapp.R; -import timber.log.Timber; - -import java.util.List; - -import static com.mapbox.mapboxsdk.style.expressions.Expression.get; -import static com.mapbox.mapboxsdk.style.expressions.Expression.literal; -import static com.mapbox.mapboxsdk.style.expressions.Expression.step; -import static com.mapbox.mapboxsdk.style.expressions.Expression.stop; -import static com.mapbox.mapboxsdk.style.expressions.Expression.switchCase; -import static com.mapbox.mapboxsdk.style.expressions.Expression.zoom; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconAllowOverlap; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconImage; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconSize; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.visibility; - -/** - * Test activity showcasing changing the icon with a zoom function and adding selection state to a SymbolLayer. - */ -public class ZoomFunctionSymbolLayerActivity extends AppCompatActivity { - - private static final String LAYER_ID = "symbolLayer"; - private static final String SOURCE_ID = "poiSource"; - private static final String BUS_MAKI_ICON_ID = "bus"; - private static final String CAFE_MAKI_ICON_ID = "cafe-11"; - private static final String KEY_PROPERTY_SELECTED = "selected"; - private static final float ZOOM_STOP_MAX_VALUE = 12.0f; - - private MapView mapView; - private MapboxMap mapboxMap; - private GeoJsonSource source; - private SymbolLayer layer; - - private boolean isInitialPosition = true; - private boolean isSelected = false; - private boolean isShowingSymbolLayer = true; - - private MapboxMap.OnMapClickListener mapClickListener = new MapboxMap.OnMapClickListener() { - @Override - public boolean onMapClick(@NonNull LatLng point) { - PointF screenPoint = mapboxMap.getProjection().toScreenLocation(point); - List<Feature> featureList = mapboxMap.queryRenderedFeatures(screenPoint, LAYER_ID); - if (!featureList.isEmpty()) { - Feature feature = featureList.get(0); - boolean selectedNow = feature.getBooleanProperty(KEY_PROPERTY_SELECTED); - isSelected = !selectedNow; - updateSource(mapboxMap.getStyle()); - } else { - Timber.e("No features found"); - } - return true; - } - }; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_zoom_symbol_layer); - - mapView = findViewById(R.id.mapView); - mapView.onCreate(savedInstanceState); - mapView.getMapAsync(map -> { - mapboxMap = map; - - map.setStyle(Style.MAPBOX_STREETS, style -> { - updateSource(style); - addLayer(style); - map.addOnMapClickListener(mapClickListener); - }); - }); - } - - private void updateSource(Style style) { - FeatureCollection featureCollection = createFeatureCollection(); - if (source != null) { - source.setGeoJson(featureCollection); - } else { - source = new GeoJsonSource(SOURCE_ID, featureCollection); - style.addSource(source); - } - } - - private void toggleSymbolLayerVisibility() { - layer.setProperties( - visibility(isShowingSymbolLayer ? Property.NONE : Property.VISIBLE) - ); - isShowingSymbolLayer = !isShowingSymbolLayer; - } - - private FeatureCollection createFeatureCollection() { - Point point = isInitialPosition - ? Point.fromLngLat(-74.01618140, 40.701745) - : Point.fromLngLat(-73.988097, 40.749864); - - JsonObject properties = new JsonObject(); - properties.addProperty(KEY_PROPERTY_SELECTED, isSelected); - Feature feature = Feature.fromGeometry(point, properties); - return FeatureCollection.fromFeatures(new Feature[] {feature}); - } - - private void addLayer(Style style) { - layer = new SymbolLayer(LAYER_ID, SOURCE_ID); - layer.setProperties( - iconImage( - step(zoom(), literal(BUS_MAKI_ICON_ID), - stop(ZOOM_STOP_MAX_VALUE, CAFE_MAKI_ICON_ID) - ) - ), - iconSize( - switchCase( - get(KEY_PROPERTY_SELECTED), literal(3.0f), - literal(1.0f) - ) - ), - iconAllowOverlap(true) - ); - style.addLayer(layer); - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.menu_symbols, menu); - return true; - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - if (mapboxMap != null) { - if (item.getItemId() == R.id.menu_action_change_location) { - isInitialPosition = !isInitialPosition; - updateSource(mapboxMap.getStyle()); - } else if (item.getItemId() == R.id.menu_action_toggle_source) { - toggleSymbolLayerVisibility(); - } - } - return super.onOptionsItemSelected(item); - } - - @Override - protected void onStart() { - super.onStart(); - mapView.onStart(); - } - - @Override - protected void onResume() { - super.onResume(); - mapView.onResume(); - } - - @Override - protected void onPause() { - super.onPause(); - mapView.onPause(); - } - - @Override - protected void onStop() { - super.onStop(); - mapView.onStop(); - } - - @Override - public void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - mapView.onSaveInstanceState(outState); - } - - @Override - public void onLowMemory() { - super.onLowMemory(); - mapView.onLowMemory(); - } - - @Override - public void onDestroy() { - super.onDestroy(); - mapView.onDestroy(); - } -}
\ No newline at end of file |