From b75ea63299e7cd5ad46509ee5619772f97abbd56 Mon Sep 17 00:00:00 2001 From: tobrun Date: Wed, 22 May 2019 11:09:57 +0200 Subject: [android] - test query line bug --- .../activity/maplayout/SimpleMapActivity.java | 163 +++++++++++++++------ .../src/main/res/layout/activity_map_simple.xml | 21 ++- 2 files changed, 133 insertions(+), 51 deletions(-) diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/maplayout/SimpleMapActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/maplayout/SimpleMapActivity.java index 5c4dd90710..5c63850cd5 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/maplayout/SimpleMapActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/maplayout/SimpleMapActivity.java @@ -1,63 +1,150 @@ package com.mapbox.mapboxsdk.testapp.activity.maplayout; + +import android.graphics.Color; +import android.graphics.RectF; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v7.app.AppCompatActivity; -import android.view.MenuItem; +import android.util.Log; +import android.view.View; +import android.widget.Button; +import android.widget.Toast; +import com.mapbox.android.gestures.MoveGestureDetector; +import com.mapbox.geojson.Feature; +import com.mapbox.geojson.FeatureCollection; +import com.mapbox.mapboxsdk.Mapbox; 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.maps.Style; -import com.mapbox.mapboxsdk.style.layers.SymbolLayer; +import com.mapbox.mapboxsdk.style.layers.LineLayer; +import com.mapbox.mapboxsdk.style.sources.GeoJsonSource; import com.mapbox.mapboxsdk.testapp.R; -import com.mapbox.mapboxsdk.testapp.utils.NavUtils; -import static com.mapbox.mapboxsdk.style.layers.Property.TEXT_ANCHOR_RIGHT; -import static com.mapbox.mapboxsdk.style.layers.Property.TEXT_ANCHOR_TOP; -import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textVariableAnchor; +import java.util.List; + +import static com.mapbox.mapboxsdk.style.expressions.Expression.exponential; +import static com.mapbox.mapboxsdk.style.expressions.Expression.interpolate; +import static com.mapbox.mapboxsdk.style.expressions.Expression.stop; +import static com.mapbox.mapboxsdk.style.expressions.Expression.zoom; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineColor; +import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineOpacity; /** - * Test activity showcasing a simple MapView without any MapboxMap interaction. + * Use MapboxMap.queryRenderedFeatures() to find and highlight certain features within the map viewport. + * The search button re-appears when the map is moved. */ -public class SimpleMapActivity extends AppCompatActivity { +public class SimpleMapActivity extends AppCompatActivity implements OnMapReadyCallback, + MapboxMap.OnMoveListener { + private static final String FILL_LAYER_ID = "FILL_LAYER_ID"; + private static final String GEO_JSON_SOURCE_ID = "GEO_JSON_SOURCE_ID"; + private static final String ID_OF_LAYER_TO_HIGHLIGHT = "road-street"; private MapView mapView; + private MapboxMap mapboxMap; + private GeoJsonSource dataGeoJsonSource; + private Button redoSearchButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + + // This contains the MapView in XML and needs to be called after the access token is configured. setContentView(R.layout.activity_map_simple); + + redoSearchButton = findViewById(R.id.redo_search_button); mapView = findViewById(R.id.mapView); mapView.onCreate(savedInstanceState); - mapView.getMapAsync(mapboxMap -> { - mapboxMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(34.054079, -118.243012), 18)); - mapboxMap.setStyle( - new Style.Builder().fromUrl(Style.MAPBOX_STREETS), style -> { - SymbolLayer symbolLayer = style.getLayerAs("poi-label"); - symbolLayer.setProperties(textVariableAnchor(new String[] {TEXT_ANCHOR_TOP, TEXT_ANCHOR_RIGHT})); - } - ); + mapView.getMapAsync(this); + } + + @Override + public void onMapReady(@NonNull final MapboxMap mapboxMap) { + this.mapboxMap = mapboxMap; + mapboxMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(34.013332, -118.312881 + ),16)); + mapboxMap.setStyle(Style.DARK, new Style.OnStyleLoaded() { + @Override + public void onStyleLoaded(@NonNull final Style style) { + + mapboxMap.addOnMoveListener(SimpleMapActivity.this); + + style.addSource(new GeoJsonSource(GEO_JSON_SOURCE_ID, + FeatureCollection.fromFeatures(new Feature[] {}))); + + style.addLayer(new LineLayer(FILL_LAYER_ID, + GEO_JSON_SOURCE_ID).withProperties( + lineOpacity(interpolate(exponential(1f), zoom(), + stop(3, 0f), + stop(8, .5f), + stop(15f, 1f))), + lineColor(Color.parseColor("#00F7FF")) + )); + + mapboxMap.addOnCameraMoveListener(new MapboxMap.OnCameraMoveListener() { + @Override + public void onCameraMove() { + Toast.makeText(SimpleMapActivity.this, "onCameraMove", Toast.LENGTH_LONG).show(); + FeatureCollection featureCollection = null; + if (style.getLayer(ID_OF_LAYER_TO_HIGHLIGHT) != null) { + Log.v("test", "id of layer to highlight is " + ID_OF_LAYER_TO_HIGHLIGHT); + featureCollection = FeatureCollection.fromFeatures(getFeaturesInViewport(ID_OF_LAYER_TO_HIGHLIGHT)); + } else { + Toast.makeText(SimpleMapActivity.this, + "Layer not found ", + Toast.LENGTH_SHORT).show(); + } + // Retrieve and update the GeoJSON source used in the FillLayer + dataGeoJsonSource = style.getSourceAs(GEO_JSON_SOURCE_ID); + if (dataGeoJsonSource != null && featureCollection != null) { + dataGeoJsonSource.setGeoJson(featureCollection); + } + redoSearchButton.setVisibility(View.INVISIBLE); + } + }); + } }); } +// }); +// } + + /** + * Perform feature query within the viewport. + */ + private List getFeaturesInViewport(String layerName) { + RectF rectF = new RectF(mapView.getLeft(), + mapView.getTop(), mapView.getRight(), mapView.getBottom()); + return mapboxMap.queryRenderedFeatures(rectF, layerName); + } @Override - protected void onStart() { - super.onStart(); - mapView.onStart(); + public void onMoveEnd(MoveGestureDetector detector) { + redoSearchButton.setVisibility(View.VISIBLE); + } + + @Override + public void onMoveBegin(MoveGestureDetector detector) { + // Left empty on purpose + } + + @Override + public void onMove(MoveGestureDetector detector) { + Log.v("mapMove", "the map is moving and detector is " + detector); } @Override - protected void onResume() { + public void onResume() { super.onResume(); mapView.onResume(); } @Override - protected void onPause() { - super.onPause(); - mapView.onPause(); + protected void onStart() { + super.onStart(); + mapView.onStart(); } @Override @@ -66,6 +153,12 @@ public class SimpleMapActivity extends AppCompatActivity { mapView.onStop(); } + @Override + public void onPause() { + super.onPause(); + mapView.onPause(); + } + @Override public void onLowMemory() { super.onLowMemory(); @@ -75,6 +168,9 @@ public class SimpleMapActivity extends AppCompatActivity { @Override protected void onDestroy() { super.onDestroy(); + if (mapboxMap != null) { + mapboxMap.removeOnMoveListener(this); + } mapView.onDestroy(); } @@ -83,23 +179,4 @@ public class SimpleMapActivity extends AppCompatActivity { super.onSaveInstanceState(outState); mapView.onSaveInstanceState(outState); } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: - // activity uses singleInstance for testing purposes - // code below provides a default navigation when using the app - onBackPressed(); - return true; - } - return super.onOptionsItemSelected(item); - } - - @Override - public void onBackPressed() { - // activity uses singleInstance for testing purposes - // code below provides a default navigation when using the app - NavUtils.navigateHome(this); - } -} +} \ No newline at end of file diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_map_simple.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_map_simple.xml index e67740ad54..4a812a4cee 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_map_simple.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_map_simple.xml @@ -1,14 +1,19 @@ + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:orientation="vertical" + tools:context=".activity.maplayout.SimpleMapActivity"> + android:id="@id/mapView" + android:layout_above="@+id/redo_search_button" + android:layout_width="match_parent" + android:layout_height="match_parent"/> + +