Skip to content

Commit

Permalink
Use vector maps on area screen
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Oct 25, 2024
1 parent 659a48a commit 21cd557
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 24 deletions.
7 changes: 5 additions & 2 deletions app/src/androidMain/kotlin/area/AreaAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import icons.iconTypeface
import map.initStyle
import map.showPolygons
import okhttp3.HttpUrl
import org.btcmap.databinding.ItemAreaDescriptionBinding
Expand Down Expand Up @@ -136,8 +137,10 @@ class AreaAdapter(
listener: Listener,
) {
if (item is Item.Map && binding is ItemMapBinding) {
binding.map.showPolygons(item.polygons, item.paddingPx)
binding.root.setOnClickListener { listener.onMapClick() }
binding.map.getMapAsync {
it.initStyle(binding.root.context)
it.showPolygons(item.polygons, item.paddingPx)
}
}

if (item is Item.Description && binding is ItemAreaDescriptionBinding) {
Expand Down
30 changes: 11 additions & 19 deletions app/src/androidMain/kotlin/map/MapViewExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,20 @@ import android.content.Context
import android.content.res.Configuration
import android.graphics.Color
import org.locationtech.jts.geom.Polygon
import org.maplibre.android.annotations.PolygonOptions
import org.maplibre.android.camera.CameraUpdateFactory
import org.maplibre.android.geometry.LatLng
import org.maplibre.android.geometry.LatLngBounds
import org.maplibre.android.maps.MapLibreMap
import org.osmdroid.util.GeoPoint
import org.osmdroid.views.MapView

fun MapView.showPolygons(polygons: List<Polygon>, paddingPx: Int) {
post {
polygons.forEach { poly ->
val osmPoly = org.osmdroid.views.overlay.Polygon(this)
osmPoly.fillPaint.color = Color.parseColor("#88f7931a")
osmPoly.outlinePaint.strokeWidth = 3f
osmPoly.outlinePaint.color = Color.parseColor("#f7931a")
osmPoly.points = poly.coordinates.map { GeoPoint(it.y, it.x) }
overlays.add(osmPoly)
invalidate()
}

zoomToBoundingBox(
boundingBox(polygons),
false,
paddingPx,
)
fun MapLibreMap.showPolygons(polygons: List<Polygon>, paddingPx: Int) {
polygons.forEach { poly ->
val librePoly = PolygonOptions().addAll(poly.coordinates.map { LatLng(it.y, it.x) })
.fillColor(Color.parseColor("#88f7931a")).strokeColor(Color.parseColor("#f7931a"))
addPolygon(librePoly)
}
val allPoints = polygons.flatMap { it.coordinates.toList() }.map { LatLng(it.y, it.x) }
moveCamera(CameraUpdateFactory.newLatLngBounds(LatLngBounds.fromLatLngs(allPoints), paddingPx))
}

fun MapLibreMap.initStyle(context: Context) {
Expand Down
2 changes: 0 additions & 2 deletions app/src/androidMain/res/layout/fragment_element.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@

<org.maplibre.android.maps.MapView
android:id="@+id/map"
android:clickable="false"
android:focusable="false"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
Expand Down
2 changes: 1 addition & 1 deletion app/src/androidMain/res/layout/item_map.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content">

<map.DisabledMapView
<org.maplibre.android.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="192dp" />
Expand Down

0 comments on commit 21cd557

Please sign in to comment.