Center the map on collars when possible
This commit is contained in:
parent
7716d77f25
commit
8bf4f513b7
|
@ -1,18 +1,14 @@
|
||||||
package com.danilafe.fencelessgrazing
|
package com.danilafe.fencelessgrazing
|
||||||
|
|
||||||
import android.app.Application
|
|
||||||
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.Request
|
|
||||||
import com.android.volley.RequestQueue
|
import com.android.volley.RequestQueue
|
||||||
import com.android.volley.Response
|
import com.android.volley.Response
|
||||||
import com.android.volley.toolbox.Volley
|
import com.android.volley.toolbox.Volley
|
||||||
import com.danilafe.fencelessgrazing.model.CollarPos
|
import com.danilafe.fencelessgrazing.model.CollarPos
|
||||||
import com.danilafe.fencelessgrazing.model.CollarSummary
|
|
||||||
import com.danilafe.fencelessgrazing.model.Polygon
|
import com.danilafe.fencelessgrazing.model.Polygon
|
||||||
import com.danilafe.fencelessgrazing.requests.CollarDetailRequest
|
import com.danilafe.fencelessgrazing.requests.CollarDetailRequest
|
||||||
import com.danilafe.fencelessgrazing.requests.CollarHistoryRequest
|
import com.danilafe.fencelessgrazing.requests.CollarHistoryRequest
|
||||||
|
@ -20,9 +16,7 @@ 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
|
||||||
import org.osmdroid.views.overlay.Polyline
|
import org.osmdroid.views.overlay.Polyline
|
||||||
import java.sql.Time
|
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import kotlin.collections.ArrayList
|
|
||||||
import kotlin.concurrent.timerTask
|
import kotlin.concurrent.timerTask
|
||||||
|
|
||||||
class CollarDetailActivity : AppCompatActivity() {
|
class CollarDetailActivity : AppCompatActivity() {
|
||||||
|
@ -34,6 +28,7 @@ class CollarDetailActivity : AppCompatActivity() {
|
||||||
private lateinit var queue: RequestQueue
|
private lateinit var queue: RequestQueue
|
||||||
private var collarId: Int = -1
|
private var collarId: Int = -1
|
||||||
|
|
||||||
|
private var centerSet: Boolean = false
|
||||||
private lateinit var map: MapView
|
private lateinit var map: MapView
|
||||||
|
|
||||||
private var refreshTimer = Timer()
|
private var refreshTimer = Timer()
|
||||||
|
@ -67,6 +62,7 @@ class CollarDetailActivity : AppCompatActivity() {
|
||||||
queue = Volley.newRequestQueue(this)
|
queue = Volley.newRequestQueue(this)
|
||||||
|
|
||||||
map = findViewById(R.id.detailMap)
|
map = findViewById(R.id.detailMap)
|
||||||
|
map.controller.setZoom(9.5)
|
||||||
mapPolygon = org.osmdroid.views.overlay.Polygon(map)
|
mapPolygon = org.osmdroid.views.overlay.Polygon(map)
|
||||||
mapMarker = Marker(map)
|
mapMarker = Marker(map)
|
||||||
mapPolyline = Polyline(map)
|
mapPolyline = Polyline(map)
|
||||||
|
@ -127,11 +123,15 @@ class CollarDetailActivity : AppCompatActivity() {
|
||||||
|
|
||||||
private fun updateAnimalHistory(points : List<GeoPoint>) {
|
private fun updateAnimalHistory(points : List<GeoPoint>) {
|
||||||
val currentPoint = points.first()
|
val currentPoint = points.first()
|
||||||
mapMarker.position = currentPoint
|
|
||||||
mapPolyline.setPoints(points)
|
|
||||||
collarPos.text = getString(R.string.collarSummaryLocation, currentPoint.longitude, currentPoint.latitude)
|
collarPos.text = getString(R.string.collarSummaryLocation, currentPoint.longitude, currentPoint.latitude)
|
||||||
if(!map.overlays.contains(mapMarker)) map.overlays.add(mapMarker)
|
if(!map.overlays.contains(mapMarker)) map.overlays.add(mapMarker)
|
||||||
if(!map.overlays.contains(mapPolyline)) map.overlays.add(mapPolyline)
|
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()
|
map.invalidate()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,6 @@ package com.danilafe.fencelessgrazing
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
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.Toast
|
import android.widget.Toast
|
||||||
import androidx.preference.PreferenceManager
|
import androidx.preference.PreferenceManager
|
||||||
import androidx.recyclerview.widget.DividerItemDecoration
|
import androidx.recyclerview.widget.DividerItemDecoration
|
||||||
|
@ -34,6 +33,7 @@ class CollarListActivity : AppCompatActivity() {
|
||||||
private lateinit var queue: RequestQueue
|
private lateinit var queue: RequestQueue
|
||||||
|
|
||||||
// The OpenStreetMap map.
|
// The OpenStreetMap map.
|
||||||
|
private var centerSet: Boolean = false
|
||||||
private lateinit var map: MapView
|
private lateinit var map: MapView
|
||||||
private val collarOverlays: MutableMap<Int, Marker> = mutableMapOf()
|
private val collarOverlays: MutableMap<Int, Marker> = mutableMapOf()
|
||||||
|
|
||||||
|
@ -51,6 +51,7 @@ class CollarListActivity : AppCompatActivity() {
|
||||||
|
|
||||||
token = intent.getStringExtra("token")!!
|
token = intent.getStringExtra("token")!!
|
||||||
map.setTileSource(TileSourceFactory.MAPNIK)
|
map.setTileSource(TileSourceFactory.MAPNIK)
|
||||||
|
map.controller.setZoom(9.5)
|
||||||
queue = Volley.newRequestQueue(this)
|
queue = Volley.newRequestQueue(this)
|
||||||
|
|
||||||
setupCollarList()
|
setupCollarList()
|
||||||
|
@ -76,7 +77,7 @@ class CollarListActivity : AppCompatActivity() {
|
||||||
summaries.clear()
|
summaries.clear()
|
||||||
summaries.addAll(it)
|
summaries.addAll(it)
|
||||||
summaryAdapter.notifyDataSetChanged()
|
summaryAdapter.notifyDataSetChanged()
|
||||||
updateMapOverlay()
|
updateMap()
|
||||||
},
|
},
|
||||||
Response.ErrorListener {
|
Response.ErrorListener {
|
||||||
Toast.makeText(this, "Failed to retrieve collar list!", Toast.LENGTH_SHORT).show()
|
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))
|
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() {
|
private fun updateMapOverlay() {
|
||||||
val currentSet = mutableSetOf<Int>()
|
val currentSet = mutableSetOf<Int>()
|
||||||
summaries.forEach {
|
summaries.forEach {
|
||||||
|
@ -132,6 +147,7 @@ class CollarListActivity : AppCompatActivity() {
|
||||||
collarOverlays.remove(it)
|
collarOverlays.remove(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
map.invalidate()
|
map.invalidate()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user