Display grazing area from detail response.

This commit is contained in:
Danila Fedorin 2020-05-13 23:54:28 -07:00
parent 48a6d1fff1
commit efb3921dc2
2 changed files with 22 additions and 15 deletions

View File

@ -3,6 +3,7 @@ package com.danilafe.fencelessgrazing.ui.activities
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import android.widget.Toast
import com.android.volley.RequestQueue
@ -12,6 +13,7 @@ import com.danilafe.fencelessgrazing.R
import com.danilafe.fencelessgrazing.model.Polygon
import com.danilafe.fencelessgrazing.requests.authenticated.CollarDetailRequest
import com.danilafe.fencelessgrazing.requests.authenticated.CollarHistoryRequest
import com.danilafe.fencelessgrazing.ui.components.GrazingPolygon
import org.osmdroid.util.GeoPoint
import org.osmdroid.views.MapView
import org.osmdroid.views.overlay.Marker
@ -81,7 +83,7 @@ class CollarDetailActivity : AppCompatActivity() {
* to display the valid grazing area for the collar; its vertices
* are updated whenever the [boundingBox] property is changed.
*/
private lateinit var mapPolygon: org.osmdroid.views.overlay.Polygon
private lateinit var mapPolygon: GrazingPolygon
/**
* The [Polygon] that represents the collar's valid grazing area.
@ -133,10 +135,9 @@ class CollarDetailActivity : AppCompatActivity() {
map = findViewById(R.id.detailMap)
map.controller.setZoom(9.5)
mapPolygon = org.osmdroid.views.overlay.Polygon(map)
mapPolygon = GrazingPolygon(map)
mapMarker = Marker(map)
mapPolyline = Polyline(map)
configureMapPolygon()
}
override fun onPause() {
@ -187,6 +188,7 @@ class CollarDetailActivity : AppCompatActivity() {
Response.Listener {
collarName.text = it.name
collarStimulus.text = getString(R.string.collarSummaryStimulus, it.stimulus)
boundingBox = if (it.boundary.size >= 3) Polygon(it.boundary) else null
},
Response.ErrorListener {
Toast.makeText(this, "Failed to retrieve details of collar", Toast.LENGTH_SHORT)
@ -197,17 +199,6 @@ class CollarDetailActivity : AppCompatActivity() {
queue.add(detailRequest)
}
/**
* Sets [mapPolygon]'s color, borders, and other relevant settings.
*/
private fun configureMapPolygon() {
mapPolygon.fillPaint.color = Color.parseColor("#32a852")
mapPolygon.fillPaint.alpha = 127
mapPolygon.outlinePaint.color = Color.parseColor("#227539")
mapPolygon.outlinePaint.strokeWidth = 0.0f
mapPolygon.title = "Valid Grazing Area"
}
/**
* Given a new location history of the animal, configures
* updates the [map] to include the relevant elements (such as the animal's
@ -244,7 +235,7 @@ class CollarDetailActivity : AppCompatActivity() {
*/
private fun updateBoundingBox(oldValue: Polygon?, polygon: Polygon) {
if(oldValue == null) map.overlays.add(mapPolygon)
val points = polygon.dataPoints.map { GeoPoint(it.longitude.toDouble(), it.latitude.toDouble()) }
val points = polygon.dataPoints.map { GeoPoint(it.latitude.toDouble(), it.longitude.toDouble()) }
val polygonPoints = points.toMutableList()
polygonPoints.add(polygonPoints[0])
mapPolygon.points = polygonPoints
@ -256,6 +247,7 @@ class CollarDetailActivity : AppCompatActivity() {
* the [mapPolygon] from the [map].
*/
private fun clearBoundingBox() {
Log.d("FencelessGrazing", "Clearing...")
map.overlays.remove(mapPolygon)
map.invalidate()
}

View File

@ -0,0 +1,15 @@
package com.danilafe.fencelessgrazing.ui.components
import android.graphics.Color
import org.osmdroid.views.MapView
import org.osmdroid.views.overlay.Polygon
class GrazingPolygon(map: MapView) : Polygon(map) {
init {
fillPaint.color = Color.parseColor("#32a852")
fillPaint.alpha = 127
outlinePaint.color = Color.parseColor("#227539")
outlinePaint.strokeWidth = 0.0f
title = "Valid Grazing Area"
}
}