Center the map on collars when possible

This commit is contained in:
Danila Fedorin 2020-05-12 18:39:35 -07:00
parent 7716d77f25
commit 8bf4f513b7
2 changed files with 26 additions and 10 deletions

View File

@ -1,18 +1,14 @@
package com.danilafe.fencelessgrazing
import android.app.Application
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.Request
import com.android.volley.RequestQueue
import com.android.volley.Response
import com.android.volley.toolbox.Volley
import com.danilafe.fencelessgrazing.model.CollarPos
import com.danilafe.fencelessgrazing.model.CollarSummary
import com.danilafe.fencelessgrazing.model.Polygon
import com.danilafe.fencelessgrazing.requests.CollarDetailRequest
import com.danilafe.fencelessgrazing.requests.CollarHistoryRequest
@ -20,9 +16,7 @@ import org.osmdroid.util.GeoPoint
import org.osmdroid.views.MapView
import org.osmdroid.views.overlay.Marker
import org.osmdroid.views.overlay.Polyline
import java.sql.Time
import java.util.*
import kotlin.collections.ArrayList
import kotlin.concurrent.timerTask
class CollarDetailActivity : AppCompatActivity() {
@ -34,6 +28,7 @@ class CollarDetailActivity : AppCompatActivity() {
private lateinit var queue: RequestQueue
private var collarId: Int = -1
private var centerSet: Boolean = false
private lateinit var map: MapView
private var refreshTimer = Timer()
@ -67,6 +62,7 @@ class CollarDetailActivity : AppCompatActivity() {
queue = Volley.newRequestQueue(this)
map = findViewById(R.id.detailMap)
map.controller.setZoom(9.5)
mapPolygon = org.osmdroid.views.overlay.Polygon(map)
mapMarker = Marker(map)
mapPolyline = Polyline(map)
@ -127,11 +123,15 @@ class CollarDetailActivity : AppCompatActivity() {
private fun updateAnimalHistory(points : List<GeoPoint>) {
val currentPoint = points.first()
mapMarker.position = currentPoint
mapPolyline.setPoints(points)
collarPos.text = getString(R.string.collarSummaryLocation, currentPoint.longitude, currentPoint.latitude)
if(!map.overlays.contains(mapMarker)) map.overlays.add(mapMarker)
if(!map.overlays.contains(mapPolyline)) map.overlays.add(mapPolyline)
mapMarker.position = currentPoint
mapPolyline.setPoints(points)
if(!centerSet) {
centerSet = true
map.controller.setCenter(currentPoint)
}
map.invalidate()
}

View File

@ -3,7 +3,6 @@ package com.danilafe.fencelessgrazing
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.DividerItemDecoration
@ -34,6 +33,7 @@ class CollarListActivity : AppCompatActivity() {
private lateinit var queue: RequestQueue
// The OpenStreetMap map.
private var centerSet: Boolean = false
private lateinit var map: MapView
private val collarOverlays: MutableMap<Int, Marker> = mutableMapOf()
@ -51,6 +51,7 @@ class CollarListActivity : AppCompatActivity() {
token = intent.getStringExtra("token")!!
map.setTileSource(TileSourceFactory.MAPNIK)
map.controller.setZoom(9.5)
queue = Volley.newRequestQueue(this)
setupCollarList()
@ -76,7 +77,7 @@ class CollarListActivity : AppCompatActivity() {
summaries.clear()
summaries.addAll(it)
summaryAdapter.notifyDataSetChanged()
updateMapOverlay()
updateMap()
},
Response.ErrorListener {
Toast.makeText(this, "Failed to retrieve collar list!", Toast.LENGTH_SHORT).show()
@ -111,6 +112,20 @@ class CollarListActivity : AppCompatActivity() {
collarList.addItemDecoration(DividerItemDecoration(collarList.context, layoutManager.orientation))
}
private fun updateMap() {
updateMapCenter()
updateMapOverlay()
}
private fun updateMapCenter() {
if(centerSet) return
centerSet = true
val averageLongitude = summaries.map { it.pos.longitude.toDouble() }.average()
val averageLatitude = summaries.map { it.pos.latitude.toDouble() }.average()
map.controller.setCenter(GeoPoint(averageLongitude, averageLatitude))
}
private fun updateMapOverlay() {
val currentSet = mutableSetOf<Int>()
summaries.forEach {
@ -132,6 +147,7 @@ class CollarListActivity : AppCompatActivity() {
collarOverlays.remove(it)
}
}
map.invalidate()
}
}