add collection and style smooth reload example

This commit is contained in:
Łukasz Paczos
2019-11-13 14:30:03 +01:00
parent c396113785
commit 97722e7555
5 changed files with 172 additions and 0 deletions

View File

@@ -484,6 +484,17 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity" />
</activity>
<activity
android:name=".activity.style.CollectionUpdateOnStyleChange"
android:description="@string/description_collection_style_change"
android:label="@string/activity_collection_style_change">
<meta-data
android:name="@string/category"
android:value="@string/category_style" />
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity" />
</activity>
<activity
android:name=".activity.style.FillExtrusionActivity"
android:description="@string/description_fill_extrusion_layer"

View File

@@ -0,0 +1,135 @@
package com.mapbox.mapboxsdk.testapp.activity.style
import android.graphics.Color
import android.os.Bundle
import android.support.design.widget.FloatingActionButton
import android.support.v7.app.AppCompatActivity
import android.widget.Toast
import com.mapbox.geojson.Feature
import com.mapbox.geojson.FeatureCollection
import com.mapbox.geojson.LineString
import com.mapbox.geojson.Point
import com.mapbox.mapboxsdk.geometry.LatLng
import com.mapbox.mapboxsdk.geometry.LatLngBounds
import com.mapbox.mapboxsdk.maps.MapboxMap
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback
import com.mapbox.mapboxsdk.maps.Style
import com.mapbox.mapboxsdk.style.layers.LineLayer
import com.mapbox.mapboxsdk.style.layers.PropertyFactory
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource
import com.mapbox.mapboxsdk.testapp.R
import java.util.*
import kotlinx.android.synthetic.main.activity_collection_update_on_style_change.*
/**
* Test activity that verifies whether the GeoJsonSource transition over style changes can be smooth.
*/
class CollectionUpdateOnStyleChange : AppCompatActivity(), OnMapReadyCallback, Style.OnStyleLoaded {
private lateinit var mapboxMap: MapboxMap
private var currentStyleIndex: Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_collection_update_on_style_change)
mapView.onCreate(savedInstanceState)
mapView.getMapAsync(this)
setupStyleChangeView()
Toast.makeText(
this,
"Make sure that the collection doesn't blink on style change",
Toast.LENGTH_LONG)
.show()
}
override fun onMapReady(map: MapboxMap) {
mapboxMap = map
mapboxMap.setStyle(Style.Builder().fromUri(STYLES[currentStyleIndex]), this)
}
override fun onStyleLoaded(style: Style) {
setupLayer(style)
}
private fun setupLayer(style: Style) {
val source = GeoJsonSource("source", featureCollection)
val lineLayer = LineLayer("layer", "source")
.withProperties(
PropertyFactory.lineColor(Color.RED),
PropertyFactory.lineWidth(10f)
)
style.addSource(source)
style.addLayer(lineLayer)
}
private fun setupStyleChangeView() {
val fabStyles = findViewById<FloatingActionButton>(R.id.fabStyles)
fabStyles.setOnClickListener {
currentStyleIndex++
if (currentStyleIndex == STYLES.size) {
currentStyleIndex = 0
}
mapboxMap.setStyle(Style.Builder().fromUri(STYLES[currentStyleIndex]), this)
}
}
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 onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
mapView.onSaveInstanceState(outState)
}
override fun onDestroy() {
super.onDestroy()
mapView.onDestroy()
}
override fun onLowMemory() {
super.onLowMemory()
mapView.onLowMemory()
}
companion object {
private val STYLES = arrayOf(Style.MAPBOX_STREETS, Style.OUTDOORS, Style.LIGHT, Style.DARK, Style.SATELLITE, Style.SATELLITE_STREETS, Style.TRAFFIC_DAY, Style.TRAFFIC_NIGHT)
private val featureCollection: FeatureCollection
init {
val bounds = LatLngBounds.from(60.0, 100.0, -60.0, -100.0)
val points = ArrayList<Point>()
for (i in 0 until 1000) {
val latLng = getLatLngInBounds(bounds)
points.add(Point.fromLngLat(latLng.longitude, latLng.latitude))
}
featureCollection = FeatureCollection.fromFeature(Feature.fromGeometry(LineString.fromLngLats(points)))
}
private fun getLatLngInBounds(bounds: LatLngBounds): LatLng {
val generator = Random()
val randomLat = bounds.latSouth + generator.nextDouble() * (bounds.latNorth - bounds.latSouth)
val randomLon = bounds.lonWest + generator.nextDouble() * (bounds.lonEast - bounds.lonWest)
return LatLng(randomLat, randomLon)
}
}
}

View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinator_layout"
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="match_parent" />
<android.support.design.widget.FloatingActionButton
android:id="@id/fabStyles"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_layers"
app:backgroundTint="@color/primary"
app:layout_anchorGravity="bottom|end" />
</android.support.design.widget.CoordinatorLayout>

View File

@@ -50,6 +50,7 @@
<string name="description_style_file">Use a local file as the style</string>
<string name="description_map_in_dialog">Display a map inside a dialog fragment</string>
<string name="description_circle_layer">Show bus stops and route in Singapore</string>
<string name="description_collection_style_change">Verify that updating the GeoJsonSource across style changes allows for smooth transitions</string>
<string name="description_url_transform">Transform urls on the fly</string>
<string name="description_cache_management">Control the cache management with FileSource API</string>
<string name="description_restricted_bounds">Limit viewport to Iceland</string>

View File

@@ -34,6 +34,7 @@
<string name="activity_gradient_line">Gradient line layer</string>
<string name="activity_data_driven_style">Data Driven Style</string>
<string name="activity_circle_layer">Circle layer</string>
<string name="activity_collection_style_change">Collection updates on style changes</string>
<string name="activity_style_file">Local Style file</string>
<string name="activity_geojson_clustering">GeoJson Clustering</string>
<string name="activity_geojson_realtime">Add live realtime data</string>