mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-03 21:53:55 -04:00
adding map markers kinda works
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
//}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user