diff options
2 files changed, 105 insertions, 52 deletions
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/PolygonActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/PolygonActivity.java index 1382ab8141..d8c3cb6011 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/PolygonActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/PolygonActivity.java @@ -2,9 +2,7 @@ package com.mapbox.mapboxsdk.testapp.activity.annotation; import android.graphics.Color; import android.os.Bundle; -import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.view.ViewGroup; @@ -19,62 +17,41 @@ import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.mapboxsdk.maps.MapboxMapOptions; import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; import com.mapbox.mapboxsdk.testapp.R; +import com.mapbox.mapboxsdk.testapp.utils.ToolbarComposer; import java.util.ArrayList; import java.util.List; +import static com.mapbox.mapboxsdk.testapp.activity.annotation.PolygonActivity.Config.BLUE_COLOR; +import static com.mapbox.mapboxsdk.testapp.activity.annotation.PolygonActivity.Config.BROKEN_SHAPE_POINTS; +import static com.mapbox.mapboxsdk.testapp.activity.annotation.PolygonActivity.Config.FULL_ALPHA; +import static com.mapbox.mapboxsdk.testapp.activity.annotation.PolygonActivity.Config.NO_ALPHA; +import static com.mapbox.mapboxsdk.testapp.activity.annotation.PolygonActivity.Config.PARTIAL_ALPHA; +import static com.mapbox.mapboxsdk.testapp.activity.annotation.PolygonActivity.Config.RED_COLOR; +import static com.mapbox.mapboxsdk.testapp.activity.annotation.PolygonActivity.Config.STAR_SHAPE_POINTS; + +/** + * Activity to test the Polygon annotation API & programmatically creating a MapView. + * <p> + * Showcases changing Polygon features as visibility, alpha, color and points. + * </p> + */ public class PolygonActivity extends AppCompatActivity implements OnMapReadyCallback { - private static final int BLUE_COLOR = Color.parseColor("#3bb2d0"); - private static final int RED_COLOR = Color.parseColor("#AF0000"); - - private static final float FULL_ALPHA = 1.0f; - private static final float PARTIAL_ALPHA = 0.5f; - private static final float NO_ALPHA = 0.0f; - - private static final List<LatLng> POINTS = new ArrayList<LatLng>() {{ - add(new LatLng(45.522585, -122.685699)); - add(new LatLng(45.534611, -122.708873)); - add(new LatLng(45.530883, -122.678833)); - add(new LatLng(45.547115, -122.667503)); - add(new LatLng(45.530643, -122.660121)); - add(new LatLng(45.533529, -122.636260)); - add(new LatLng(45.521743, -122.659091)); - add(new LatLng(45.510677, -122.648792)); - add(new LatLng(45.515008, -122.664070)); - add(new LatLng(45.502496, -122.669048)); - } - }; - - private List<LatLng> ADDITIONAL_POINTS = new ArrayList<LatLng>() {{ - add(new LatLng(45.515369, -122.678489)); - add(new LatLng(45.506346, -122.702007)); - add(new LatLng(45.522585, -122.685699)); - } - }; - private MapView mapView; private MapboxMap mapboxMap; - private Polygon polygon; + private Polygon polygon; private boolean fullAlpha = true; private boolean visible = true; - private boolean allPoints = true; private boolean color = true; + private boolean allPoints; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_polygon); - - Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); - setSupportActionBar(toolbar); - - ActionBar actionBar = getSupportActionBar(); - if (actionBar != null) { - actionBar.setDisplayHomeAsUpEnabled(true); - actionBar.setDisplayShowHomeEnabled(true); - } + ToolbarComposer.initDefaultUpToolbar(this, R.id.toolbar); // configure inital map state MapboxMapOptions options = new MapboxMapOptions() @@ -95,15 +72,17 @@ public class PolygonActivity extends AppCompatActivity implements OnMapReadyCall mapView.getMapAsync(this); // add to layout - ((ViewGroup) findViewById(R.id.container)).addView(mapView); + ViewGroup container = (ViewGroup) findViewById(R.id.container); + if (container != null) { + container.addView(mapView); + } } @Override - public void onMapReady(MapboxMap mapboxMap) { - this.mapboxMap = mapboxMap; - POINTS.addAll(ADDITIONAL_POINTS); + public void onMapReady(MapboxMap map) { + mapboxMap = map; polygon = mapboxMap.addPolygon(new PolygonOptions() - .addAll(POINTS) + .addAll(STAR_SHAPE_POINTS) .fillColor(BLUE_COLOR)); } @@ -156,12 +135,7 @@ public class PolygonActivity extends AppCompatActivity implements OnMapReadyCall case R.id.action_id_points: allPoints = !allPoints; - if (allPoints) { - POINTS.addAll(ADDITIONAL_POINTS); - } else { - POINTS.removeAll(ADDITIONAL_POINTS); - } - polygon.setPoints(POINTS); + polygon.setPoints(allPoints ? STAR_SHAPE_POINTS : BROKEN_SHAPE_POINTS); return true; case R.id.action_id_color: @@ -178,4 +152,34 @@ public class PolygonActivity extends AppCompatActivity implements OnMapReadyCall getMenuInflater().inflate(R.menu.menu_polygon, menu); return true; } + + final static class Config { + static final int BLUE_COLOR = Color.parseColor("#3bb2d0"); + static final int RED_COLOR = Color.parseColor("#AF0000"); + + static final float FULL_ALPHA = 1.0f; + static final float PARTIAL_ALPHA = 0.5f; + static final float NO_ALPHA = 0.0f; + + static final List<LatLng> STAR_SHAPE_POINTS = new ArrayList<LatLng>() { + { + add(new LatLng(45.522585, -122.685699)); + add(new LatLng(45.534611, -122.708873)); + add(new LatLng(45.530883, -122.678833)); + add(new LatLng(45.547115, -122.667503)); + add(new LatLng(45.530643, -122.660121)); + add(new LatLng(45.533529, -122.636260)); + add(new LatLng(45.521743, -122.659091)); + add(new LatLng(45.510677, -122.648792)); + add(new LatLng(45.515008, -122.664070)); + add(new LatLng(45.502496, -122.669048)); + add(new LatLng(45.515369, -122.678489)); + add(new LatLng(45.506346, -122.702007)); + add(new LatLng(45.522585, -122.685699)); + } + }; + + static final List<LatLng> BROKEN_SHAPE_POINTS = + STAR_SHAPE_POINTS.subList(0, STAR_SHAPE_POINTS.size() - 3); + } } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/utils/ToolbarComposer.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/utils/ToolbarComposer.java new file mode 100644 index 0000000000..7822e41bd9 --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/utils/ToolbarComposer.java @@ -0,0 +1,49 @@ +package com.mapbox.mapboxsdk.testapp.utils; + +import android.support.annotation.IdRes; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.v7.app.ActionBar; +import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.Toolbar; + +import com.mapbox.mapboxsdk.testapp.R; + +/** + * Helper class to compose a Toolbar in an AppCompatActivity + */ +public class ToolbarComposer { + + + /** + * Initialises an up navigation toolbar with id R.id.toolbar on an AppCompatActivity. + * + * @param activity The activity hosting the Toolbar with id R.id.toolbar + */ + @Nullable + public static Toolbar initDefaultUpToolbar(@NonNull AppCompatActivity activity) { + return initDefaultUpToolbar(activity, R.id.toolbar); + } + + /** + * Initialises an up navigation toolbar given a view id on an AppCompatActivity. + * + * @param activity The activity hosting the Toolbar + * @param toolbarRes The view id resource used to look up the Toolbar + */ + @Nullable + public static Toolbar initDefaultUpToolbar(@NonNull AppCompatActivity activity, @IdRes int toolbarRes) { + Toolbar toolbar = (Toolbar) activity.findViewById(toolbarRes); + if (toolbar != null) { + activity.setSupportActionBar(toolbar); + + ActionBar actionBar = activity.getSupportActionBar(); + if (actionBar != null) { + actionBar.setDisplayHomeAsUpEnabled(true); + actionBar.setDisplayShowHomeEnabled(true); + } + } + return toolbar; + } + +} |