From efb3921dc24a0f19fff4ff49d9f0de15fed08384 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 13 May 2020 23:54:28 -0700 Subject: [PATCH] Display grazing area from detail response. --- .../ui/activities/CollarDetailActivity.kt | 22 ++++++------------- .../ui/components/GrazingPolygon.kt | 15 +++++++++++++ 2 files changed, 22 insertions(+), 15 deletions(-) create mode 100644 app/src/main/java/com/danilafe/fencelessgrazing/ui/components/GrazingPolygon.kt diff --git a/app/src/main/java/com/danilafe/fencelessgrazing/ui/activities/CollarDetailActivity.kt b/app/src/main/java/com/danilafe/fencelessgrazing/ui/activities/CollarDetailActivity.kt index 2665e33..b4528f6 100644 --- a/app/src/main/java/com/danilafe/fencelessgrazing/ui/activities/CollarDetailActivity.kt +++ b/app/src/main/java/com/danilafe/fencelessgrazing/ui/activities/CollarDetailActivity.kt @@ -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() } diff --git a/app/src/main/java/com/danilafe/fencelessgrazing/ui/components/GrazingPolygon.kt b/app/src/main/java/com/danilafe/fencelessgrazing/ui/components/GrazingPolygon.kt new file mode 100644 index 0000000..09042f8 --- /dev/null +++ b/app/src/main/java/com/danilafe/fencelessgrazing/ui/components/GrazingPolygon.kt @@ -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" + } +} \ No newline at end of file