Skip to content

Commit 5257385

Browse files
committed
extract event layer setup
1 parent 1e0ffbc commit 5257385

2 files changed

Lines changed: 99 additions & 95 deletions

File tree

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package org.btcmap.map
2+
3+
import android.graphics.Color
4+
import org.btcmap.map.MapFragment.Companion.ICON_OFFSET_Y
5+
import org.maplibre.android.style.expressions.Expression
6+
import org.maplibre.android.style.layers.CircleLayer
7+
import org.maplibre.android.style.layers.Layer
8+
import org.maplibre.android.style.layers.Property.ICON_ANCHOR_CENTER
9+
import org.maplibre.android.style.layers.PropertyFactory
10+
import org.maplibre.android.style.layers.SymbolLayer
11+
import org.maplibre.android.style.sources.GeoJsonOptions
12+
import org.maplibre.android.style.sources.GeoJsonSource
13+
14+
fun createEventLayers(
15+
markerBackgroundColor: Int,
16+
usingOpenFreeMap: Boolean,
17+
): Pair<GeoJsonSource, List<Layer>> {
18+
val source = GeoJsonSource(
19+
id = "event",
20+
geoJson = """{"type":"FeatureCollection","features":[]}""",
21+
options = GeoJsonOptions().withCluster(true).withClusterMaxZoom(14).withClusterRadius(30),
22+
)
23+
24+
val clusterBackground by lazy {
25+
CircleLayer("event_cluster_background", source.id).apply {
26+
setProperties(
27+
PropertyFactory.circleColor(markerBackgroundColor),
28+
PropertyFactory.circleRadius(23f),
29+
)
30+
val pointCount = Expression.toNumber(Expression.get("point_count"))
31+
setFilter(
32+
Expression.all(
33+
Expression.has("point_count"),
34+
Expression.gte(
35+
pointCount,
36+
Expression.literal(1)
37+
)
38+
)
39+
)
40+
}
41+
}
42+
43+
val clusterCount =
44+
SymbolLayer("event_cluster_count", source.id).apply {
45+
if (usingOpenFreeMap) {
46+
setProperties(PropertyFactory.textFont(arrayOf("Noto Sans Regular")))
47+
}
48+
setProperties(
49+
PropertyFactory.textField(Expression.toString(Expression.get("point_count"))),
50+
PropertyFactory.textSize(16f),
51+
PropertyFactory.textColor(Color.WHITE),
52+
)
53+
}
54+
55+
val eventMarker =
56+
SymbolLayer("event_marker", source.id).apply {
57+
setProperties(
58+
PropertyFactory.iconImage("btcmap-marker"),
59+
PropertyFactory.iconAnchor(Expression.literal("bottom")),
60+
PropertyFactory.iconAllowOverlap(true),
61+
PropertyFactory.iconIgnorePlacement(true)
62+
)
63+
setFilter(
64+
Expression.neq(Expression.get("cluster"), true)
65+
)
66+
}
67+
68+
val eventIcon =
69+
SymbolLayer("event_icon", source.id).apply {
70+
setProperties(
71+
PropertyFactory.iconImage("marker-icon-event"),
72+
PropertyFactory.iconAnchor(ICON_ANCHOR_CENTER),
73+
PropertyFactory.iconOffset(
74+
arrayOf(
75+
0f,
76+
ICON_OFFSET_Y
77+
)
78+
),
79+
PropertyFactory.iconAllowOverlap(true),
80+
PropertyFactory.iconIgnorePlacement(true)
81+
)
82+
setFilter(
83+
Expression.neq(Expression.get("cluster"), true)
84+
)
85+
}
86+
87+
return Pair(source, listOf(clusterBackground, clusterCount, eventMarker, eventIcon))
88+
}

app/src/main/kotlin/org/btcmap/map/MapFragment.kt

Lines changed: 11 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -216,23 +216,26 @@ class MapFragment : Fragment() {
216216
}
217217

