diff options
Diffstat (limited to 'platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/SymbolGeneratorActivity.java')
-rw-r--r-- | platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/SymbolGeneratorActivity.java | 138 |
1 files changed, 86 insertions, 52 deletions
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 index 6e9e45cfaa..ca4176be6e 100644 --- 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 @@ -15,22 +15,18 @@ import android.view.View; import android.widget.TextView; import android.widget.Toast; -import com.google.gson.GsonBuilder; -import com.mapbox.mapboxsdk.geometry.LatLng; +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.style.layers.Filter; 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 com.mapbox.services.commons.geojson.Feature; -import com.mapbox.services.commons.geojson.FeatureCollection; -import com.mapbox.services.commons.geojson.Geometry; -import com.mapbox.services.commons.geojson.custom.GeometryDeserializer; -import com.mapbox.services.commons.geojson.custom.PositionDeserializer; -import com.mapbox.services.commons.models.Position; + import java.io.IOException; import java.util.HashMap; @@ -38,8 +34,24 @@ 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.get; +import static com.mapbox.mapboxsdk.style.expressions.Expression.literal; +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.upcase; +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.iconSize; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textAnchor; +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. @@ -49,7 +61,10 @@ public class SymbolGeneratorActivity extends AppCompatActivity implements OnMapR 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_VALUE = "name_sort"; + 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; @@ -68,23 +83,20 @@ public class SymbolGeneratorActivity extends AppCompatActivity implements OnMapR public void onMapReady(final MapboxMap map) { mapboxMap = map; addSymbolClickListener(); - new LoadDataTask(map, SymbolGeneratorActivity.this).execute(); + new LoadDataTask(this).execute(); } private void addSymbolClickListener() { - mapboxMap.setOnMapClickListener(new MapboxMap.OnMapClickListener() { - @Override - public void onMapClick(@NonNull LatLng point) { - PointF screenPoint = mapboxMap.getProjection().toScreenLocation(point); - List<Feature> features = mapboxMap.queryRenderedFeatures(screenPoint, LAYER_ID); - if (!features.isEmpty()) { - Feature feature = features.get(0); - Timber.v("Feature was clicked with data: %s", feature.toJson()); - Toast.makeText( - SymbolGeneratorActivity.this, - "hello from: " + feature.getStringProperty(FEATURE_VALUE), - Toast.LENGTH_LONG).show(); - } + mapboxMap.setOnMapClickListener(point -> { + PointF screenPoint = mapboxMap.getProjection().toScreenLocation(point); + List<Feature> features = mapboxMap.queryRenderedFeatures(screenPoint, LAYER_ID); + if (!features.isEmpty()) { + Feature feature = features.get(0); + Timber.v("Feature was clicked with data: %s", feature.toJson()); + Toast.makeText( + SymbolGeneratorActivity.this, + "hello from: " + feature.getStringProperty(FEATURE_NAME), + Toast.LENGTH_LONG).show(); } }); } @@ -100,6 +112,12 @@ public class SymbolGeneratorActivity extends AppCompatActivity implements OnMapR if (item.getItemId() == R.id.menu_action_icon_overlap) { SymbolLayer layer = mapboxMap.getLayerAs(LAYER_ID); layer.setProperties(iconAllowOverlap(!layer.getIconAllowOverlap().getValue())); + return true; + } else if (item.getItemId() == R.id.menu_action_filter) { + SymbolLayer layer = mapboxMap.getLayerAs(LAYER_ID); + layer.setFilter(Filter.eq(FEATURE_RANK, 1)); + //layer.setFilter(eq(get(FEATURE_RANK), 1)); + return true; } return super.onOptionsItemSelected(item); } @@ -178,56 +196,72 @@ public class SymbolGeneratorActivity extends AppCompatActivity implements OnMapR private static class LoadDataTask extends AsyncTask<Void, Void, FeatureCollection> { - private final MapboxMap mapboxMap; - private final Context context; + private SymbolGeneratorActivity activity; - LoadDataTask(MapboxMap mapboxMap, Context context) { - this.mapboxMap = mapboxMap; - this.context = context; + LoadDataTask(SymbolGeneratorActivity activity) { + this.activity = activity; } @Override protected FeatureCollection doInBackground(Void... params) { try { // read local geojson from raw folder - String tinyCountriesJson = ResourceUtils.readRawResource(context, R.raw.tiny_countries); - - // convert geojson to a model - FeatureCollection featureCollection = new GsonBuilder() - .registerTypeAdapter(Geometry.class, new GeometryDeserializer()) - .registerTypeAdapter(Position.class, new PositionDeserializer()) - .create().fromJson(tinyCountriesJson, FeatureCollection.class); + String tinyCountriesJson = ResourceUtils.readRawResource(activity, R.raw.tiny_countries); + return FeatureCollection.fromJson(tinyCountriesJson); - return featureCollection; } catch (IOException exception) { return null; } } - @Override protected void onPostExecute(FeatureCollection featureCollection) { super.onPostExecute(featureCollection); - if (featureCollection == null) { + if (featureCollection == null || activity == null) { return; } - // add a geojson to the map - Source source = new GeoJsonSource(SOURCE_ID, featureCollection); - mapboxMap.addSource(source); - - // create layer use - mapboxMap.addLayer(new SymbolLayer(LAYER_ID, SOURCE_ID) - .withProperties( - iconImage("{" + FEATURE_ID + "}"), // { } is a token notation - iconAllowOverlap(false) - ) - ); - - new GenerateSymbolTask(mapboxMap, context).execute(featureCollection); + activity.onDataLoaded(featureCollection); } } + public void onDataLoaded(@NonNull FeatureCollection featureCollection) { + // add a geojson to the map + Source source = new GeoJsonSource(SOURCE_ID, featureCollection); + mapboxMap.addSource(source); + + // create layer use + mapboxMap.addLayer(new SymbolLayer(LAYER_ID, SOURCE_ID) + .withProperties( + + // icon configuration + iconImage(get(literal(FEATURE_ID))), + iconAllowOverlap(false), + iconSize( + division(get(literal(FEATURE_RANK)), literal(2)) + ), + iconAnchor(ICON_ANCHOR_BOTTOM), + iconOffset(new Float[] {0.0f, -5.0f}), + + // text field configuration + textField( + concat( + upcase(literal("a ")), + get(literal(FEATURE_TYPE)), + downcase(literal(" IN ")), + get(literal(FEATURE_REGION)) + ) + ), + textSize( + product(get(literal(FEATURE_RANK)), pi()) + ), + textAnchor(TEXT_ANCHOR_TOP) + ) + ); + + new GenerateSymbolTask(mapboxMap, this).execute(featureCollection); + } + private static class GenerateSymbolTask extends AsyncTask<FeatureCollection, Void, HashMap<String, Bitmap>> { private MapboxMap mapboxMap; @@ -244,7 +278,7 @@ public class SymbolGeneratorActivity extends AppCompatActivity implements OnMapR FeatureCollection featureCollection = params[0]; HashMap<String, Bitmap> imagesMap = new HashMap<>(); - for (Feature feature : featureCollection.getFeatures()) { + for (Feature feature : featureCollection.features()) { String countryName = feature.getStringProperty(FEATURE_ID); TextView textView = new TextView(context); textView.setBackgroundColor(context.getResources().getColor(R.color.blueAccent)); |