Guide to using GeoJsonDataSource (#2977)

Co-authored-by: Bart Louwers <bart@emeel.net>
This commit is contained in:
Dilshodbek Jumabaev
2024-11-28 22:32:17 +05:00
committed by GitHub
parent 4878005ffe
commit 8b32c9a783
9 changed files with 155 additions and 4 deletions

View File

@@ -43,6 +43,7 @@ class QuerySourceFeaturesActivity : AppCompatActivity() {
}
private fun initStyle(style: Style) {
// # --8<-- [start:JsonObject]
val properties = JsonObject()
properties.addProperty("key1", "value1")
val source = GeoJsonSource(
@@ -62,6 +63,7 @@ class QuerySourceFeaturesActivity : AppCompatActivity() {
val layer = CircleLayer("test-layer", source.id)
.withFilter(visible)
style.addLayer(layer)
// # --8<-- [end:JsonObject]
// Add a click listener
maplibreMap.addOnMapClickListener { point: LatLng? ->

View File

@@ -56,6 +56,7 @@ class CollectionUpdateOnStyleChange : AppCompatActivity(), OnMapReadyCallback, S
}
private fun setupLayer(style: Style) {
// # --8<-- [start:setupLayer]
val source = GeoJsonSource("source", featureCollection)
val lineLayer = LineLayer("layer", "source")
.withProperties(
@@ -65,6 +66,7 @@ class CollectionUpdateOnStyleChange : AppCompatActivity(), OnMapReadyCallback, S
style.addSource(source)
style.addLayer(lineLayer)
// # --8<-- [end:setupLayer]
}
private fun setupStyleChangeView() {

View File

@@ -45,10 +45,11 @@ class HeatmapLayerActivity : AppCompatActivity() {
}
)
}
// # --8<-- [start:createEarthquakeSource]
private fun createEarthquakeSource(): GeoJsonSource {
return GeoJsonSource(EARTHQUAKE_SOURCE_ID, URI(EARTHQUAKE_SOURCE_URL))
}
// # --8<-- [end:createEarthquakeSource]
private fun createHeatmapLayer(): HeatmapLayer {
val layer = HeatmapLayer(HEATMAP_LAYER_ID, EARTHQUAKE_SOURCE_ID)
@@ -188,6 +189,7 @@ class HeatmapLayerActivity : AppCompatActivity() {
mapView.onDestroy()
}
// # --8<-- [start:constants]
companion object {
private const val EARTHQUAKE_SOURCE_URL =
"https://maplibre.org/maplibre-gl-js-docs/assets/earthquakes.geojson"
@@ -196,4 +198,5 @@ class HeatmapLayerActivity : AppCompatActivity() {
private const val HEATMAP_LAYER_SOURCE = "earthquakes"
private const val CIRCLE_LAYER_ID = "earthquakes-circle"
}
// # --8<-- [end:constants]
}

View File

@@ -29,7 +29,7 @@ class NoStyleActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
binding = ActivityMapSimpleBinding.inflate(layoutInflater)
setContentView(binding.root)
// # --8<-- [start:setup]
binding.mapView.getMapAsync { map ->
map.moveCamera(CameraUpdateFactory.newLatLngZoom(cameraTarget, cameraZoom))
map.setStyle(
@@ -39,6 +39,7 @@ class NoStyleActivity : AppCompatActivity() {
.withLayer(SymbolLayer(layerId, sourceId).withProperties(iconImage(imageId)))
)
}
// # --8<-- [end:setup]
}
override fun onStart() {

View File

@@ -329,6 +329,7 @@ class RuntimeStyleActivity : AppCompatActivity() {
private fun addParksLayer() {
// Add a source
// # --8<-- [start:source]
val source: Source = try {
GeoJsonSource("amsterdam-spots", ResourceUtils.readRawResource(this, R.raw.amsterdam))
} catch (ioException: IOException) {
@@ -347,6 +348,7 @@ class RuntimeStyleActivity : AppCompatActivity() {
PropertyFactory.fillOpacity(0.3f),
PropertyFactory.fillAntialias(true)
)
// # --8<-- [end:source]
// Only show me parks (except westerpark with stroke-width == 3)
layer.setFilter(

View File

@@ -61,6 +61,7 @@ class ZoomFunctionSymbolLayerActivity : AppCompatActivity() {
}
}
// # --8<-- [start:updateSource]
private fun updateSource(style: Style?) {
val featureCollection = createFeatureCollection()
if (source != null) {
@@ -70,6 +71,7 @@ class ZoomFunctionSymbolLayerActivity : AppCompatActivity() {
style!!.addSource(source!!)
}
}
// # --8<-- [end:updateSource]
private fun toggleSymbolLayerVisibility() {
layer!!.setProperties(
@@ -78,6 +80,7 @@ class ZoomFunctionSymbolLayerActivity : AppCompatActivity() {
isShowingSymbolLayer = !isShowingSymbolLayer
}
// # --8<-- [start:createFeatureCollection]
private fun createFeatureCollection(): FeatureCollection {
val point = if (isInitialPosition) {
Point.fromLngLat(-74.01618140, 40.701745)
@@ -89,6 +92,7 @@ class ZoomFunctionSymbolLayerActivity : AppCompatActivity() {
val feature = Feature.fromGeometry(point, properties)
return FeatureCollection.fromFeatures(arrayOf(feature))
}
// # --8<-- [end:createFeatureCollection]
private fun addLayer(style: Style) {
layer = SymbolLayer(LAYER_ID, SOURCE_ID)

View File

@@ -187,9 +187,9 @@ class MapSnapshotterWithinExpression : AppCompatActivity() {
super.onSaveInstanceState(outState, outPersistentState)
binding.mapView.onSaveInstanceState(outState)
}
private fun bufferLineStringGeometry(): Polygon {
private fun bufferLineStringGeometry(): Polygon {
// TODO replace static data by Turf#Buffer: mapbox-java/issues/987
// # --8<-- [start:fromJson]
return FeatureCollection.fromJson(
"""
{
@@ -250,6 +250,7 @@ class MapSnapshotterWithinExpression : AppCompatActivity() {
}
""".trimIndent()
).features()!![0].geometry() as Polygon
// # --8<-- [end:fromJson]
}
companion object {

View File

@@ -7,6 +7,7 @@ import java.io.*
object ResourceUtils {
@JvmStatic
// # --8<-- [start:readRawResource]
fun readRawResource(context: Context?, @RawRes rawResource: Int): String {
var json = ""
if (context != null) {
@@ -23,6 +24,7 @@ object ResourceUtils {
}
return json
}
// # --8<-- [end:readRawResource]
fun convertDpToPx(context: Context, dp: Float): Float {
return TypedValue.applyDimension(