[android] - example to render symbols offline with resources loaded from assets
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
12447
platform/android/MapboxGLAndroidSDKTestApp/src/main/assets/streets.json
Normal file
12447
platform/android/MapboxGLAndroidSDKTestApp/src/main/assets/streets.json
Normal file
File diff suppressed because it is too large
Load Diff
@@ -9,100 +9,119 @@ 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.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;
|
||||
((ViewGroup) findViewById(R.id.container)).addView(mapView);
|
||||
}
|
||||
|
||||
// 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
|
||||
);
|
||||
@Override
|
||||
public void onMapReady(MapboxMap mapboxMap) {
|
||||
this.mapboxMap = mapboxMap;
|
||||
|
||||
// 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 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 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);
|
||||
// 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
|
||||
@@ -132,13 +151,12 @@ public class SymbolLayerActivity extends AppCompatActivity implements MapboxMap.
|
||||
|
||||
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) {
|
||||
@@ -186,6 +204,9 @@ public class SymbolLayerActivity extends AppCompatActivity implements MapboxMap.
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
if (mapboxMap != null) {
|
||||
mapboxMap.removeOnMapClickListener(this);
|
||||
}
|
||||
mapView.onDestroy();
|
||||
}
|
||||
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -35,5 +35,6 @@
|
||||
"SymbolGeneratorActivity",
|
||||
"TextureViewTransparentBackgroundActivity",
|
||||
"SimpleMapActivity",
|
||||
"RenderTestActivity"
|
||||
"RenderTestActivity",
|
||||
"SymbolLayerActivity"
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user