From 246d43be8051362acd9bd830d66c9231c0cbf591 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Tue, 12 May 2020 00:51:53 -0700 Subject: [PATCH] Start working on bounding box display --- .../fencelessgrazing/CollarDetailActivity.kt | 48 +++++++++++++++++++ .../fencelessgrazing/model/Polygon.kt | 1 + .../res/layout/activity_collar_detail.xml | 8 ++++ 3 files changed, 57 insertions(+) diff --git a/app/src/main/java/com/danilafe/fencelessgrazing/CollarDetailActivity.kt b/app/src/main/java/com/danilafe/fencelessgrazing/CollarDetailActivity.kt index e4d2962..46873fd 100644 --- a/app/src/main/java/com/danilafe/fencelessgrazing/CollarDetailActivity.kt +++ b/app/src/main/java/com/danilafe/fencelessgrazing/CollarDetailActivity.kt @@ -1,18 +1,66 @@ package com.danilafe.fencelessgrazing +import android.graphics.Color import androidx.appcompat.app.AppCompatActivity import android.os.Bundle +import com.danilafe.fencelessgrazing.model.CollarPos +import com.danilafe.fencelessgrazing.model.Polygon +import org.osmdroid.util.GeoPoint +import org.osmdroid.views.MapView class CollarDetailActivity : AppCompatActivity() { private lateinit var token: String private var collarId: Int = -1 + private lateinit var map: MapView + + private lateinit var mapPolygon: org.osmdroid.views.overlay.Polygon + private var boundingBox: Polygon? = null + set(newValue) { + if(newValue != null) updateBoundingBox(field, newValue) + else clearBoundingBox() + field = newValue + } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_collar_detail) token = intent.getStringExtra("token")!! collarId = intent.getIntExtra("identifier", -1) + + map = findViewById(R.id.detailMap) + mapPolygon = org.osmdroid.views.overlay.Polygon(map) + configureMapPolygon() + } + + private fun configureMapPolygon() { + mapPolygon.fillPaint.color = Color.CYAN + mapPolygon.fillPaint.alpha = 127 + mapPolygon.outlinePaint.strokeWidth = 0.0f + mapPolygon.title = "Valid Grazing Area" + } + + 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 polygonPoints = points.toMutableList() + polygonPoints.add(polygonPoints[0]) + mapPolygon.points = polygonPoints + } + + private fun clearBoundingBox() { + map.overlays.remove(mapPolygon) + } + + override fun onPause() { + super.onPause() + map.onPause() + } + + override fun onResume() { + super.onResume() + map.onResume() } } diff --git a/app/src/main/java/com/danilafe/fencelessgrazing/model/Polygon.kt b/app/src/main/java/com/danilafe/fencelessgrazing/model/Polygon.kt index 51d4e3e..1b1b576 100644 --- a/app/src/main/java/com/danilafe/fencelessgrazing/model/Polygon.kt +++ b/app/src/main/java/com/danilafe/fencelessgrazing/model/Polygon.kt @@ -1,2 +1,3 @@ package com.danilafe.fencelessgrazing.model +data class Polygon(val dataPoints : List) \ No newline at end of file diff --git a/app/src/main/res/layout/activity_collar_detail.xml b/app/src/main/res/layout/activity_collar_detail.xml index 5da2fce..54b15f9 100644 --- a/app/src/main/res/layout/activity_collar_detail.xml +++ b/app/src/main/res/layout/activity_collar_detail.xml @@ -5,5 +5,13 @@ android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".CollarDetailActivity"> + \ No newline at end of file