Add more examples to MapLibre Andoid Example Documentation (#3038)
This commit is contained in:
@@ -24,7 +24,7 @@ import org.maplibre.android.utils.BitmapUtils
|
||||
* with an ImageSource
|
||||
*
|
||||
*
|
||||
* GL-native equivalent of https://maplibre.org/maplibre-gl-js-docs/example/animate-images/
|
||||
* MapLibre Native equivalent of https://maplibre.org/maplibre-gl-js/docs/examples/animate-images/
|
||||
*
|
||||
*/
|
||||
class AnimatedImageSourceActivity : AppCompatActivity(), OnMapReadyCallback {
|
||||
@@ -40,6 +40,7 @@ class AnimatedImageSourceActivity : AppCompatActivity(), OnMapReadyCallback {
|
||||
}
|
||||
|
||||
override fun onMapReady(map: MapLibreMap) {
|
||||
// --8<-- [start:onMapReady]
|
||||
val quad = LatLngQuad(
|
||||
LatLng(46.437, -80.425),
|
||||
LatLng(46.437, -71.516),
|
||||
@@ -50,7 +51,7 @@ class AnimatedImageSourceActivity : AppCompatActivity(), OnMapReadyCallback {
|
||||
val layer = RasterLayer(ID_IMAGE_LAYER, ID_IMAGE_SOURCE)
|
||||
map.setStyle(
|
||||
Style.Builder()
|
||||
.fromUri(TestStyles.getPredefinedStyleWithFallback("Streets"))
|
||||
.fromUri(TestStyles.AMERICANA)
|
||||
.withSource(imageSource)
|
||||
.withLayer(layer)
|
||||
) { style: Style? ->
|
||||
@@ -59,6 +60,7 @@ class AnimatedImageSourceActivity : AppCompatActivity(), OnMapReadyCallback {
|
||||
handler.postDelayed(it, 100)
|
||||
}
|
||||
}
|
||||
// --8<-- [end:onMapReady]
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
@@ -110,11 +112,13 @@ class AnimatedImageSourceActivity : AppCompatActivity(), OnMapReadyCallback {
|
||||
}
|
||||
|
||||
override fun run() {
|
||||
// --8<-- [start:setImage]
|
||||
imageSource.setImage(drawables[drawableIndex++]!!)
|
||||
if (drawableIndex > 3) {
|
||||
drawableIndex = 0
|
||||
}
|
||||
handler.postDelayed(this, 1000)
|
||||
// --8<-- [end:setImage]
|
||||
}
|
||||
|
||||
init {
|
||||
|
||||
@@ -35,7 +35,6 @@ class AnimatedSymbolLayerActivity : AppCompatActivity() {
|
||||
private val random = Random()
|
||||
private lateinit var mapView: MapView
|
||||
private lateinit var maplibreMap: MapLibreMap
|
||||
private var style: Style? = null
|
||||
private val randomCars: MutableList<Car> = ArrayList()
|
||||
private var randomCarSource: GeoJsonSource? = null
|
||||
private var taxi: Car? = null
|
||||
@@ -49,22 +48,21 @@ class AnimatedSymbolLayerActivity : AppCompatActivity() {
|
||||
mapView.onCreate(savedInstanceState)
|
||||
mapView.getMapAsync {
|
||||
maplibreMap = it
|
||||
it.setStyle(TestStyles.getPredefinedStyleWithFallback("Streets")) { style: Style? ->
|
||||
this.style = style
|
||||
setupCars()
|
||||
animateRandomRoutes()
|
||||
animateTaxi()
|
||||
it.setStyle(TestStyles.OPENFREEMAP_LIBERY) { style ->
|
||||
setupCars(style)
|
||||
animateRandomRoutes(style)
|
||||
animateTaxi(style)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun setupCars() {
|
||||
addRandomCars()
|
||||
addPassenger()
|
||||
addMainCar()
|
||||
private fun setupCars(style: Style) {
|
||||
addRandomCars(style)
|
||||
addPassenger(style)
|
||||
addTaxi(style)
|
||||
}
|
||||
|
||||
private fun animateRandomRoutes() {
|
||||
private fun animateRandomRoutes(style: Style) {
|
||||
val longestDrive = longestDrive
|
||||
val random = Random()
|
||||
for (car in randomCars) {
|
||||
@@ -85,7 +83,7 @@ class AnimatedSymbolLayerActivity : AppCompatActivity() {
|
||||
override fun onAnimationEnd(animation: Animator) {
|
||||
super.onAnimationEnd(animation)
|
||||
updateRandomDestinations()
|
||||
animateRandomRoutes()
|
||||
animateRandomRoutes(style)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -105,7 +103,8 @@ class AnimatedSymbolLayerActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun animateTaxi() {
|
||||
// --8<-- [start:animateTaxi]
|
||||
private fun animateTaxi(style: Style) {
|
||||
val valueAnimator = ValueAnimator.ofObject(LatLngEvaluator(), taxi!!.current, taxi!!.next)
|
||||
valueAnimator.addUpdateListener(object : AnimatorUpdateListener {
|
||||
private var latLng: LatLng? = null
|
||||
@@ -118,8 +117,8 @@ class AnimatedSymbolLayerActivity : AppCompatActivity() {
|
||||
valueAnimator.addListener(object : AnimatorListenerAdapter() {
|
||||
override fun onAnimationEnd(animation: Animator) {
|
||||
super.onAnimationEnd(animation)
|
||||
updatePassenger()
|
||||
animateTaxi()
|
||||
updatePassenger(style)
|
||||
animateTaxi(style)
|
||||
}
|
||||
})
|
||||
valueAnimator.addListener(object : AnimatorListenerAdapter() {
|
||||
@@ -134,15 +133,16 @@ class AnimatedSymbolLayerActivity : AppCompatActivity() {
|
||||
valueAnimator.start()
|
||||
animators.add(valueAnimator)
|
||||
}
|
||||
// --8<-- [end:animateTaxi]
|
||||
|
||||
private fun updatePassenger() {
|
||||
private fun updatePassenger(style: Style) {
|
||||
passenger = latLngInBounds
|
||||
updatePassengerSource()
|
||||
updatePassengerSource(style)
|
||||
taxi!!.setNext(passenger)
|
||||
}
|
||||
|
||||
private fun updatePassengerSource() {
|
||||
val source = style!!.getSourceAs<GeoJsonSource>(PASSENGER_SOURCE)
|
||||
private fun updatePassengerSource(style: Style) {
|
||||
val source = style.getSourceAs<GeoJsonSource>(PASSENGER_SOURCE)
|
||||
val featureCollection = FeatureCollection.fromFeatures(
|
||||
arrayOf(
|
||||
Feature.fromGeometry(
|
||||
@@ -198,7 +198,7 @@ class AnimatedSymbolLayerActivity : AppCompatActivity() {
|
||||
private val duration: Long
|
||||
get() = (random.nextInt(DURATION_RANDOM_MAX) + DURATION_BASE).toLong()
|
||||
|
||||
private fun addRandomCars() {
|
||||
private fun addRandomCars(style: Style) {
|
||||
var latLng: LatLng
|
||||
var next: LatLng
|
||||
for (i in 0..9) {
|
||||
@@ -216,12 +216,13 @@ class AnimatedSymbolLayerActivity : AppCompatActivity() {
|
||||
randomCars.add(
|
||||
Car(feature, next, duration)
|
||||
)
|
||||
|
||||
}
|
||||
randomCarSource = GeoJsonSource(RANDOM_CAR_SOURCE, featuresFromRoutes())
|
||||
style!!.addSource(randomCarSource!!)
|
||||
style!!.addImage(
|
||||
style.addSource(randomCarSource!!)
|
||||
style.addImage(
|
||||
RANDOM_CAR_IMAGE_ID,
|
||||
(ResourcesCompat.getDrawable(resources, R.drawable.ic_car_top, theme) as BitmapDrawable).bitmap
|
||||
ResourcesCompat.getDrawable(resources, R.drawable.ic_car_top, theme)!!
|
||||
)
|
||||
val symbolLayer = SymbolLayer(RANDOM_CAR_LAYER, RANDOM_CAR_SOURCE)
|
||||
symbolLayer.withProperties(
|
||||
@@ -230,10 +231,12 @@ class AnimatedSymbolLayerActivity : AppCompatActivity() {
|
||||
PropertyFactory.iconRotate(Expression.get(PROPERTY_BEARING)),
|
||||
PropertyFactory.iconIgnorePlacement(true)
|
||||
)
|
||||
style!!.addLayerBelow(symbolLayer, WATERWAY_LAYER_ID)
|
||||
|
||||
style.addLayerBelow(symbolLayer, "label_country_1")
|
||||
}
|
||||
|
||||
private fun addPassenger() {
|
||||
// --8<-- [start:addPassenger]
|
||||
private fun addPassenger(style: Style) {
|
||||
passenger = latLngInBounds
|
||||
val featureCollection = FeatureCollection.fromFeatures(
|
||||
arrayOf(
|
||||
@@ -245,22 +248,24 @@ class AnimatedSymbolLayerActivity : AppCompatActivity() {
|
||||
)
|
||||
)
|
||||
)
|
||||
style!!.addImage(
|
||||
style.addImage(
|
||||
PASSENGER,
|
||||
(ResourcesCompat.getDrawable(resources, R.drawable.icon_burned, theme) as BitmapDrawable).bitmap
|
||||
ResourcesCompat.getDrawable(resources, R.drawable.icon_burned, theme)!!
|
||||
)
|
||||
val geoJsonSource = GeoJsonSource(PASSENGER_SOURCE, featureCollection)
|
||||
style!!.addSource(geoJsonSource)
|
||||
style.addSource(geoJsonSource)
|
||||
val symbolLayer = SymbolLayer(PASSENGER_LAYER, PASSENGER_SOURCE)
|
||||
symbolLayer.withProperties(
|
||||
PropertyFactory.iconImage(PASSENGER),
|
||||
PropertyFactory.iconIgnorePlacement(true),
|
||||
PropertyFactory.iconAllowOverlap(true)
|
||||
)
|
||||
style!!.addLayerBelow(symbolLayer, RANDOM_CAR_LAYER)
|
||||
style.addLayerBelow(symbolLayer, RANDOM_CAR_LAYER)
|
||||
}
|
||||
// --8<-- [end:addPassenger]
|
||||
|
||||
private fun addMainCar() {
|
||||
// --8<-- [start:addTaxi]
|
||||
private fun addTaxi(style: Style) {
|
||||
val latLng = latLngInBounds
|
||||
val properties = JsonObject()
|
||||
properties.addProperty(PROPERTY_BEARING, Car.getBearing(latLng, passenger))
|
||||
@@ -273,12 +278,12 @@ class AnimatedSymbolLayerActivity : AppCompatActivity() {
|
||||
)
|
||||
val featureCollection = FeatureCollection.fromFeatures(arrayOf(feature))
|
||||
taxi = Car(feature, passenger, duration)
|
||||
style!!.addImage(
|
||||
style.addImage(
|
||||
TAXI,
|
||||
(ResourcesCompat.getDrawable(resources, R.drawable.ic_taxi_top, theme) as BitmapDrawable).bitmap
|
||||
)
|
||||
taxiSource = GeoJsonSource(TAXI_SOURCE, featureCollection)
|
||||
style!!.addSource(taxiSource!!)
|
||||
style.addSource(taxiSource!!)
|
||||
val symbolLayer = SymbolLayer(TAXI_LAYER, TAXI_SOURCE)
|
||||
symbolLayer.withProperties(
|
||||
PropertyFactory.iconImage(TAXI),
|
||||
@@ -286,17 +291,23 @@ class AnimatedSymbolLayerActivity : AppCompatActivity() {
|
||||
PropertyFactory.iconAllowOverlap(true),
|
||||
PropertyFactory.iconIgnorePlacement(true)
|
||||
)
|
||||
style!!.addLayer(symbolLayer)
|
||||
style.addLayer(symbolLayer)
|
||||
}
|
||||
// --8<-- [end:addTaxi]
|
||||
|
||||
|
||||
// --8<-- [start:latLngInBounds]
|
||||
private val latLngInBounds: LatLng
|
||||
get() {
|
||||
val bounds = maplibreMap.projection.visibleRegion.latLngBounds
|
||||
val generator = Random()
|
||||
val randomLat = bounds.latitudeSouth + generator.nextDouble() * bounds.latitudeNorth - bounds.latitudeSouth
|
||||
val randomLon = bounds.longitudeWest + generator.nextDouble() * bounds.longitudeEast - bounds.longitudeWest
|
||||
|
||||
val randomLat = bounds.latitudeSouth + generator.nextDouble() * (bounds.latitudeNorth - bounds.latitudeSouth)
|
||||
val randomLon = bounds.longitudeWest + generator.nextDouble() * (bounds.longitudeEast - bounds.longitudeWest)
|
||||
|
||||
return LatLng(randomLat, randomLon)
|
||||
}
|
||||
// --8<-- [end:latLngInBounds]
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
|
||||
@@ -41,7 +41,7 @@ class BuildingFillExtrusionActivity : AppCompatActivity() {
|
||||
if (map != null) {
|
||||
maplibreMap = map
|
||||
}
|
||||
maplibreMap.setStyle(TestStyles.getPredefinedStyleWithFallback("Streets")) { style: Style ->
|
||||
maplibreMap.setStyle(TestStyles.OPENFREEMAP_BRIGHT) { style: Style ->
|
||||
setupBuildings(style)
|
||||
setupLight()
|
||||
}
|
||||
@@ -50,37 +50,43 @@ class BuildingFillExtrusionActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
private fun setupBuildings(style: Style) {
|
||||
val fillExtrusionLayer = FillExtrusionLayer("3d-buildings", "composite")
|
||||
// --8<-- [start:setupBuildings]
|
||||
val fillExtrusionLayer = FillExtrusionLayer("building-3d", "openmaptiles")
|
||||
fillExtrusionLayer.sourceLayer = "building"
|
||||
fillExtrusionLayer.setFilter(
|
||||
Expression.eq(
|
||||
Expression.get("extrude"),
|
||||
Expression.literal("true")
|
||||
Expression.all(
|
||||
Expression.has("render_height"),
|
||||
Expression.has("render_min_height")
|
||||
)
|
||||
)
|
||||
fillExtrusionLayer.minZoom = 15f
|
||||
fillExtrusionLayer.setProperties(
|
||||
PropertyFactory.fillExtrusionColor(Color.LTGRAY),
|
||||
PropertyFactory.fillExtrusionHeight(Expression.get("height")),
|
||||
PropertyFactory.fillExtrusionBase(Expression.get("min_height")),
|
||||
PropertyFactory.fillExtrusionHeight(Expression.get("render_height")),
|
||||
PropertyFactory.fillExtrusionBase(Expression.get("render_min_height")),
|
||||
PropertyFactory.fillExtrusionOpacity(0.9f)
|
||||
)
|
||||
style.addLayer(fillExtrusionLayer)
|
||||
// --8<-- [end:setupBuildings]
|
||||
}
|
||||
|
||||
private fun setupLight() {
|
||||
light = maplibreMap.style!!.light
|
||||
findViewById<View>(R.id.fabLightPosition).setOnClickListener { v: View? ->
|
||||
// --8<-- [start:lightPosition]
|
||||
isInitPosition = !isInitPosition
|
||||
if (isInitPosition) {
|
||||
light!!.position = Position(1.5f, 90f, 80f)
|
||||
} else {
|
||||
light!!.position = Position(1.15f, 210f, 30f)
|
||||
}
|
||||
// --8<-- [end:lightPosition]
|
||||
}
|
||||
findViewById<View>(R.id.fabLightColor).setOnClickListener { v: View? ->
|
||||
// --8<-- [start:lightColor]
|
||||
isRedColor = !isRedColor
|
||||
light!!.setColor(ColorUtils.colorToRgbaString(if (isRedColor) Color.RED else Color.BLUE))
|
||||
// --8<-- [end:lightColor]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,13 +4,12 @@ import android.graphics.Color
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.appcompat.content.res.AppCompatResources
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.content.res.ResourcesCompat
|
||||
import com.google.android.material.floatingactionbutton.FloatingActionButton
|
||||
import org.maplibre.android.maps.MapView
|
||||
import org.maplibre.android.maps.MapView.OnDidFinishLoadingStyleListener
|
||||
import org.maplibre.android.maps.MapLibreMap
|
||||
import org.maplibre.android.maps.OnMapReadyCallback
|
||||
import org.maplibre.android.maps.Style
|
||||
import org.maplibre.android.style.expressions.Expression
|
||||
import org.maplibre.android.style.layers.CircleLayer
|
||||
@@ -42,52 +41,59 @@ class CircleLayerActivity : AppCompatActivity(), View.OnClickListener {
|
||||
private var isLoadingStyle = true
|
||||
public override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
setContentView(R.layout.activity_circle_layer)
|
||||
mapView = findViewById(R.id.mapView)
|
||||
mapView.onCreate(savedInstanceState)
|
||||
mapView.getMapAsync(
|
||||
OnMapReadyCallback { map: MapLibreMap? ->
|
||||
if (map != null) {
|
||||
maplibreMap = map
|
||||
}
|
||||
maplibreMap.setStyle(TestStyles.getPredefinedStyleWithFallback("Satellite Hybrid"))
|
||||
mapView.addOnDidFinishLoadingStyleListener(
|
||||
OnDidFinishLoadingStyleListener {
|
||||
val style = maplibreMap.style
|
||||
addBusStopSource(style)
|
||||
addBusStopCircleLayer(style)
|
||||
initFloatingActionButtons()
|
||||
isLoadingStyle = false
|
||||
}
|
||||
)
|
||||
mapView.getMapAsync { map: MapLibreMap? ->
|
||||
if (map != null) {
|
||||
maplibreMap = map
|
||||
}
|
||||
)
|
||||
maplibreMap.setStyle(TestStyles.PROTOMAPS_WHITE) {
|
||||
maplibreMap.getStyle { style ->
|
||||
addBusStopSource(style)
|
||||
addBusStopCircleLayer(style)
|
||||
initFloatingActionButtons()
|
||||
isLoadingStyle = false
|
||||
|
||||
style.addImage(
|
||||
"bus-icon",
|
||||
AppCompatResources.getDrawable(this, R.drawable.ic_directions_bus_black)!!
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun addBusStopSource(style: Style?) {
|
||||
private fun addBusStopSource(style: Style) {
|
||||
// --8<-- [start:addBusStopSource]
|
||||
try {
|
||||
source = GeoJsonSource(SOURCE_ID, URI(URL_BUS_ROUTES))
|
||||
} catch (exception: URISyntaxException) {
|
||||
Timber.e(exception, "That's not an url... ")
|
||||
}
|
||||
style!!.addSource(source!!)
|
||||
style.addSource(source!!)
|
||||
// --8<-- [end:addBusStopSource]
|
||||
}
|
||||
|
||||
private fun addBusStopCircleLayer(style: Style?) {
|
||||
private fun addBusStopCircleLayer(style: Style) {
|
||||
// --8<-- [start:addBusStopCircleLayer]
|
||||
layer = CircleLayer(LAYER_ID, SOURCE_ID)
|
||||
layer!!.setProperties(
|
||||
PropertyFactory.circleColor(Color.parseColor("#FF9800")),
|
||||
PropertyFactory.circleRadius(2.0f)
|
||||
)
|
||||
style!!.addLayerBelow(layer!!, "water_intermittent")
|
||||
style.addLayer(layer!!)
|
||||
// --8<-- [end:addBusStopCircleLayer]
|
||||
|
||||
}
|
||||
|
||||
private fun initFloatingActionButtons() {
|
||||
routeFab = findViewById(R.id.fab_route)
|
||||
routeFab.setColorFilter(ContextCompat.getColor(this@CircleLayerActivity, R.color.primary))
|
||||
routeFab.setOnClickListener(this@CircleLayerActivity)
|
||||
routeFab.setColorFilter(ContextCompat.getColor(this, R.color.primary))
|
||||
routeFab.setOnClickListener(this)
|
||||
styleFab = findViewById(R.id.fab_style)
|
||||
styleFab.setOnClickListener(this@CircleLayerActivity)
|
||||
styleFab.setOnClickListener(this)
|
||||
}
|
||||
|
||||
override fun onClick(view: View) {
|
||||
@@ -113,102 +119,107 @@ class CircleLayerActivity : AppCompatActivity(), View.OnClickListener {
|
||||
}
|
||||
|
||||
private fun addClusteredSource() {
|
||||
try {
|
||||
maplibreMap.style!!.addSource(
|
||||
GeoJsonSource(
|
||||
SOURCE_ID_CLUSTER,
|
||||
URI(URL_BUS_ROUTES),
|
||||
GeoJsonOptions()
|
||||
.withCluster(true)
|
||||
.withClusterMaxZoom(14)
|
||||
.withClusterRadius(50)
|
||||
maplibreMap.getStyle { style ->
|
||||
try {
|
||||
// --8<-- [start:addClusteredSource]
|
||||
style.addSource(
|
||||
GeoJsonSource(
|
||||
SOURCE_ID_CLUSTER,
|
||||
URI(URL_BUS_ROUTES),
|
||||
GeoJsonOptions()
|
||||
.withCluster(true)
|
||||
.withClusterMaxZoom(14)
|
||||
.withClusterRadius(50)
|
||||
)
|
||||
)
|
||||
)
|
||||
} catch (malformedUrlException: URISyntaxException) {
|
||||
Timber.e(malformedUrlException, "That's not an url... ")
|
||||
}
|
||||
// --8<-- [end:addClusteredSource]
|
||||
} catch (malformedUrlException: URISyntaxException) {
|
||||
Timber.e(malformedUrlException, "That's not an url... ")
|
||||
}
|
||||
|
||||
// Add unclustered layer
|
||||
val layers = arrayOf(
|
||||
intArrayOf(
|
||||
150,
|
||||
ResourcesCompat.getColor(
|
||||
// --8<-- [start:unclusteredLayer]
|
||||
val unclustered = SymbolLayer("unclustered-points", SOURCE_ID_CLUSTER)
|
||||
unclustered.setProperties(
|
||||
PropertyFactory.iconImage("bus-icon"),
|
||||
)
|
||||
unclustered.setFilter(
|
||||
Expression.neq(Expression.get("cluster"), true)
|
||||
)
|
||||
style.addLayer(unclustered)
|
||||
// --8<-- [end:unclusteredLayer]
|
||||
|
||||
// --8<-- [start:clusteredCircleLayers]
|
||||
val layers = arrayOf(
|
||||
150 to ResourcesCompat.getColor(
|
||||
resources,
|
||||
R.color.redAccent,
|
||||
theme
|
||||
)
|
||||
),
|
||||
intArrayOf(20, ResourcesCompat.getColor(resources, R.color.greenAccent, theme)),
|
||||
intArrayOf(
|
||||
0,
|
||||
ResourcesCompat.getColor(
|
||||
),
|
||||
20 to ResourcesCompat.getColor(resources, R.color.greenAccent, theme),
|
||||
0 to ResourcesCompat.getColor(
|
||||
resources,
|
||||
R.color.blueAccent,
|
||||
theme
|
||||
)
|
||||
)
|
||||
)
|
||||
val unclustered = SymbolLayer("unclustered-points", SOURCE_ID_CLUSTER)
|
||||
unclustered.setProperties(
|
||||
PropertyFactory.iconImage("bus-15")
|
||||
)
|
||||
maplibreMap.style!!.addLayer(unclustered)
|
||||
for (i in layers.indices) {
|
||||
// Add some nice circles
|
||||
val circles = CircleLayer("cluster-$i", SOURCE_ID_CLUSTER)
|
||||
circles.setProperties(
|
||||
PropertyFactory.circleColor(layers[i][1]),
|
||||
PropertyFactory.circleRadius(18f)
|
||||
)
|
||||
val pointCount = Expression.toNumber(Expression.get("point_count"))
|
||||
circles.setFilter(
|
||||
if (i == 0) {
|
||||
Expression.all(
|
||||
Expression.has("point_count"),
|
||||
Expression.gte(
|
||||
pointCount,
|
||||
Expression.literal(
|
||||
layers[i][0]
|
||||
)
|
||||
)
|
||||
// --8<-- [end:clusteredCircleLayers]
|
||||
|
||||
)
|
||||
} else {
|
||||
Expression.all(
|
||||
Expression.has("point_count"),
|
||||
Expression.gt(
|
||||
pointCount,
|
||||
Expression.literal(
|
||||
layers[i][0]
|
||||
)
|
||||
),
|
||||
Expression.lt(
|
||||
pointCount,
|
||||
Expression.literal(
|
||||
layers[i - 1][0]
|
||||
// --8<-- [start:clusteredCircleLayersLoop]
|
||||
for (i in layers.indices) {
|
||||
// Add some nice circles
|
||||
val circles = CircleLayer("cluster-$i", SOURCE_ID_CLUSTER)
|
||||
circles.setProperties(
|
||||
PropertyFactory.circleColor(layers[i].second),
|
||||
PropertyFactory.circleRadius(18f)
|
||||
)
|
||||
|
||||
val pointCount = Expression.toNumber(Expression.get("point_count"))
|
||||
circles.setFilter(
|
||||
if (i == 0) {
|
||||
Expression.all(
|
||||
Expression.has("point_count"),
|
||||
Expression.gte(
|
||||
pointCount,
|
||||
Expression.literal(layers[i].first)
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
} else {
|
||||
Expression.all(
|
||||
Expression.has("point_count"),
|
||||
Expression.gt(
|
||||
pointCount,
|
||||
Expression.literal(layers[i].first)
|
||||
),
|
||||
Expression.lt(
|
||||
pointCount,
|
||||
Expression.literal(layers[i - 1].first)
|
||||
)
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
style.addLayer(circles)
|
||||
}
|
||||
// --8<-- [end:clusteredCircleLayersLoop]
|
||||
|
||||
// --8<-- [start:countLabels]
|
||||
val count = SymbolLayer("count", SOURCE_ID_CLUSTER)
|
||||
count.setProperties(
|
||||
PropertyFactory.textField(Expression.toString(Expression.get("point_count"))),
|
||||
PropertyFactory.textFont(arrayOf("Noto Sans Medium")),
|
||||
PropertyFactory.textSize(12f),
|
||||
PropertyFactory.textColor(Color.WHITE),
|
||||
PropertyFactory.textIgnorePlacement(true),
|
||||
PropertyFactory.textAllowOverlap(true)
|
||||
)
|
||||
maplibreMap.style!!.addLayer(circles)
|
||||
style.addLayer(count)
|
||||
// --8<-- [end:countLabels]
|
||||
}
|
||||
|
||||
// Add the count labels
|
||||
val count = SymbolLayer("count", SOURCE_ID_CLUSTER)
|
||||
count.setProperties(
|
||||
PropertyFactory.textField(Expression.toString(Expression.get("point_count"))),
|
||||
PropertyFactory.textSize(12f),
|
||||
PropertyFactory.textColor(Color.WHITE),
|
||||
PropertyFactory.textIgnorePlacement(true),
|
||||
PropertyFactory.textAllowOverlap(true)
|
||||
)
|
||||
maplibreMap.style!!.addLayer(count)
|
||||
}
|
||||
|
||||
private fun removeFabs() {
|
||||
routeFab!!.visibility = View.GONE
|
||||
styleFab!!.visibility = View.GONE
|
||||
routeFab.visibility = View.GONE
|
||||
styleFab.visibility = View.GONE
|
||||
}
|
||||
|
||||
private fun changeMapStyle() {
|
||||
@@ -226,13 +237,8 @@ class CircleLayerActivity : AppCompatActivity(), View.OnClickListener {
|
||||
maplibreMap.setStyle(Style.Builder().fromUri(nextStyle))
|
||||
}
|
||||
|
||||
private fun addBusStop() {
|
||||
maplibreMap.style!!.addLayer(layer!!)
|
||||
maplibreMap.style!!.addSource(source!!)
|
||||
}
|
||||
|
||||
private val nextStyle: String
|
||||
private get() {
|
||||
get() {
|
||||
currentStyleIndex++
|
||||
if (currentStyleIndex == Data.STYLES.size) {
|
||||
currentStyleIndex = 0
|
||||
@@ -277,12 +283,8 @@ class CircleLayerActivity : AppCompatActivity(), View.OnClickListener {
|
||||
|
||||
private object Data {
|
||||
val STYLES = arrayOf(
|
||||
TestStyles.getPredefinedStyleWithFallback("Streets"),
|
||||
TestStyles.getPredefinedStyleWithFallback("Outdoor"),
|
||||
TestStyles.getPredefinedStyleWithFallback("Bright"),
|
||||
TestStyles.getPredefinedStyleWithFallback("Pastel"),
|
||||
TestStyles.getPredefinedStyleWithFallback("Satellite Hybrid"),
|
||||
TestStyles.getPredefinedStyleWithFallback("Satellite Hybrid")
|
||||
TestStyles.PROTOMAPS_WHITE,
|
||||
TestStyles.PROTOMAPS_LIGHT
|
||||
)
|
||||
}
|
||||
|
||||
@@ -290,7 +292,7 @@ class CircleLayerActivity : AppCompatActivity(), View.OnClickListener {
|
||||
const val SOURCE_ID = "bus_stop"
|
||||
const val SOURCE_ID_CLUSTER = "bus_stop_cluster"
|
||||
const val URL_BUS_ROUTES =
|
||||
"https://raw.githubusercontent.com/cheeaun/busrouter-sg/master/data/2/bus-stops.geojson"
|
||||
"https://s3.eu-central-1.amazonaws.com/maplibre-native/android-documentation-resources/bus-stops.geojson"
|
||||
const val LAYER_ID = "stops_layer"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ class CustomSpriteActivity : AppCompatActivity() {
|
||||
mapView.onCreate(savedInstanceState)
|
||||
mapView.getMapAsync {
|
||||
maplibreMap = it
|
||||
it.setStyle(TestStyles.getPredefinedStyleWithFallback("Streets")) { style: Style ->
|
||||
it.setStyle(TestStyles.OPENFREEMAP_LIBERY) { style: Style ->
|
||||
val fab = findViewById<FloatingActionButton>(R.id.fab)
|
||||
fab.setColorFilter(
|
||||
ContextCompat.getColor(
|
||||
@@ -50,6 +50,7 @@ class CustomSpriteActivity : AppCompatActivity() {
|
||||
override fun onClick(view: View) {
|
||||
if (!this::point.isInitialized) {
|
||||
Timber.i("First click -> Car")
|
||||
// --8<-- [start:addImage]
|
||||
// Add an icon to reference later
|
||||
style.addImage(
|
||||
CUSTOM_ICON,
|
||||
@@ -76,8 +77,9 @@ class CustomSpriteActivity : AppCompatActivity() {
|
||||
)
|
||||
|
||||
// lets add a circle below labels!
|
||||
maplibreMap.style!!.addLayerBelow(layer, "water_intermittent")
|
||||
maplibreMap.style!!.addLayerBelow(layer, "water-intermittent")
|
||||
fab.setImageResource(R.drawable.ic_directions_car_black)
|
||||
// --8<-- [end:addImage]
|
||||
} else {
|
||||
// Update point
|
||||
point = Point.fromLngLat(
|
||||
|
||||
@@ -42,7 +42,7 @@ class DataDrivenStyleActivity : AppCompatActivity() {
|
||||
mapView.getMapAsync {
|
||||
// Store for later
|
||||
maplibreMap = it
|
||||
it.setStyle(TestStyles.getPredefinedStyleWithFallback("Streets")) { style: Style? ->
|
||||
it.setStyle(TestStyles.VERSATILES) { style: Style? ->
|
||||
// Add a parks layer
|
||||
addParksLayer()
|
||||
|
||||
@@ -155,42 +155,56 @@ class DataDrivenStyleActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private fun addExponentialZoomFunction() {
|
||||
Timber.i("Add exponential zoom function")
|
||||
val layer = maplibreMap.style!!.getLayerAs<FillLayer>("water")!!
|
||||
layer.setProperties(
|
||||
PropertyFactory.fillColor(
|
||||
Expression.interpolate(
|
||||
Expression.exponential(0.5f),
|
||||
Expression.zoom(),
|
||||
Expression.stop(1, Expression.color(Color.RED)),
|
||||
Expression.stop(5, Expression.color(Color.BLUE)),
|
||||
Expression.stop(10, Expression.color(Color.GREEN))
|
||||
)
|
||||
)
|
||||
)
|
||||
Timber.i("Fill color: %s", layer.fillColor)
|
||||
maplibreMap.getStyle { style ->
|
||||
style.layers.filter { it.id.startsWith("water") }.filterIsInstance<FillLayer>()
|
||||
.forEach { layer ->
|
||||
// --8<-- [start:addExponentialZoomFunction]
|
||||
layer.setProperties(
|
||||
PropertyFactory.fillColor(
|
||||
Expression.interpolate(
|
||||
Expression.exponential(0.5f),
|
||||
Expression.zoom(),
|
||||
Expression.stop(1, Expression.color(Color.RED)),
|
||||
Expression.stop(5, Expression.color(Color.BLUE)),
|
||||
Expression.stop(10, Expression.color(Color.GREEN))
|
||||
)
|
||||
)
|
||||
)
|
||||
// --8<-- [end:addExponentialZoomFunction]
|
||||
Timber.i("Fill color: %s", layer.fillColor)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun addIntervalZoomFunction() {
|
||||
Timber.i("Add interval zoom function")
|
||||
val layer = maplibreMap.style!!.getLayerAs<FillLayer>("water")!!
|
||||
layer.setProperties(
|
||||
PropertyFactory.fillColor(
|
||||
Expression.step(
|
||||
Expression.zoom(),
|
||||
Expression.rgba(0.0f, 255.0f, 255.0f, 1.0f),
|
||||
Expression.stop(1, Expression.rgba(255.0f, 0.0f, 0.0f, 1.0f)),
|
||||
Expression.stop(5, Expression.rgba(0.0f, 0.0f, 255.0f, 1.0f)),
|
||||
Expression.stop(10, Expression.rgba(0.0f, 255.0f, 0.0f, 1.0f))
|
||||
maplibreMap.getStyle { style ->
|
||||
style.layers.filter { it.id.startsWith("water") }.filterIsInstance<FillLayer>().forEach { layer ->
|
||||
// --8<-- [start:addIntervalZoomFunction]
|
||||
layer.setProperties(
|
||||
PropertyFactory.fillColor(
|
||||
Expression.step(
|
||||
Expression.zoom(),
|
||||
Expression.rgba(0.0f, 255.0f, 255.0f, 1.0f),
|
||||
Expression.stop(1, Expression.rgba(255.0f, 0.0f, 0.0f, 1.0f)),
|
||||
Expression.stop(5, Expression.rgba(0.0f, 0.0f, 255.0f, 1.0f)),
|
||||
Expression.stop(10, Expression.rgba(0.0f, 255.0f, 0.0f, 1.0f))
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
Timber.i("Fill color: %s", layer.fillColor)
|
||||
// --8<-- [end:addIntervalZoomFunction]
|
||||
|
||||
Timber.i("Fill color: %s", layer.fillColor)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun addExponentialSourceFunction() {
|
||||
Timber.i("Add exponential source function")
|
||||
// --8<-- [start:addExponentialSourceFunction]
|
||||
val layer = maplibreMap.style!!.getLayerAs<FillLayer>(AMSTERDAM_PARKS_LAYER)!!
|
||||
layer.setProperties(
|
||||
PropertyFactory.fillColor(
|
||||
@@ -203,11 +217,13 @@ class DataDrivenStyleActivity : AppCompatActivity() {
|
||||
)
|
||||
)
|
||||
)
|
||||
// --8<-- [end:addExponentialSourceFunction]
|
||||
Timber.i("Fill color: %s", layer.fillColor)
|
||||
}
|
||||
|
||||
private fun addCategoricalSourceFunction() {
|
||||
Timber.i("Add categorical source function")
|
||||
// --8<-- [start:addCategoricalSourceFunction]
|
||||
val layer = maplibreMap.style!!.getLayerAs<FillLayer>(AMSTERDAM_PARKS_LAYER)!!
|
||||
layer.setProperties(
|
||||
PropertyFactory.fillColor(
|
||||
@@ -223,22 +239,27 @@ class DataDrivenStyleActivity : AppCompatActivity() {
|
||||
)
|
||||
)
|
||||
)
|
||||
// --8<-- [end:addCategoricalSourceFunction]
|
||||
|
||||
Timber.i("Fill color: %s", layer.fillColor)
|
||||
}
|
||||
|
||||
private fun addIdentitySourceFunction() {
|
||||
Timber.i("Add identity source function")
|
||||
// --8<-- [start:addIdentitySourceFunction]
|
||||
val layer = maplibreMap.style!!.getLayerAs<FillLayer>(AMSTERDAM_PARKS_LAYER)!!
|
||||
layer.setProperties(
|
||||
PropertyFactory.fillOpacity(
|
||||
Expression.get("fill-opacity")
|
||||
)
|
||||
)
|
||||
// --8<-- [end:addIdentitySourceFunction]
|
||||
Timber.i("Fill opacity: %s", layer.fillOpacity)
|
||||
}
|
||||
|
||||
private fun addIntervalSourceFunction() {
|
||||
Timber.i("Add interval source function")
|
||||
// --8<-- [start:addIntervalSourceFunction]
|
||||
val layer = maplibreMap.style!!.getLayerAs<FillLayer>(AMSTERDAM_PARKS_LAYER)!!
|
||||
layer.setProperties(
|
||||
PropertyFactory.fillColor(
|
||||
@@ -251,11 +272,13 @@ class DataDrivenStyleActivity : AppCompatActivity() {
|
||||
)
|
||||
)
|
||||
)
|
||||
// --8<-- [end:addIntervalSourceFunction]
|
||||
Timber.i("Fill color: %s", layer.fillColor)
|
||||
}
|
||||
|
||||
private fun addCompositeExponentialFunction() {
|
||||
Timber.i("Add composite exponential function")
|
||||
// --8<-- [start:addCompositeExponentialFunction]
|
||||
val layer = maplibreMap.style!!.getLayerAs<FillLayer>(AMSTERDAM_PARKS_LAYER)!!
|
||||
layer.setProperties(
|
||||
PropertyFactory.fillColor(
|
||||
@@ -295,11 +318,13 @@ class DataDrivenStyleActivity : AppCompatActivity() {
|
||||
)
|
||||
)
|
||||
)
|
||||
// --8<-- [end:addCompositeExponentialFunction]
|
||||
Timber.i("Fill color: %s", layer.fillColor)
|
||||
}
|
||||
|
||||
private fun addCompositeIntervalFunction() {
|
||||
Timber.i("Add composite interval function")
|
||||
// --8<-- [start:addCompositeIntervalFunction]
|
||||
val layer = maplibreMap.style!!.getLayerAs<FillLayer>(AMSTERDAM_PARKS_LAYER)!!
|
||||
layer.setProperties(
|
||||
PropertyFactory.fillColor(
|
||||
@@ -339,11 +364,13 @@ class DataDrivenStyleActivity : AppCompatActivity() {
|
||||
)
|
||||
)
|
||||
)
|
||||
// --8<-- [end:addCompositeIntervalFunction]
|
||||
Timber.i("Fill color: %s", layer.fillColor)
|
||||
}
|
||||
|
||||
private fun addCompositeCategoricalFunction() {
|
||||
Timber.i("Add composite categorical function")
|
||||
// --8<-- [start:addCompositeCategoricalFunction]
|
||||
val layer = maplibreMap.style!!.getLayerAs<FillLayer>(AMSTERDAM_PARKS_LAYER)!!
|
||||
layer.setProperties(
|
||||
PropertyFactory.fillColor(
|
||||
@@ -503,6 +530,7 @@ class DataDrivenStyleActivity : AppCompatActivity() {
|
||||
)
|
||||
)
|
||||
)
|
||||
// --8<-- [end:addCompositeCategoricalFunction]
|
||||
Timber.i("Fill color: %s", layer.fillColor)
|
||||
}
|
||||
|
||||
|
||||
@@ -51,13 +51,12 @@ class DistanceExpressionActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
private fun setupStyle() {
|
||||
// --8<-- [start:FillLayer]
|
||||
val center = Point.fromLngLat(lon, lat)
|
||||
val circle = TurfTransformation.circle(center, 150.0, TurfConstants.UNIT_METRES)
|
||||
// Setup style with additional layers,
|
||||
// using Streets as a base style
|
||||
maplibreMap.setStyle(
|
||||
Style.Builder()
|
||||
.fromUri(TestStyles.getPredefinedStyleWithFallback("Streets"))
|
||||
.fromUri(TestStyles.OPENFREEMAP_BRIGHT)
|
||||
.withSources(
|
||||
GeoJsonSource(
|
||||
POINT_ID,
|
||||
@@ -71,24 +70,26 @@ class DistanceExpressionActivity : AppCompatActivity() {
|
||||
fillOpacity(0.5f),
|
||||
fillColor(Color.parseColor("#3bb2d0"))
|
||||
),
|
||||
"poi-label"
|
||||
"poi"
|
||||
)
|
||||
// --8<-- [end:FillLayer]
|
||||
) { style ->
|
||||
// Show only POI labels inside circle radius using distance expression
|
||||
val symbolLayer = style.getLayer("poi_z16") as SymbolLayer
|
||||
symbolLayer.setFilter(
|
||||
lt(
|
||||
distance(
|
||||
Point.fromLngLat(lon, lat)
|
||||
),
|
||||
150
|
||||
)
|
||||
)
|
||||
|
||||
// Hide other types of labels to highlight POI labels
|
||||
(style.getLayer("road_label") as SymbolLayer?)?.setProperties(visibility(NONE))
|
||||
(style.getLayer("airport-label-major") as SymbolLayer?)?.setProperties(visibility(NONE))
|
||||
(style.getLayer("poi_transit") as SymbolLayer?)?.setProperties(visibility(NONE))
|
||||
// --8<-- [start:distanceExpression]
|
||||
for (layer in style.layers) {
|
||||
if (layer is SymbolLayer) {
|
||||
if (layer.id.startsWith("poi")) {
|
||||
layer.setFilter(lt(
|
||||
distance(
|
||||
Point.fromLngLat(lon, lat)
|
||||
),
|
||||
150
|
||||
))
|
||||
} else {
|
||||
layer.setProperties(visibility(NONE))
|
||||
}
|
||||
}
|
||||
}
|
||||
// --8<-- [end:distanceExpression]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,7 +125,7 @@ class DistanceExpressionActivity : AppCompatActivity() {
|
||||
|
||||
override fun onSaveInstanceState(outState: Bundle, outPersistentState: PersistableBundle) {
|
||||
super.onSaveInstanceState(outState, outPersistentState)
|
||||
outState?.let {
|
||||
outState.let {
|
||||
binding.mapView.onSaveInstanceState(it)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ class DraggableMarkerActivity : AppCompatActivity() {
|
||||
|
||||
maplibreMap.setStyle(
|
||||
Style.Builder()
|
||||
.fromUri(TestStyles.getPredefinedStyleWithFallback("Streets"))
|
||||
.fromUri(TestStyles.PROTOMAPS_LIGHT)
|
||||
.withImage(markerImageId, IconFactory.getInstance(this).defaultMarker().bitmap)
|
||||
.withSource(source)
|
||||
.withLayer(layer)
|
||||
@@ -91,6 +91,7 @@ class DraggableMarkerActivity : AppCompatActivity() {
|
||||
)
|
||||
)
|
||||
|
||||
// --8<-- [start:addOnMapClickListener]
|
||||
maplibreMap.addOnMapClickListener {
|
||||
// Adding a marker on map click
|
||||
val features = maplibreMap.queryRenderedSymbols(it, layerId)
|
||||
@@ -108,7 +109,9 @@ class DraggableMarkerActivity : AppCompatActivity() {
|
||||
|
||||
false
|
||||
}
|
||||
// --8<-- [end:addOnMapClickListener]
|
||||
|
||||
// --8<-- [start:draggableSymbolsManager]
|
||||
draggableSymbolsManager = DraggableSymbolsManager(
|
||||
mapView,
|
||||
maplibreMap,
|
||||
@@ -148,22 +151,25 @@ class DraggableMarkerActivity : AppCompatActivity() {
|
||||
.show()
|
||||
}
|
||||
})
|
||||
// --8<-- [end:draggableSymbolsManager]
|
||||
}
|
||||
}
|
||||
|
||||
// --8<-- [start:addMarker]
|
||||
private fun addMarker(latLng: LatLng) {
|
||||
featureCollection.features()?.add(
|
||||
Feature.fromGeometry(Point.fromLngLat(latLng.longitude, latLng.latitude), null, generateMarkerId())
|
||||
)
|
||||
source.setGeoJson(featureCollection)
|
||||
}
|
||||
// --8<-- [end:addMarker]
|
||||
|
||||
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
|
||||
// Dispatching parent's touch events to the manager
|
||||
draggableSymbolsManager?.onParentTouchEvent(ev)
|
||||
return super.dispatchTouchEvent(ev)
|
||||
}
|
||||
|
||||
// --8<-- [start:DraggableSymbolsManager]
|
||||
/**
|
||||
* A manager, that allows dragging symbols after they are long clicked.
|
||||
* Since this manager lives outside of the Maps SDK, we need to intercept parent's motion events
|
||||
@@ -201,7 +207,7 @@ class DraggableMarkerActivity : AppCompatActivity() {
|
||||
|
||||
private val androidGesturesManager: AndroidGesturesManager = AndroidGesturesManager(mapView.context, false)
|
||||
private var draggedSymbolId: String? = null
|
||||
private val onSymbolDragListeners: MutableList<OnSymbolDragListener> = mutableListOf<OnSymbolDragListener>()
|
||||
private val onSymbolDragListeners: MutableList<OnSymbolDragListener> = mutableListOf()
|
||||
|
||||
init {
|
||||
maplibreMap.addOnMapLongClickListener {
|
||||
@@ -303,6 +309,7 @@ class DraggableMarkerActivity : AppCompatActivity() {
|
||||
fun onSymbolDragFinished(id: String)
|
||||
}
|
||||
}
|
||||
// --8<-- [end:DraggableSymbolsManager]
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
|
||||
@@ -4,15 +4,32 @@ import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.view.View
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.content.res.ResourcesCompat
|
||||
import org.maplibre.android.camera.CameraPosition
|
||||
import org.maplibre.android.geometry.LatLng
|
||||
import org.maplibre.android.maps.*
|
||||
import org.maplibre.android.style.layers.*
|
||||
import org.maplibre.android.style.sources.GeoJsonSource
|
||||
import org.maplibre.android.testapp.R
|
||||
import org.maplibre.android.testapp.styles.TestStyles
|
||||
import org.maplibre.geojson.Feature
|
||||
import org.maplibre.geojson.Point
|
||||
import timber.log.Timber
|
||||
import java.net.URI
|
||||
import java.net.URISyntaxException
|
||||
import kotlin.math.atan2
|
||||
|
||||
fun calculateRotationAngle(from: Point, to: Point): Float {
|
||||
val longitudeDiff = to.longitude() - from.longitude()
|
||||
val latitudeDiff = to.latitude() - from.latitude()
|
||||
|
||||
val angleRadians = atan2(longitudeDiff, latitudeDiff)
|
||||
val angleDegrees = Math.toDegrees(angleRadians)
|
||||
|
||||
return ((angleDegrees + 360) % 360).toFloat() // Normalize to 0-360 degrees
|
||||
}
|
||||
|
||||
/**
|
||||
* Test activity showcasing using realtime GeoJSON to move a symbol on your map
|
||||
@@ -26,8 +43,16 @@ class RealTimeGeoJsonActivity : AppCompatActivity(), OnMapReadyCallback {
|
||||
private lateinit var maplibreMap: MapLibreMap
|
||||
private var handler: Handler? = null
|
||||
private var runnable: Runnable? = null
|
||||
|
||||
private var lastLocation: Point? = null
|
||||
|
||||
private val netherlands = LatLng(52.646396, 5.723877)
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
enableEdgeToEdge()
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
supportActionBar?.hide() // Hides the action bar if present
|
||||
setContentView(R.layout.activity_default)
|
||||
mapView = findViewById<View>(R.id.mapView) as MapView
|
||||
mapView.onCreate(savedInstanceState)
|
||||
@@ -36,21 +61,31 @@ class RealTimeGeoJsonActivity : AppCompatActivity(), OnMapReadyCallback {
|
||||
|
||||
override fun onMapReady(map: MapLibreMap) {
|
||||
maplibreMap = map
|
||||
maplibreMap.setStyle(TestStyles.getPredefinedStyleWithFallback("Streets")) { style -> // add source
|
||||
maplibreMap.cameraPosition = CameraPosition.Builder().target(netherlands).zoom(6.0).build()
|
||||
maplibreMap.setStyle(TestStyles.PROTOMAPS_WHITE) { style -> // add source
|
||||
ResourcesCompat.getDrawable(resources, R.drawable.ic_airplanemode_active_black, theme)
|
||||
?.let { style.addImage("plane", it) }
|
||||
// --8<-- [start:addSource]
|
||||
try {
|
||||
style.addSource(GeoJsonSource(ID_GEOJSON_SOURCE, URI(URL_GEOJSON_SOURCE)))
|
||||
} catch (malformedUriException: URISyntaxException) {
|
||||
Timber.e(malformedUriException, "Invalid URL")
|
||||
}
|
||||
// --8<-- [end:addSource]
|
||||
|
||||
// add layer
|
||||
// --8<-- [start:addLayer]
|
||||
val layer = SymbolLayer(ID_GEOJSON_LAYER, ID_GEOJSON_SOURCE)
|
||||
layer.setProperties(PropertyFactory.iconImage("rocket_15"))
|
||||
layer.setProperties(
|
||||
PropertyFactory.iconImage("plane"),
|
||||
PropertyFactory.iconAllowOverlap(true)
|
||||
)
|
||||
style.addLayer(layer)
|
||||
// --8<-- [end:addLayer]
|
||||
|
||||
|
||||
// loop refresh geojson
|
||||
handler = Handler(Looper.getMainLooper())
|
||||
runnable = RefreshGeoJsonRunnable(maplibreMap!!, handler!!)
|
||||
runnable = RefreshGeoJsonRunnable(maplibreMap, handler!!)
|
||||
runnable?.let {
|
||||
handler!!.postDelayed(it, 2000)
|
||||
}
|
||||
@@ -90,16 +125,46 @@ class RealTimeGeoJsonActivity : AppCompatActivity(), OnMapReadyCallback {
|
||||
mapView.onSaveInstanceState(outState)
|
||||
}
|
||||
|
||||
private class RefreshGeoJsonRunnable(
|
||||
private fun setIconRotation(features: List<Feature>) {
|
||||
// --8<-- [start:setIconRotation]
|
||||
if (features.size != 1) {
|
||||
Timber.e("Expected only one feature")
|
||||
return
|
||||
}
|
||||
|
||||
val feature = features[0]
|
||||
val geometry = feature.geometry()
|
||||
if (geometry !is Point) {
|
||||
Timber.e("Expected geometry to be a point")
|
||||
return
|
||||
}
|
||||
|
||||
if (lastLocation == null) {
|
||||
lastLocation = geometry
|
||||
return
|
||||
}
|
||||
|
||||
maplibreMap.style!!.getLayer(ID_GEOJSON_LAYER)!!.setProperties(
|
||||
PropertyFactory.iconRotate(calculateRotationAngle(lastLocation!!, geometry)),
|
||||
)
|
||||
// --8<-- [end:setIconRotation]
|
||||
lastLocation = geometry
|
||||
}
|
||||
|
||||
// --8<-- [start:Runnable]
|
||||
private inner class RefreshGeoJsonRunnable(
|
||||
private val maplibreMap: MapLibreMap,
|
||||
private val handler: Handler
|
||||
) : Runnable {
|
||||
override fun run() {
|
||||
val geoJsonSource = maplibreMap.style!!.getSource(ID_GEOJSON_SOURCE) as GeoJsonSource
|
||||
geoJsonSource.setUri(URL_GEOJSON_SOURCE)
|
||||
val features = geoJsonSource.querySourceFeatures(null)
|
||||
setIconRotation(features)
|
||||
handler.postDelayed(this, 2000)
|
||||
}
|
||||
}
|
||||
// --8<-- [end:Runnable]
|
||||
|
||||
companion object {
|
||||
private const val ID_GEOJSON_LAYER = "border"
|
||||
|
||||
@@ -11,6 +11,8 @@ object TestStyles {
|
||||
|
||||
const val OPENFREEMAP_LIBERY = "https://tiles.openfreemap.org/styles/liberty"
|
||||
|
||||
const val OPENFREEMAP_BRIGHT = "https://tiles.openfreemap.org/styles/bright"
|
||||
|
||||
const val AWS_OPEN_DATA_STANDARD_LIGHT = "https://maps.geo.us-east-2.amazonaws.com/maps/v0/maps/OpenDataStyle/style-descriptor?key=v1.public.eyJqdGkiOiI1NjY5ZTU4My0yNWQwLTQ5MjctODhkMS03OGUxOTY4Y2RhMzgifR_7GLT66TNRXhZJ4KyJ-GK1TPYD9DaWuc5o6YyVmlikVwMaLvEs_iqkCIydspe_vjmgUVsIQstkGoInXV_nd5CcmqRMMa-_wb66SxDdbeRDvmmkpy2Ow_LX9GJDgL2bbiCws0wupJPFDwWCWFLwpK9ICmzGvNcrPbX5uczOQL0N8V9iUvziA52a1WWkZucIf6MUViFRf3XoFkyAT15Ll0NDypAzY63Bnj8_zS8bOaCvJaQqcXM9lrbTusy8Ftq8cEbbK5aMFapXRjug7qcrzUiQ5sr0g23qdMvnKJQFfo7JuQn8vwAksxrQm6A0ByceEXSfyaBoVpFcTzEclxUomhY.NjAyMWJkZWUtMGMyOS00NmRkLThjZTMtODEyOTkzZTUyMTBi"
|
||||
|
||||
private fun protomaps(style: String): String {
|
||||
@@ -23,7 +25,9 @@ object TestStyles {
|
||||
val PROTOMAPS_GRAYSCALE = protomaps("grayscale")
|
||||
|
||||
val PROTOMAPS_WHITE = protomaps("white")
|
||||
|
||||
|
||||
val PROTOMAPS_BLACK = protomaps("black")
|
||||
|
||||
fun getPredefinedStyleWithFallback(name: String): String {
|
||||
try {
|
||||
val style = Style.getPredefinedStyle(name)
|
||||
|
||||
Reference in New Issue
Block a user