Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Offline symbol render example #12676

Merged
merged 1 commit into from
Aug 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12,447 changes: 12,447 additions & 0 deletions platform/android/MapboxGLAndroidSDKTestApp/src/main/assets/streets.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -9,100 +9,119 @@
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.CameraUpdateFactory;
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.style.layers.Property;
import com.mapbox.mapboxsdk.style.layers.SymbolLayer;
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.mapboxsdk.utils.BitmapUtils;


import java.util.Arrays;
import java.util.List;

import static com.mapbox.mapboxsdk.style.expressions.Expression.any;
import static com.mapbox.mapboxsdk.style.expressions.Expression.get;
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.lte;
import static com.mapbox.mapboxsdk.style.expressions.Expression.not;
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.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.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 {
public class SymbolLayerActivity extends AppCompatActivity implements MapboxMap.OnMapClickListener, OnMapReadyCallback {

private static final String MARKER_SOURCE = "marker-source";
private static final String MARKER_LAYER = "marker-layer";
private static final String MARKER_ICON = "my-layers-image";

public static final String MARKER_SOURCE = "marker-source";
public static final String MARKER_LAYER = "marker-layer";
private MapboxMap mapboxMap;
private MapView mapView;
private boolean initialFont;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_symbollayer);

mapView = (MapView) findViewById(R.id.mapView);
// Create map configuration
MapboxMapOptions mapboxMapOptions = new MapboxMapOptions();
mapboxMapOptions.camera(new CameraPosition.Builder().target(
new LatLng(52.35273, 4.91638))
.zoom(13)
.build()
);
mapboxMapOptions.styleUrl("asset://streets.json");

// Create map programmatically, add to view hierarchy
mapView = new MapView(this, mapboxMapOptions);
mapView.getMapAsync(this);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(map -> {
mapboxMap = map;

// Add a sdf image for the makers
Drawable icLayersDrawable = getResources().getDrawable(R.drawable.ic_layers);
Bitmap icLayersBitmap = BitmapUtils.getBitmapFromDrawable(icLayersDrawable);
mapboxMap.addImage(
"my-layers-image",
icLayersBitmap,
true
);

// Add a source
FeatureCollection markers = FeatureCollection.fromFeatures(new Feature[] {
Feature.fromGeometry(Point.fromLngLat(4.91638, 52.35673), featureProperties("Marker 1")),
Feature.fromGeometry(Point.fromLngLat(4.91638, 52.34673), featureProperties("Marker 2"))
});
mapboxMap.addSource(new GeoJsonSource(MARKER_SOURCE, markers));

// Add the symbol-layer
mapboxMap.addLayer(
new SymbolLayer(MARKER_LAYER, MARKER_SOURCE)
.withProperties(
iconImage("my-layers-image"),
iconAllowOverlap(true),
iconAnchor(Property.ICON_ANCHOR_BOTTOM),
textField(get("title")),
iconColor(Color.RED),
textColor(Color.RED),
textAnchor(Property.TEXT_ANCHOR_TOP),
textSize(10f)
).withFilter((any(not(has("price")), lte(get("price"), literal(25)))))
);

// Show
mapboxMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(52.35273, 4.91638), 14));

// Set a click-listener so we can manipulate the map
mapboxMap.setOnMapClickListener(SymbolLayerActivity.this);
((ViewGroup) findViewById(R.id.container)).addView(mapView);
}

@Override
public void onMapReady(MapboxMap mapboxMap) {
this.mapboxMap = mapboxMap;

// Add a sdf image for the makers
Drawable icLayersDrawable = getResources().getDrawable(R.drawable.ic_layers);
Bitmap icLayersBitmap = BitmapUtils.getBitmapFromDrawable(icLayersDrawable);
mapboxMap.addImage(
MARKER_ICON,
icLayersBitmap,
true
);

// Add a source
FeatureCollection markers = FeatureCollection.fromFeatures(new Feature[] {
Feature.fromGeometry(Point.fromLngLat(4.91638, 52.35673), featureProperties("Marker 1")),
Feature.fromGeometry(Point.fromLngLat(4.91638, 52.34673), featureProperties("Marker 2"))
});
mapboxMap.addSource(new GeoJsonSource(MARKER_SOURCE, markers));

// Add the symbol-layer
mapboxMap.addLayer(
new SymbolLayer(MARKER_LAYER, MARKER_SOURCE)
.withProperties(
iconImage(MARKER_ICON),
iconIgnorePlacement(true),
iconAllowOverlap(true),
iconAnchor(Property.ICON_ANCHOR_BOTTOM),
iconColor(Color.RED),
textField(get("title")),
textFont(new String[] {"DIN Offc Pro Regular", "Arial Unicode MS Regular"}),
textColor(Color.RED),
textAllowOverlap(true),
textIgnorePlacement(true),
textAnchor(Property.TEXT_ANCHOR_TOP),
textSize(10f)
)
);

// Set a click-listener so we can manipulate the map
mapboxMap.addOnMapClickListener(SymbolLayerActivity.this);
}

@Override
Expand Down Expand Up @@ -132,13 +151,12 @@ private void toggleTextField() {

private void toggleTextFont() {
SymbolLayer layer = mapboxMap.getLayerAs(MARKER_LAYER);

String[] fonts = layer.getTextFont().getValue();
if (fonts == null || fonts.length == 0 || Arrays.asList(fonts).contains("Arial Unicode MS Regular")) {
if (initialFont) {
layer.setProperties(textFont(new String[] {"DIN Offc Pro Bold", "Arial Unicode MS Bold"}));
} else {
layer.setProperties(textFont(new String[] {"DIN Offc Pro Medium", "Arial Unicode MS Regular"}));
}
initialFont = !initialFont;
}

private JsonObject featureProperties(String title) {
Expand Down Expand Up @@ -186,6 +204,9 @@ public void onLowMemory() {
@Override
public void onDestroy() {
super.onDestroy();
if (mapboxMap != null) {
mapboxMap.removeOnMapClickListener(this);
}
mapView.onDestroy();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.mapbox.mapboxsdk.maps.MapView
android:id="@id/mapView"
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:mapbox_styleUrl="@string/mapbox_style_mapbox_streets"/>
android:orientation="vertical"/>

</RelativeLayout>
3 changes: 2 additions & 1 deletion platform/android/scripts/exclude-activity-gen.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@
"SymbolGeneratorActivity",
"TextureViewTransparentBackgroundActivity",
"SimpleMapActivity",
"RenderTestActivity"
"RenderTestActivity",
"SymbolLayerActivity"
]