218218
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
219-
val merchantSourceAndLayers = createMerchantsSourceAndLayers(
219+
val merchantLayers = createMerchantsSourceAndLayers(
220220
markerBackgroundColor = prefs.markerBackgroundColor(requireContext()),
221221
markerBadgeBackgroundColor = prefs.badgeBackgroundColor(requireContext()),
222222
markerBadgeTextColor = prefs.badgeTextColor(requireContext()),
223223
usingOpenFreeMap = usingOpenFreeMap(),
224224
)
225-
226-
merchantsSource = merchantSourceAndLayers.first
227-
225+
val eventLayers = createEventLayers(
226+
markerBackgroundColor = prefs.markerBackgroundColor(requireContext()),
227+
usingOpenFreeMap = usingOpenFreeMap(),
228+
)
229+
merchantsSource = merchantLayers.first
230+
eventsSource = eventLayers.first
228231
binding.map.getMapAsync { map ->
229232
map.getStyle { style ->
230-
style.addSource(merchantSourceAndLayers.first)
231-
merchantSourceAndLayers.second.forEach { style.addLayer(it) }
233+
style.addSource(merchantLayers.first)
234+
merchantLayers.second.forEach { style.addLayer(it) }
235+
style.addSource(eventLayers.first)
236+
eventLayers.second.forEach { style.addLayer(it) }
232237
}
233238
}
234-
235-
initEventsMap()
236239
initExchangesMap()
237240

238241
initSearchBar(binding)
@@ -767,93 +770,6 @@ class MapFragment : Fragment() {
767770
private lateinit var eventsSource: GeoJsonSource
768771
private lateinit var exchangesSource: GeoJsonSource
769772

770-
private fun initEventsMap() {
771-
val eventsSource = GeoJsonSource(
772-
"eventsSource",
773-
EMPTY_GEOJSON,
774-
GeoJsonOptions()
775-
.withCluster(true)
776-
.withClusterMaxZoom(14)
777-
.withClusterRadius(30)
778-
)
779-
780-
val eventsClusterBackgroundLayer by lazy {
781-
CircleLayer("eventsClusterBackground", eventsSource.id).apply {
782-
setProperties(
783-
PropertyFactory.circleColor(prefs.markerBackgroundColor(requireContext())),
784-
PropertyFactory.circleRadius(23f),
785-
)
786-
val pointCount = Expression.toNumber(Expression.get("point_count"))
787-
setFilter(
788-
Expression.all(
789-
Expression.has("point_count"),
790-
Expression.gte(
791-
pointCount,
792-
Expression.literal(1)
793-
)
794-
)
795-
)
796-
}
797-
}
798-
799-
val eventsClusterCountLayer =
800-
SymbolLayer("eventsClusterCount", eventsSource.id).apply {
801-
if (usingOpenFreeMap()) {
802-
setProperties(PropertyFactory.textFont(arrayOf("Noto Sans Regular")))
803-
}
804-
setProperties(
805-
PropertyFactory.textField(Expression.toString(Expression.get("point_count"))),
806-
PropertyFactory.textSize(16f),
807-
PropertyFactory.textColor(Color.WHITE),
808-
)
809-
}
810-
811-
val eventsLayer =
812-
SymbolLayer(LAYER_EVENTS, eventsSource.id).apply {
813-
setProperties(
814-
PropertyFactory.iconImage("btcmap-marker"),
815-
PropertyFactory.iconAnchor(Expression.literal("bottom")),
816-
PropertyFactory.iconAllowOverlap(true),
817-
PropertyFactory.iconIgnorePlacement(true)
818-
)
819-
setFilter(
820-
Expression.neq(Expression.get("cluster"), true)
821-
)
822-
}
823-
824-
val eventsCategoryIconsLayer =
825-
SymbolLayer(LAYER_EVENTS_CATEGORY_ICONS, eventsSource.id).apply {
826-
setProperties(
827-
PropertyFactory.iconImage("marker-icon-event"),
828-
PropertyFactory.iconAnchor(ICON_ANCHOR_CENTER),
829-
PropertyFactory.iconOffset(
830-
arrayOf(
831-
0f,
832-
ICON_OFFSET_Y
833-
)
834-
),
835-
PropertyFactory.iconAllowOverlap(true),
836-
PropertyFactory.iconIgnorePlacement(true)
837-
)
838-
setFilter(
839-
Expression.neq(Expression.get("cluster"), true)
840-
)
841-
}
842-
843-
binding.map.getMapAsync { map ->
844-
map.getStyle { style ->
845-
style.addSource(eventsSource)
846-
847-
style.addLayer(eventsClusterBackgroundLayer)
848-
style.addLayer(eventsClusterCountLayer)
849-
style.addLayer(eventsLayer)
850-
style.addLayer(eventsCategoryIconsLayer)
851-
}
852-
}
853-
854-
this.eventsSource = eventsSource
855-
}
856-
857773
private fun initExchangesMap() {
858774
val exchangesSource = GeoJsonSource(
859775
"exchangesSource",

0 commit comments

Comments
 (0)