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

Commit

Permalink
[android] - update symbol layer example with changing symbol location
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed Oct 4, 2017
1 parent 42b4818 commit 6a846ce
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
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;
Expand Down Expand Up @@ -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);
Expand All @@ -59,15 +64,15 @@ public void onCreate(Bundle savedInstanceState) {
@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 {
Expand All @@ -76,11 +81,15 @@ private void updateSource(boolean selected) {
}
}

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});
}
Expand Down Expand Up @@ -118,15 +127,31 @@ public void onMapClick(@NonNull LatLng point) {
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");
}
}
});
}

@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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 6a846ce

Please sign in to comment.