Add getLayer and getSource snapshotter methods (#292)

* Expose snapshotter layer and source objects

* Add example snapshotter + within activity

* Bump vendor/mapbox-gl-native
This commit is contained in:
Alexander Shalamov
2020-03-25 07:42:23 -04:00
committed by GitHub
parent da6d8a2b06
commit 6984dbf155
7 changed files with 380 additions and 1 deletions

View File

@@ -396,6 +396,17 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity" />
</activity>
<activity
android:name=".activity.turf.MapSnapshotterWithinExpression"
android:description="@string/description_snapshot_within"
android:label="@string/activity_snapshot_within">
<meta-data
android:name="@string/category"
android:value="@string/category_imagegenerator" />
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity" />
</activity>
<activity
android:name=".activity.maplayout.DoubleMapActivity"
android:description="@string/description_doublemap"

View File

@@ -0,0 +1,274 @@
package com.mapbox.mapboxsdk.testapp.activity.turf
import android.graphics.Color
import android.os.Bundle
import android.os.PersistableBundle
import androidx.appcompat.app.AppCompatActivity
import com.mapbox.geojson.*
import com.mapbox.mapboxsdk.camera.CameraPosition
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.Style
import com.mapbox.mapboxsdk.snapshotter.MapSnapshotter
import com.mapbox.mapboxsdk.style.expressions.Expression.within
import com.mapbox.mapboxsdk.style.layers.CircleLayer
import com.mapbox.mapboxsdk.style.layers.FillLayer
import com.mapbox.mapboxsdk.style.layers.LineLayer
import com.mapbox.mapboxsdk.style.layers.Property.NONE
import com.mapbox.mapboxsdk.style.layers.PropertyFactory.*
import com.mapbox.mapboxsdk.style.layers.SymbolLayer
import com.mapbox.mapboxsdk.style.sources.GeoJsonOptions
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource
import com.mapbox.mapboxsdk.testapp.R
import kotlinx.android.synthetic.main.activity_physical_circle.mapView
import kotlinx.android.synthetic.main.activity_snapshot.*
/**
* An Activity that showcases the use of MapSnapshotter with 'within' expression
*/
class MapSnapshotterWithinExpression : AppCompatActivity() {
private lateinit var mapboxMap: MapboxMap
private lateinit var snapshotter: MapSnapshotter
private var snapshotInProgress = false
private val cameraListener = object : MapView.OnCameraDidChangeListener {
override fun onCameraDidChange(animated: Boolean) {
if (!snapshotInProgress) {
snapshotInProgress = true
snapshotter.setCameraPosition(mapboxMap.cameraPosition)
snapshotter.start {
imageView.setImageBitmap(it.bitmap)
snapshotInProgress = false
}
}
}
}
private val snapshotterObserver = object : MapSnapshotter.Observer {
override fun onDidFinishLoadingStyle() {
// Show only POI labels inside geometry using within expression
(snapshotter.getLayer("poi-label") as SymbolLayer).setFilter(
within(
bufferLineStringGeometry()
)
)
// Hide other types of labels to highlight POI labels
(snapshotter.getLayer("road-label") as SymbolLayer).setProperties(visibility(NONE))
(snapshotter.getLayer("transit-label") as SymbolLayer).setProperties(visibility(NONE))
(snapshotter.getLayer("road-number-shield") as SymbolLayer).setProperties(visibility(NONE))
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_mapsnapshotter_within_expression)
mapView.onCreate(savedInstanceState)
mapView.getMapAsync { map ->
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() {
mapboxMap.easeCamera(
CameraUpdateFactory.newCameraPosition(
CameraPosition.Builder()
.zoom(16.0)
.target(LatLng(38.905156245642814, -77.06535338052844))
.bearing(80.68015859462369)
.tilt(55.0)
.build()
), 1000
)
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>(
Point.fromLngLat(
-77.06866264343262,
38.90506061276737
),
Point.fromLngLat(
-77.06283688545227,
38.905194197410545
),
Point.fromLngLat(
-77.06285834312439,
38.906429843444094
),
Point.fromLngLat(
-77.0630407333374,
38.90680554236621
)
)
// Setup style with additional layers,
// using Style.MAPBOX_STREETS as a base style
mapboxMap.setStyle(
Style.Builder()
.fromUri(Style.MAPBOX_STREETS)
) {
mapView.addOnCameraDidChangeListener(cameraListener)
}
val options = MapSnapshotter.Options(imageView.measuredWidth / 2, imageView.measuredHeight / 2)
.withCameraPosition(mapboxMap.cameraPosition)
.withPixelRatio(2.0f)
.withStyleBuilder(Style.Builder()
.fromUri(Style.MAPBOX_STREETS)
.withSources(
GeoJsonSource(
POINT_ID, LineString.fromLngLats(coordinates)
),
GeoJsonSource(
FILL_ID,
FeatureCollection.fromFeature(Feature.fromGeometry(bufferLineStringGeometry())),
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"
).withLayerBelow(
FillLayer(FILL_ID, FILL_ID)
.withProperties(
fillOpacity(0.12f),
fillColor(Color.YELLOW)
), LINE_ID
))
snapshotter = MapSnapshotter(this, options)
snapshotter.setObserver(snapshotterObserver)
}
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()
mapView.onDestroy()
}
override fun onSaveInstanceState(outState: Bundle?, outPersistentState: PersistableBundle?) {
super.onSaveInstanceState(outState, outPersistentState)
outState?.let {
mapView.onSaveInstanceState(it)
}
}
private fun bufferLineStringGeometry(): 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"
}
}

View File

@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.style.DraggableMarkerActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="313dp">
</com.mapbox.mapboxsdk.maps.MapView>
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/primary"
android:contentDescription="@null" />
</LinearLayout>
</RelativeLayout>

View File

@@ -28,6 +28,7 @@
<string name="description_polygon">Add a polygon to a map</string>
<string name="description_scroll_by">Scroll with pixels in x,y direction</string>
<string name="description_snapshot">Example to make a snapshot of the map</string>
<string name="description_snapshot_within">Example of snapshotter with within expression</string>
<string name="description_doublemap">2 maps in a view hierarchy</string>
<string name="description_dynamic_info_window_adapter">Learn how to create a dynamic custom InfoWindow</string>
<string name="description_viewpager">Use SupportMapFragments in a ViewPager</string>

View File

@@ -21,6 +21,7 @@
<string name="activity_scroll_by">Scroll By Method</string>
<string name="activity_double_map">Double Map Activity</string>
<string name="activity_snapshot">Snapshot Activity</string>
<string name="activity_snapshot_within">Snapshot with within expression</string>
<string name="activity_custom_layer">Custom Layer</string>
<string name="activity_map_padding">Map Padding</string>
<string name="activity_debug_mode">Debug Mode</string>