Created
January 29, 2020 15:06
-
-
Save Firsto/121b14b8de894a6d170ed06249d826b5 to your computer and use it in GitHub Desktop.
SImple view to add scale bar in map container layout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context | |
import android.graphics.Canvas | |
import android.graphics.Color | |
import android.graphics.Paint | |
import android.graphics.Paint.Style.FILL | |
import android.graphics.Paint.Style.STROKE | |
import android.graphics.Path | |
import android.graphics.Rect | |
import android.util.AttributeSet | |
import android.view.View | |
import kotlin.math.abs | |
import kotlin.math.cos | |
import kotlin.math.pow | |
class ScaleView @JvmOverloads constructor(context: Context?, | |
attrs: AttributeSet? = null, | |
defStyleAttr: Int = 0 | |
) : View(context, attrs, defStyleAttr) { | |
private val density = resources.displayMetrics.density | |
private val color = Color.parseColor("#333333") | |
private val scaleData: ScaleData | |
private val textSize: Float | |
private val strokeWidth: Float | |
private val textPaint = Paint() | |
private val strokePaint = Paint() | |
private val strokePath = Path() | |
private val textHeight: Float | |
private val linePosition: Float | |
init { | |
scaleData = ScaleData(density) | |
textSize = 12 * density | |
strokeWidth = 1.5f * density | |
textPaint.isAntiAlias = true | |
textPaint.color = color | |
textPaint.style = FILL | |
textPaint.textSize = textSize | |
strokePaint.isAntiAlias = true | |
strokePaint.color = color | |
strokePaint.style = STROKE | |
strokePaint.strokeWidth = strokeWidth | |
val textRect = Rect() | |
val possibleText = "1234567890 nmi" | |
textPaint.getTextBounds(possibleText, 0, possibleText.length, textRect) | |
textHeight = textRect.height().toFloat() | |
linePosition = textHeight + textHeight / 2 | |
} | |
fun update(zoom: Float = -1f, latitude: Double = -100.0) { | |
scaleData.update(zoom, latitude) | |
invalidate() | |
} | |
val maxHeight: Int | |
get() = (textPaint.textSize * 3 + textPaint.strokeWidth).toInt() | |
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { | |
super.onSizeChanged(w, h, oldw, oldh) | |
scaleData.maxWidth = w | |
update() | |
} | |
override fun onDraw(canvas: Canvas) { | |
if (scaleData.lineLength > 0) { | |
canvas.drawText(scaleData.toString(), 0f, textHeight, textPaint) | |
strokePath.rewind() | |
strokePath.moveTo(0f, linePosition) | |
strokePath.lineTo(scaleData.lineLength, linePosition) | |
canvas.drawPath(strokePath, strokePaint) | |
} | |
} | |
/** | |
* All calculations are based on the http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Resolution_and_Scale | |
* | |
* @field TILE_SIZE : 513592.62 feet or 156543.03 meters | |
*/ | |
internal data class ScaleData(private val density: Float) { | |
internal var maxWidth = 0 | |
private var lastZoom = -1f | |
private var lastLatitude = -100.0 | |
private var distance = 0f | |
var lineLength: Float = 0f | |
fun update(zoom: Float, latitude: Double) { | |
lastZoom = zoom | |
lastLatitude = latitude | |
distance = 0f | |
if (lastZoom < 0 || abs(lastLatitude) > 90) return | |
val resolution = | |
TILE_SIZE / density * cos(lastLatitude * Math.PI / 180) / 2.0.pow(lastZoom.toDouble()) | |
var dIndex = DISTANCES.size | |
var screenDistance = maxWidth + 1.0 | |
while (screenDistance > maxWidth && dIndex > 0) { | |
distance = DISTANCES[--dIndex] | |
screenDistance = abs(distance / resolution) | |
} | |
lineLength = screenDistance.toFloat() | |
} | |
override fun toString() = if (distance < UNIT_SIZE) { | |
"${distance.toInt()} m" | |
} else { | |
"${(distance / UNIT_SIZE).toInt()} nmi" | |
} | |
companion object { | |
private const val TILE_SIZE = 156543.03 // 513592.62; | |
private const val UNIT_SIZE = 1852f // 6076.12f; | |
private val DISTANCES = floatArrayOf( | |
1f, 2f, 5f, 10f, 20f, 50f, 100f, 200f, 500f, 1000f, 2000f, | |
UNIT_SIZE, 2 * UNIT_SIZE, 5 * UNIT_SIZE, 10 * UNIT_SIZE, 20 * UNIT_SIZE, 50 * UNIT_SIZE, | |
100 * UNIT_SIZE, 200 * UNIT_SIZE, 500 * UNIT_SIZE, 1000 * UNIT_SIZE, 2000 * UNIT_SIZE | |
) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment