Skip to content

Commit

Permalink
Added more accurate location display on the maps
Browse files Browse the repository at this point in the history
  • Loading branch information
CNugteren committed Nov 16, 2019
1 parent 14bdf2a commit da4a3dd
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 20 deletions.
35 changes: 23 additions & 12 deletions app/src/main/java/foss/cnugteren/nlweer/DrawWebView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class DrawWebView : WebView {
var lat = 0f
var lon = 0f
var drawCircles = false
lateinit var imageCoordinates: Array<Float> // minLat, minLon, maxLat, maxLon
lateinit var imageCoordinates: Array<Float> // minLat, minLon, maxLat, maxLon, width-offset-based-on-height, height-offset-based-on-width
private var scaleWidth = 1f
private var scaleHeight = 1f
private var minScale = 1f
Expand Down Expand Up @@ -57,26 +57,37 @@ class DrawWebView : WebView {

// Some test values to test the map imageCoordinates
private fun drawTestCircles(canvas: Canvas) {
drawCircle(canvas, 52.11f, 5.18f) // de Bilt
drawCircle(canvas, 50.868f, 5.702f) // Maastricht
drawCircle(canvas, 53.167f, 4.839f) // top of Texel
drawCircle(canvas, 51.529f, 3.428f) // Bottom left: Westkapelle
drawCircle(canvas, 53.229f, 7.191f) // Top right at the bottom of Dollart and border with DE
drawCircle(canvas, 51.3694f,3.3659f) // SW border with Belgium at Cadzand
drawCircle(canvas, 50.7543f,6.0209f) // SE border with Belgium & Germany at Vaalserberg
drawCircle(canvas, 51.8236f,5.9449f) // E border with Germany at Nijmegen
drawCircle(canvas, 53.2360f,7.2098f) // NE border with Germany at the bottom of the Dollart
drawCircle(canvas, 53.4390f,5.5466f) // N border with the sea at the eastern most point of Terschelling
drawCircle(canvas, 53.1851f,4.8522f) // NW border with the sea at the northern most point of Texel
drawCircle(canvas, 51.9841f,4.0964f) // S border with the sea at the extreme point at Hoek van Holland
}

private fun drawCircle(canvas: Canvas, lat: Float, lon: Float) {
if (imageCoordinates.size < 6) { return } // invalid data provided

val minLat = imageCoordinates[0]; val minLon = imageCoordinates[1]
val maxLat = imageCoordinates[2]; val maxLon = imageCoordinates[3]
if (lat < minLat || lon < minLon || lat > maxLat || lon > maxLon) {

// Percentage of the image computation
var percentWidth = (lon - minLon) / (maxLon - minLon)
var percentHeight = (lat - minLat) / (maxLat - minLat)
percentWidth += percentHeight * imageCoordinates[4]
percentHeight += percentWidth * imageCoordinates[5]
if (percentHeight < 0 || percentWidth < 0 || percentHeight > 1 || percentWidth > 1) {
return // skip drawing - out of bounds
}
val offsetWidth = (fullWidth - imageWidth * minScale) / 2f
val offsetHeight = (fullHeight - imageHeight * minScale) / 2f
val percentWidth = (lon - minLon) / (maxLon - minLon)
val percentHeight = (lat - minLat) / (maxLat - minLat)

// Pixel space
val xPos = imageWidth * minScale * percentWidth
val yPos = imageHeight * minScale * (1f - percentHeight)
canvas.drawCircle(xPos + offsetWidth, yPos + offsetHeight,40f, painter)
val offsetWidth = (fullWidth - imageWidth * minScale) / 2f
val offsetHeight = (fullHeight - imageHeight * minScale) / 2f
canvas.drawCircle(xPos + offsetWidth, yPos + offsetHeight,25f, painter)
canvas.drawCircle(xPos + offsetWidth, yPos + offsetHeight,3f, painter)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ class KnmiRainM1Fragment : BaseFragment() {

override fun imageWidth(): Int { return 425 }
override fun imageHeight(): Int { return 445 }
override fun coordinates(): Array<Float> { return arrayOf(50.39f, 1.90f, 53.85f, 7.45f) }
override fun coordinates(): Array<Float> { return arrayOf(50.60f, 1.85f, 54.05f, 7.20f, -0.10f, 0.09f) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ class KnmiTemperatureFragment : BaseFragment() {

override fun imageWidth(): Int { return 569 }
override fun imageHeight(): Int { return 622 }
override fun coordinates(): Array<Float> { return arrayOf(50.70f, 2.70f, 53.67f, 7.57f) }
override fun coordinates(): Array<Float> { return arrayOf(50.70f, 2.95f, 53.65f, 7.30f, -0.01f, 0.00f) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ class KnmiTodayFragment : BaseFragment() {

override fun imageWidth(): Int { return 425 }
override fun imageHeight(): Int { return 467 }
override fun coordinates(): Array<Float> { return arrayOf(50.70f, 3.00f, 53.67f, 7.57f) }
override fun coordinates(): Array<Float> { return arrayOf(50.70f, 3.10f, 53.65f, 7.55f, -0.01f, 0.00f) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ class KnmiTomorrowFragment : BaseFragment() {

override fun imageWidth(): Int { return 425 }
override fun imageHeight(): Int { return 467 }
override fun coordinates(): Array<Float> { return arrayOf(50.70f, 3.00f, 53.67f, 7.57f) }
override fun coordinates(): Array<Float> { return arrayOf(50.70f, 3.10f, 53.65f, 7.55f, -0.01f, 0.00f) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ class KnmiTonightFragment : BaseFragment() {

override fun imageWidth(): Int { return 425 }
override fun imageHeight(): Int { return 467 }
override fun coordinates(): Array<Float> { return arrayOf(50.70f, 3.00f, 53.67f, 7.57f) }
override fun coordinates(): Array<Float> { return arrayOf(50.70f, 3.10f, 53.65f, 7.55f, -0.01f, 0.00f) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ class KnmiWindFragment : BaseFragment() {

override fun imageWidth(): Int { return 569 }
override fun imageHeight(): Int { return 622 }
override fun coordinates(): Array<Float> { return arrayOf(50.70f, 2.70f, 53.67f, 7.57f) }
override fun coordinates(): Array<Float> { return arrayOf(50.70f, 2.95f, 53.65f, 7.30f, -0.01f, 0.00f) }
}
2 changes: 1 addition & 1 deletion app/src/main/res/values-nl/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<string name="menu_buienradar_header">Buienradar</string>

<string name="settings_location_enable">Laat mijn locatie zien</string>
<string name="settings_location_enable_sub">Teken een cirkel ongeveer op mijn locatie</string>
<string name="settings_location_enable_sub">Teken een cirkel op mijn locatie</string>
<string name="settings_latitude">Mijn locatie: latitude</string>
<string name="settings_longitude">Mijn locatie: longitude</string>

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<string name="menu_buienradar_header">Buienradar</string>

<string name="settings_location_enable">Show my location</string>
<string name="settings_location_enable_sub">Shows a circle at roughly your location</string>
<string name="settings_location_enable_sub">Shows a circle at your location</string>
<string name="settings_latitude">My location: latitude</string>
<string name="settings_longitude">My location: longitude</string>

Expand Down

0 comments on commit da4a3dd

Please sign in to comment.