Display grazing area from detail response.
This commit is contained in:
parent
48a6d1fff1
commit
efb3921dc2
|
@ -3,6 +3,7 @@ package com.danilafe.fencelessgrazing.ui.activities
|
||||||
import android.graphics.Color
|
import android.graphics.Color
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.util.Log
|
||||||
import android.widget.TextView
|
import android.widget.TextView
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import com.android.volley.RequestQueue
|
import com.android.volley.RequestQueue
|
||||||
|
@ -12,6 +13,7 @@ import com.danilafe.fencelessgrazing.R
|
||||||
import com.danilafe.fencelessgrazing.model.Polygon
|
import com.danilafe.fencelessgrazing.model.Polygon
|
||||||
import com.danilafe.fencelessgrazing.requests.authenticated.CollarDetailRequest
|
import com.danilafe.fencelessgrazing.requests.authenticated.CollarDetailRequest
|
||||||
import com.danilafe.fencelessgrazing.requests.authenticated.CollarHistoryRequest
|
import com.danilafe.fencelessgrazing.requests.authenticated.CollarHistoryRequest
|
||||||
|
import com.danilafe.fencelessgrazing.ui.components.GrazingPolygon
|
||||||
import org.osmdroid.util.GeoPoint
|
import org.osmdroid.util.GeoPoint
|
||||||
import org.osmdroid.views.MapView
|
import org.osmdroid.views.MapView
|
||||||
import org.osmdroid.views.overlay.Marker
|
import org.osmdroid.views.overlay.Marker
|
||||||
|
@ -81,7 +83,7 @@ class CollarDetailActivity : AppCompatActivity() {
|
||||||
* to display the valid grazing area for the collar; its vertices
|
* to display the valid grazing area for the collar; its vertices
|
||||||
* are updated whenever the [boundingBox] property is changed.
|
* 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.
|
* The [Polygon] that represents the collar's valid grazing area.
|
||||||
|
@ -133,10 +135,9 @@ class CollarDetailActivity : AppCompatActivity() {
|
||||||
|
|
||||||
map = findViewById(R.id.detailMap)
|
map = findViewById(R.id.detailMap)
|
||||||
map.controller.setZoom(9.5)
|
map.controller.setZoom(9.5)
|
||||||
mapPolygon = org.osmdroid.views.overlay.Polygon(map)
|
mapPolygon = GrazingPolygon(map)
|
||||||
mapMarker = Marker(map)
|
mapMarker = Marker(map)
|
||||||
mapPolyline = Polyline(map)
|
mapPolyline = Polyline(map)
|
||||||
configureMapPolygon()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onPause() {
|
override fun onPause() {
|
||||||
|
@ -187,6 +188,7 @@ class CollarDetailActivity : AppCompatActivity() {
|
||||||
Response.Listener {
|
Response.Listener {
|
||||||
collarName.text = it.name
|
collarName.text = it.name
|
||||||
collarStimulus.text = getString(R.string.collarSummaryStimulus, it.stimulus)
|
collarStimulus.text = getString(R.string.collarSummaryStimulus, it.stimulus)
|
||||||
|
boundingBox = if (it.boundary.size >= 3) Polygon(it.boundary) else null
|
||||||
},
|
},
|
||||||
Response.ErrorListener {
|
Response.ErrorListener {
|
||||||
Toast.makeText(this, "Failed to retrieve details of collar", Toast.LENGTH_SHORT)
|
Toast.makeText(this, "Failed to retrieve details of collar", Toast.LENGTH_SHORT)
|
||||||
|
@ -197,17 +199,6 @@ class CollarDetailActivity : AppCompatActivity() {
|
||||||
queue.add(detailRequest)
|
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
|
* Given a new location history of the animal, configures
|
||||||
* updates the [map] to include the relevant elements (such as the animal's
|
* 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) {
|
private fun updateBoundingBox(oldValue: Polygon?, polygon: Polygon) {
|
||||||
if(oldValue == null) map.overlays.add(mapPolygon)
|
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()
|
val polygonPoints = points.toMutableList()
|
||||||
polygonPoints.add(polygonPoints[0])
|
polygonPoints.add(polygonPoints[0])
|
||||||
mapPolygon.points = polygonPoints
|
mapPolygon.points = polygonPoints
|
||||||
|
@ -256,6 +247,7 @@ class CollarDetailActivity : AppCompatActivity() {
|
||||||
* the [mapPolygon] from the [map].
|
* the [mapPolygon] from the [map].
|
||||||
*/
|
*/
|
||||||
private fun clearBoundingBox() {
|
private fun clearBoundingBox() {
|
||||||
|
Log.d("FencelessGrazing", "Clearing...")
|
||||||
map.overlays.remove(mapPolygon)
|
map.overlays.remove(mapPolygon)
|
||||||
map.invalidate()
|
map.invalidate()
|
||||||
}
|
}
|
||||||
|
|
|
@ -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"
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user