[android] test activity for runtime changes to symbols

This commit is contained in:
Ivo van Dongen
2016-11-07 10:51:13 +01:00
committed by Ivo van Dongen
parent 33aa14c939
commit a9249b331a
5 changed files with 263 additions and 1 deletions

View File

@@ -338,7 +338,15 @@
android:label="@string/activity_circle">
<meta-data
android:name="@string/category"
android:value="@string/category_style" />
android:value="@string/category_style"/>
</activity>
<activity
android:name=".activity.style.SymbolLayerActivity"
android:description="@string/description_symbol_layer"
android:label="@string/activity_symbol_layer">
<meta-data
android:name="@string/category"
android:value="@string/category_style"/>
</activity>
<activity
android:name=".activity.style.GeoJsonClusteringActivity"

View File

@@ -0,0 +1,214 @@
package com.mapbox.mapboxsdk.testapp.activity.style;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.PointF;
import android.os.Bundle;
import android.support.annotation.NonNull;
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 com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.style.layers.Layer;
import com.mapbox.mapboxsdk.style.layers.SymbolLayer;
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.services.commons.geojson.Feature;
import com.mapbox.services.commons.geojson.FeatureCollection;
import com.mapbox.services.commons.geojson.Point;
import java.util.Arrays;
import java.util.List;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circleColor;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circleRadius;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconAllowOverlap;
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.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.textSize;
/**
* Example to test runtime manipulation of symbol layers
*/
public class SymbolLayerActivity extends AppCompatActivity implements MapboxMap.OnMapClickListener {
public static final String MARKER_SOURCE = "marker-source";
public static final String MARKER_LAYER = "marker-layer";
private MapboxMap mapboxMap;
private MapView mapView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_symbollayer);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
}
mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(@NonNull final MapboxMap map) {
mapboxMap = map;
//Add a image for the makers
mapboxMap.addImage("my-marker-image", BitmapFactory.decodeResource(SymbolLayerActivity.this.getResources(), R.drawable.mapbox_marker_icon_default));
//Add a source
FeatureCollection markers = FeatureCollection.fromFeatures(new Feature[]{
Feature.fromGeometry(Point.fromCoordinates(new double[]{4.91638, 52.35673}), featureProperties("Marker 1")),
Feature.fromGeometry(Point.fromCoordinates(new double[]{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-marker-image"),
iconAllowOverlap(true),
textField("{title}"),
textColor(Color.RED),
textSize(10f)
)
);
//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 onMapClick(@NonNull LatLng point) {
//Query which features are clicked
PointF screenLoc = mapboxMap.getProjection().toScreenLocation(point);
List<Feature> features = mapboxMap.queryRenderedFeatures(screenLoc, MARKER_LAYER);
SymbolLayer layer = mapboxMap.getLayerAs(MARKER_LAYER);
if (features.size() == 0) {
//Reset
layer.setProperties(iconSize(1f));
} else {
layer.setProperties(iconSize(3f));
}
}
private void toggleTextSize() {
SymbolLayer layer = mapboxMap.getLayerAs(MARKER_LAYER);
layer.setProperties(layer.getTextSize().getValue() > 10 ? textSize(10f) : textSize(20f));
}
private void toggleTextField() {
SymbolLayer layer = mapboxMap.getLayerAs(MARKER_LAYER);
layer.setProperties("{title}".equals(layer.getTextField().getValue()) ? textField("āA") : textField("{title}"));
}
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")) {
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"}));
}
}
private JsonObject featureProperties(String title) {
JsonObject object = new JsonObject();
object.add("title", new JsonPrimitive(title));
return object;
}
@Override
protected void onStart() {
super.onStart();
mapView.onStart();
}
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
@Override
protected void onStop() {
super.onStop();
mapView.onStop();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_symbol_layer, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
case R.id.action_toggle_text_size:
toggleTextSize();
return true;
case R.id.action_toggle_text_field:
toggleTextField();
return true;
case R.id.action_toggle_text_font:
toggleTextFont();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}

View File

@@ -0,0 +1,22 @@
<?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">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/primary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<com.mapbox.mapboxsdk.maps.MapView
android:id="@id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar"
app:mapbox_styleUrl="@string/mapbox_style_mapbox_streets"/>
</RelativeLayout>

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_toggle_text_size"
android:title="Toggle text size"/>
<item
android:id="@+id/action_toggle_text_field"
android:title="Toggle text field contents"/>
<item
android:id="@+id/action_toggle_text_font"
android:title="Toggle text font"/>
</menu>

View File

@@ -68,6 +68,7 @@
<string name="activity_query_rendered_features_box_symbol_count">Count symbols in box</string>
<string name="activity_query_rendered_features_box_highlight">Highlight features in box</string>
<string name="activity_circle">Add Circle</string>
<string name="activity_symbol_layer">Symbols</string>
<string name="activity_add_sprite">Add Custom Sprite</string>
<string name="activity_navigation_drawer">Android SDK View integration</string>
<string name="activity_video_view">Video View</string>
@@ -113,6 +114,7 @@
<string name="description_viewpager">Use SupportMapFragments in a ViewPager</string>
<string name="description_runtime_style">Adopt the map style on the fly</string>
<string name="description_circle">Use GeoJson source to show a circle</string>
<string name="description_symbol_layer">Manipulate symbols at runtime</string>
<string name="description_custom_sprite">Use a custom sprite in a Symbol Layer</string>
<string name="description_geojson_clustering">Use GeoJson sources and dynamic layers to cluster information</string>
<string name="description_geojson_realtime">Use realtime GeoJSON data streams to move a symbol on your map</string>