Skip to content

Commit

Permalink
feat: add map ScaleBarOverlay
Browse files Browse the repository at this point in the history
  • Loading branch information
andrekir committed Nov 4, 2024
1 parent c6cc33e commit eea62e6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/src/main/java/com/geeksville/mesh/ui/map/MapFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalHapticFeedback
import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.compose.ui.unit.dp
Expand Down Expand Up @@ -85,6 +86,7 @@ import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay
import com.geeksville.mesh.model.map.clustering.RadiusMarkerClusterer
import com.geeksville.mesh.util.addCopyright
import com.geeksville.mesh.util.addMapEventListener
import com.geeksville.mesh.util.addScaleBarOverlay
import com.geeksville.mesh.util.createLatLongGrid
import java.io.File
import java.text.DateFormat
Expand Down Expand Up @@ -230,6 +232,7 @@ fun MapView(
var myLocationOverlay: MyLocationNewOverlay? by remember { mutableStateOf(null) }

val context = LocalContext.current
val density = LocalDensity.current
val mPrefs = remember { context.getSharedPreferences(prefsName, Context.MODE_PRIVATE) }

val haptic = LocalHapticFeedback.current
Expand Down Expand Up @@ -441,6 +444,7 @@ fun MapView(
}

addCopyright() // Copyright is required for certain map sources
addScaleBarOverlay(density)
createLatLongGrid(false)

invalidate()
Expand Down
22 changes: 22 additions & 0 deletions app/src/main/java/com/geeksville/mesh/util/MapViewExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ package com.geeksville.mesh.util

import android.graphics.Color
import android.graphics.Paint
import android.graphics.Typeface
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import org.osmdroid.events.DelayedMapListener
import org.osmdroid.events.MapListener
import org.osmdroid.events.ScrollEvent
import org.osmdroid.events.ZoomEvent
import org.osmdroid.views.MapView
import org.osmdroid.views.overlay.CopyrightOverlay
import org.osmdroid.views.overlay.ScaleBarOverlay
import org.osmdroid.views.overlay.gridlines.LatLonGridlineOverlay2

/**
Expand Down Expand Up @@ -45,6 +50,23 @@ fun MapView.createLatLongGrid(enabled: Boolean) {
}
}

fun MapView.addScaleBarOverlay(density: Density) {
if (overlays.none { it is ScaleBarOverlay }) {
val scaleBarOverlay = ScaleBarOverlay(this).apply {
setAlignBottom(true)
with(density) {
setScaleBarOffset(15.dp.toPx().toInt(), 40.dp.toPx().toInt())
setTextSize(12.sp.toPx())
}
textPaint.apply {
isAntiAlias = true
typeface = Typeface.DEFAULT_BOLD
}
}
overlays.add(scaleBarOverlay)
}
}

private const val INACTIVITY_DELAY_MILLIS = 500L
fun MapView.addMapEventListener(onEvent: () -> Unit) {
addMapListener(DelayedMapListener(object : MapListener {
Expand Down

0 comments on commit eea62e6

Please sign in to comment.