This commit is contained in:
Vadim Furman 2021-02-06 22:08:49 -08:00
parent be738787f0
commit 5ef6baef7a
3 changed files with 47 additions and 34 deletions

View file

@ -86,32 +86,34 @@ class MapFragment : ScreenFragment("Map"), Logging {
return FeatureCollection.fromFeatures(locations)
}
fun zoomToNodes(map: MapboxMap) {
if (nodesWithPosition.isNotEmpty()) {
val update = if (nodesWithPosition.size >= 2) {
// Multiple nodes, make them all fit on the map view
val bounds = LatLngBounds.Builder()
// Add all positions
bounds.includes(nodesWithPosition.map { it.position!! }
.map { LatLng(it.latitude, it.longitude) })
CameraUpdateFactory.newLatLngBounds(bounds.build(), 150)
} else {
// Only one node, just zoom in on it
val it = nodesWithPosition[0].position!!
val cameraPos = CameraPosition.Builder().target(
LatLng(it.latitude, it.longitude)
).zoom(9.0).build()
CameraUpdateFactory.newCameraPosition(cameraPos)
}
map.animateCamera(update, 1000)
}
}
nodePositions.setGeoJson(getCurrentNodes()) // Update node positions
zoomToNodes(map)
}
fun zoomToNodes(map: MapboxMap) {
val nodesWithPosition = model.nodeDB.nodes.value?.values?.filter { it.validPosition != null }
if (nodesWithPosition != null && nodesWithPosition.isNotEmpty()) {
val update = if (nodesWithPosition.size >= 2) {
// Multiple nodes, make them all fit on the map view
val bounds = LatLngBounds.Builder()
// Add all positions
bounds.includes(nodesWithPosition.map { it.position!! }
.map { LatLng(it.latitude, it.longitude) })
CameraUpdateFactory.newLatLngBounds(bounds.build(), 150)
} else {
// Only one node, just zoom in on it
val it = nodesWithPosition[0].position!!
val cameraPos = CameraPosition.Builder().target(
LatLng(it.latitude, it.longitude)
).zoom(9.0).build()
CameraUpdateFactory.newCameraPosition(cameraPos)
}
map.animateCamera(update, 1000)
}
}
override fun onCreateView(
@ -132,11 +134,14 @@ class MapFragment : ScreenFragment("Map"), Logging {
var mapView: MapView? = null
/**
* Mapbox native code can crash painfully if you ever call a mapbox view function while the view is not actively being show
*/
/**
* Mapbox native code can crash painfully if you ever call a mapbox view function while the view is not actively being show
*/
private val isViewVisible: Boolean
get() = view != null && isResumed && (mapView?.isDestroyed != false)
get() = view != null && isResumed && mapView != null && !(mapView!!.isDestroyed)
override fun onViewCreated(viewIn: View, savedInstanceState: Bundle?) {
super.onViewCreated(viewIn, savedInstanceState)
@ -164,9 +169,7 @@ class MapFragment : ScreenFragment("Map"), Logging {
style.addLayer(labelLayer)
}
//map.uiSettings.isScrollGesturesEnabled = true
//map.uiSettings.isZoomGesturesEnabled = true
map.uiSettings.isRotateGesturesEnabled = false
// Provide initial positions
model.nodeDB.nodes.value?.let { nodes ->
onNodesChanged(map, nodes.values)
@ -175,10 +178,11 @@ class MapFragment : ScreenFragment("Map"), Logging {
// Any times nodes change update our map
model.nodeDB.nodes.observe(viewLifecycleOwner, Observer { nodes ->
debug("Nodes updated! map visible = $isViewVisible")
if (isViewVisible)
debug("Nodes updated! map visible = $isViewVisible view!= null: ${view != null} isResumed: $isResumed mapview destroyed: ${mapView?.isDestroyed}")
// if (isViewVisible)
onNodesChanged(map, nodes.values)
})
zoomToNodes(map)
}
}
}