adding map markers kinda works

This commit is contained in:
geeksville
2020-03-30 12:47:01 -07:00
parent ecef170004
commit 915bd837ed
2 changed files with 66 additions and 13 deletions

View File

@@ -7,14 +7,22 @@ import androidx.compose.Composable
import androidx.compose.onCommit
import androidx.ui.core.ContextAmbient
import androidx.ui.fakeandroidview.AndroidView
import androidx.ui.layout.Column
import androidx.ui.material.MaterialTheme
import androidx.ui.tooling.preview.Preview
import com.geeksville.android.Logging
import com.geeksville.mesh.R
import com.geeksville.mesh.model.NodeDB
import com.geeksville.mesh.model.UIState
import com.mapbox.geojson.Feature
import com.mapbox.geojson.FeatureCollection
import com.mapbox.geojson.Point
import com.mapbox.mapboxsdk.maps.MapView
import com.mapbox.mapboxsdk.maps.Style
import com.mapbox.mapboxsdk.style.layers.Property
import com.mapbox.mapboxsdk.style.layers.PropertyFactory
import com.mapbox.mapboxsdk.style.layers.SymbolLayer
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource
object mapLog : Logging
@@ -74,23 +82,53 @@ fun MapContent() {
}
}
Column {
AndroidView(R.layout.map_view) { view ->
view as MapView
view.onCreate(UIState.savedInstanceState)
mapLifecycleCallbacks.view = view
(context.applicationContext as Application).registerActivityLifecycleCallbacks(
mapLifecycleCallbacks
// Find all nodes with valid locations
val locations = NodeDB.nodes.values.mapNotNull { node ->
val p = node.position
if (p != null && (p.latitude != 0.0 || p.longitude != 0.0))
Feature.fromGeometry(
Point.fromLngLat(
p.latitude,
p.longitude
)
)
else
null
}
val nodeSourceId = "node-positions"
val nodeLayerId = "node-layer"
val markerImageId = "my-marker-image"
val nodePositions =
GeoJsonSource(nodeSourceId, FeatureCollection.fromFeatures(locations))
view.getMapAsync { map ->
map.setStyle(Style.OUTDOORS) {
// Map is set up and the style has loaded. Now you can add data or make other map adjustments
}
// val markerIcon = BitmapFactory.decodeResource(context.resources, R.drawable.ic_twotone_person_pin_24)
val markerIcon = context.getDrawable(R.drawable.ic_twotone_person_pin_24)!!
val nodeLayer = SymbolLayer(nodeLayerId, nodeSourceId)
nodeLayer.setProperties(
PropertyFactory.iconImage(markerImageId),
PropertyFactory.iconAnchor(Property.ICON_ANCHOR_BOTTOM)
)
//Column {
AndroidView(R.layout.map_view) { view ->
view as MapView
view.onCreate(UIState.savedInstanceState)
mapLifecycleCallbacks.view = view
(context.applicationContext as Application).registerActivityLifecycleCallbacks(
mapLifecycleCallbacks
)
view.getMapAsync { map ->
map.setStyle(Style.OUTDOORS) { style ->
style.addImage(markerImageId, markerIcon)
style.addSource(nodePositions)
style.addLayer(nodeLayer)
}
}
}
//}
}