From 02b5e60f8678fbbc8c4bbf526b1c9092632cd603 Mon Sep 17 00:00:00 2001 From: Tobrun Date: Tue, 24 Mar 2020 12:52:54 +0100 Subject: [PATCH] [testapp] - add example using within expression (#288) --- .../src/main/AndroidManifest.xml | 11 + .../activity/turf/WithinExpressionActivity.kt | 268 ++++++++++++++++++ .../res/layout/activity_within_expression.xml | 13 + .../src/main/res/values/descriptions.xml | 1 + .../src/main/res/values/titles.xml | 1 + 5 files changed, 294 insertions(+) create mode 100644 MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/turf/WithinExpressionActivity.kt create mode 100644 MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_within_expression.xml diff --git a/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml b/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml index ee239a6d5..cb9d37e4e 100644 --- a/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml +++ b/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml @@ -998,6 +998,17 @@ android:name="android.support.PARENT_ACTIVITY" android:value=".activity.FeatureOverviewActivity" /> + + + + + mapboxMap = map + + // Setup camera position above Georgetown + mapboxMap.cameraPosition = CameraPosition.Builder() + .target(LatLng(38.90628988399711, -77.06574689337494)) + .zoom(15.5) + .build() + + // Wait for the map to become idle before manipulating the style and camera of the map + mapView.addOnDidBecomeIdleListener(object : MapView.OnDidBecomeIdleListener { + override fun onDidBecomeIdle() { + handler.postDelayed(runnable, 2500) + mapView.removeOnDidBecomeIdleListener(this) + } + }) + + // Load mapbox streets and add lines and circles + setupStyle() + } + } + + private fun setupStyle() { + // Assume the route is represented by an array of coordinates. + val coordinates = listOf( + Point.fromLngLat( + -77.06866264343262, + 38.90506061276737 + ), + Point.fromLngLat( + -77.06283688545227, + 38.905194197410545 + ), + Point.fromLngLat( + -77.06285834312439, + 38.906429843444094 + ), + Point.fromLngLat( + -77.0630407333374, + 38.90680554236621 + ) + ) + + // Convert to a LineString + val routeLineString = LineString.fromLngLats(coordinates) + + // Create buffer around linestring + val bufferedRouteGeometry = bufferLineStringGeometry(routeLineString) + + // Setup style with additional layers, + // using Style.MAPBOX_STREETS as a base style + mapboxMap.setStyle( + Style.Builder() + .fromUri(Style.MAPBOX_STREETS) + .withSources( + GeoJsonSource( + POINT_ID, LineString.fromLngLats(coordinates) + ), + GeoJsonSource( + FILL_ID, + FeatureCollection.fromFeature(Feature.fromGeometry(bufferedRouteGeometry)), + GeoJsonOptions().withBuffer(0).withTolerance(0.0f) + ) + ) + .withLayerBelow( + LineLayer(LINE_ID, POINT_ID) + .withProperties(lineWidth(7.5f), lineColor(Color.LTGRAY)), + "poi-label" + ) + .withLayerBelow( + CircleLayer(POINT_ID, POINT_ID) + .withProperties( + circleRadius(7.5f), + circleColor(Color.DKGRAY), + circleOpacity(0.75f) + ), "poi-label" + ) + ) + } + + private fun optimizeStyle() { + val style = mapboxMap.style!! + + // Add fill layer to represent buffered LineString + mapboxMap.style!!.addLayerBelow( + FillLayer(FILL_ID, FILL_ID) + .withProperties( + fillOpacity(0.12f), + fillColor(Color.YELLOW) + ), LINE_ID + ) + + // Move to a new camera position + mapboxMap.easeCamera( + CameraUpdateFactory.newCameraPosition( + CameraPosition.Builder() + .zoom(16.0) + .target(LatLng(38.905156245642814, -77.06535338052844)) + .bearing(80.68015859462369) + .tilt(55.0) + .build() + ), 1750 + ) + + // Show only POI labels inside geometry using within expression + val symbolLayer = style.getLayer("poi-label") as SymbolLayer + symbolLayer.setFilter( + within( + bufferLineStringGeometry() + ) + ) + + // Hide other types of labels to highlight POI labels + (style.getLayer("road-label") as SymbolLayer).setProperties(visibility(NONE)) + (style.getLayer("transit-label") as SymbolLayer).setProperties(visibility(NONE)) + (style.getLayer("road-number-shield") as SymbolLayer).setProperties(visibility(NONE)) + } + + override fun onStart() { + super.onStart() + mapView.onStart() + } + + override fun onResume() { + super.onResume() + mapView.onResume() + } + + override fun onPause() { + super.onPause() + mapView.onPause() + } + + override fun onStop() { + super.onStop() + mapView.onStop() + } + + override fun onLowMemory() { + super.onLowMemory() + mapView.onLowMemory() + } + + override fun onDestroy() { + super.onDestroy() + handler.removeCallbacks(runnable) + mapView.onDestroy() + } + + override fun onSaveInstanceState(outState: Bundle?, outPersistentState: PersistableBundle?) { + super.onSaveInstanceState(outState, outPersistentState) + outState?.let { + mapView.onSaveInstanceState(it) + } + } + + private fun bufferLineStringGeometry(lineString: LineString? = null): Polygon { + // TODO replace static data by Turf#Buffer: mapbox-java/issues/987 + return FeatureCollection.fromJson( + """ + { + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [ + [ + [ + -77.06867337226866, + 38.90467655551809 + ], + [ + -77.06233263015747, + 38.90479344272695 + ], + [ + -77.06234335899353, + 38.906463238984344 + ], + [ + -77.06290125846863, + 38.907206285691615 + ], + [ + -77.06364154815674, + 38.90684728656818 + ], + [ + -77.06326603889465, + 38.90637140121084 + ], + [ + -77.06321239471436, + 38.905561553883246 + ], + [ + -77.0691454410553, + 38.905436318935635 + ], + [ + -77.06912398338318, + 38.90466820642439 + ], + [ + -77.06867337226866, + 38.90467655551809 + ] + ] + ] + } + } + ] + } + """.trimIndent() + ).features()!![0].geometry() as Polygon + } + + companion object { + const val POINT_ID = "point" + const val FILL_ID = "fill" + const val LINE_ID = "line" + } +} \ No newline at end of file diff --git a/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_within_expression.xml b/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_within_expression.xml new file mode 100644 index 000000000..de8abb502 --- /dev/null +++ b/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_within_expression.xml @@ -0,0 +1,13 @@ + + + + + + \ No newline at end of file diff --git a/MapboxGLAndroidSDKTestApp/src/main/res/values/descriptions.xml b/MapboxGLAndroidSDKTestApp/src/main/res/values/descriptions.xml index 075949be5..32ca4de17 100644 --- a/MapboxGLAndroidSDKTestApp/src/main/res/values/descriptions.xml +++ b/MapboxGLAndroidSDKTestApp/src/main/res/values/descriptions.xml @@ -19,6 +19,7 @@ Debug Mode Offline Map example Download an offline region. + Usage of the within expression on a buffered geometry Update metadata example Delete region example Change resources cache path example diff --git a/MapboxGLAndroidSDKTestApp/src/main/res/values/titles.xml b/MapboxGLAndroidSDKTestApp/src/main/res/values/titles.xml index 0a0166945..0089648ff 100644 --- a/MapboxGLAndroidSDKTestApp/src/main/res/values/titles.xml +++ b/MapboxGLAndroidSDKTestApp/src/main/res/values/titles.xml @@ -87,4 +87,5 @@ Performance Measurement Physical Unit Circle Download region + Within expression \ No newline at end of file