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

Commit

Permalink
[android] - example to render symbols offline with resources loaded f…
Browse files Browse the repository at this point in the history
…rom assets
  • Loading branch information
tobrun committed Aug 20, 2018
1 parent 1a664a8 commit bce2e74
Show file tree
Hide file tree
Showing 9 changed files with 12,536 additions and 66 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -9,100 +9,127 @@
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.testapp.utils.ResourceUtils;
import com.mapbox.mapboxsdk.utils.BitmapUtils;
import timber.log.Timber;


import java.util.Arrays;
import java.io.IOException;
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
* loads the font 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);
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
try {
// Create map configuration
MapboxMapOptions mapboxMapOptions = new MapboxMapOptions();
mapboxMapOptions.camera(new CameraPosition.Builder().target(
new LatLng(52.35273, 4.91638))
.zoom(13)
.build()
);
mapboxMapOptions.styleJson(ResourceUtils.readRawResource(this, R.raw.streets));

// 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);
} catch (IOException exception) {
Timber.e(exception);
}
}

// 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);
@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 +159,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 +212,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>
Loading

0 comments on commit bce2e74

Please sign in to comment.