[android] - update symbol layer example with changing symbol location
This commit is contained in:
committed by
Tobrun
parent
42b4818f16
commit
6a846ce5a8
@@ -4,6 +4,8 @@ import android.graphics.PointF;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||
@@ -48,6 +50,9 @@ public class ZoomFunctionSymbolLayerActivity extends AppCompatActivity {
|
||||
private MapboxMap mapboxMap;
|
||||
private GeoJsonSource source;
|
||||
|
||||
private boolean isInitialPosition = true;
|
||||
private boolean isSelected = false;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
@@ -59,15 +64,15 @@ public class ZoomFunctionSymbolLayerActivity extends AppCompatActivity {
|
||||
@Override
|
||||
public void onMapReady(@NonNull final MapboxMap map) {
|
||||
mapboxMap = map;
|
||||
updateSource(false);
|
||||
updateSource();
|
||||
addLayer();
|
||||
addMapClickListener();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void updateSource(boolean selected) {
|
||||
FeatureCollection featureCollection = createFeatureCollection(selected);
|
||||
private void updateSource() {
|
||||
FeatureCollection featureCollection = createFeatureCollection();
|
||||
if (source != null) {
|
||||
source.setGeoJson(featureCollection);
|
||||
} else {
|
||||
@@ -76,11 +81,15 @@ public class ZoomFunctionSymbolLayerActivity extends AppCompatActivity {
|
||||
}
|
||||
}
|
||||
|
||||
private FeatureCollection createFeatureCollection(boolean selected) {
|
||||
Point point = Point.fromCoordinates(Position.fromCoordinates(-74.016181, 40.701745));
|
||||
private FeatureCollection createFeatureCollection() {
|
||||
Position position = isInitialPosition
|
||||
? Position.fromCoordinates(-74.01618140, 40.701745)
|
||||
: Position.fromCoordinates(-73.988097, 40.749864);
|
||||
|
||||
Point point = Point.fromCoordinates(position);
|
||||
Feature feature = Feature.fromGeometry(point);
|
||||
JsonObject properties = new JsonObject();
|
||||
properties.addProperty(KEY_PROPERTY_SELECTED, selected);
|
||||
properties.addProperty(KEY_PROPERTY_SELECTED, isSelected);
|
||||
feature.setProperties(properties);
|
||||
return FeatureCollection.fromFeatures(new Feature[] {feature});
|
||||
}
|
||||
@@ -118,8 +127,9 @@ public class ZoomFunctionSymbolLayerActivity extends AppCompatActivity {
|
||||
List<Feature> featureList = mapboxMap.queryRenderedFeatures(screenPoint, LAYER_ID);
|
||||
if (!featureList.isEmpty()) {
|
||||
Feature feature = featureList.get(0);
|
||||
boolean isSelected = feature.getBooleanProperty(KEY_PROPERTY_SELECTED);
|
||||
updateSource(!isSelected);
|
||||
boolean selectedNow = feature.getBooleanProperty(KEY_PROPERTY_SELECTED);
|
||||
isSelected = !selectedNow;
|
||||
updateSource();
|
||||
} else {
|
||||
Timber.e("No features found");
|
||||
}
|
||||
@@ -127,6 +137,21 @@ public class ZoomFunctionSymbolLayerActivity extends AppCompatActivity {
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
getMenuInflater().inflate(R.menu.menu_symbols, menu);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
if (mapboxMap != null && item.getItemId() == R.id.menu_action_change_location) {
|
||||
isInitialPosition = !isInitialPosition;
|
||||
updateSource();
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
<item
|
||||
android:id="@+id/menu_action_change_location"
|
||||
android:title="@string/menuitem_change_location"
|
||||
app:showAsAction="never"/>
|
||||
</menu>
|
||||
@@ -11,6 +11,7 @@
|
||||
<string name="menuitem_title_change_location_source_mock">Change to mock location source</string>
|
||||
<string name="menuitem_title_change_location_source_null">Reset location source to null</string>
|
||||
<string name="menuitem_change_icon_overlap">Toggle icon overlap</string>
|
||||
<string name="menuitem_change_location">Change location</string>
|
||||
<string name="button_camera_move">Move</string>
|
||||
<string name="button_camera_ease">Ease</string>
|
||||
<string name="button_camera_animate">Animate</string>
|
||||
|
||||
Reference in New Issue
Block a user