Skip to content

Commit

Permalink
[Annotation] Automatically add layer and source while changing style. (
Browse files Browse the repository at this point in the history
…#29)

* [Annotation] Automatically add layer and source while changing style.

* Apply the cache properties while changing style

* Fix test cases
  • Loading branch information
Kevin Li authored Jan 26, 2021
1 parent 9691efd commit be1bbbe
Show file tree
Hide file tree
Showing 18 changed files with 347 additions and 304 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import com.mapbox.maps.extension.style.utils.ColorUtils
import com.mapbox.maps.plugin.annotation.generated.*
import com.mapbox.maps.plugin.annotation.getAnnotationPlugin
import com.mapbox.maps.testapp.R
import com.mapbox.maps.testapp.utils.Assets
import kotlinx.android.synthetic.main.activity_add_marker_symbol.*
import kotlinx.android.synthetic.main.activity_add_marker_symbol.mapView
import kotlinx.android.synthetic.main.activity_annotation.*
Expand Down Expand Up @@ -51,7 +50,7 @@ class CircleActivity : AppCompatActivity() {
val color = Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256))
circleOptionsList.add(
CircleOptions()
.withPoint(createRandomPoints())
.withPoint(Utils.createRandomPoint())
.withCircleColor(ColorUtils.colorToRgbaString(color))
.withCircleRadius(8.0)
.withDraggable(true)
Expand All @@ -62,7 +61,7 @@ class CircleActivity : AppCompatActivity() {
try {
create(
FeatureCollection.fromJson(
Assets.loadStringFromAssets(
Utils.loadStringFromAssets(
this@CircleActivity,
"annotations.json"
)
Expand All @@ -75,13 +74,9 @@ class CircleActivity : AppCompatActivity() {
}

deleteAll.setOnClickListener { circleManager?.deleteAll() }
}

private fun createRandomPoints(): Point {
return Point.fromLngLat(
random.nextDouble() * -360.0 + 180.0,
random.nextDouble() * -180.0 + 90.0
)
changeStyle.setOnClickListener {
mapView.getMapboxMap().loadStyleUri(Utils.nextStyle)
}
}

override fun onStart() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import com.mapbox.maps.extension.style.utils.ColorUtils
import com.mapbox.maps.plugin.annotation.generated.*
import com.mapbox.maps.plugin.annotation.getAnnotationPlugin
import com.mapbox.maps.testapp.R
import com.mapbox.maps.testapp.utils.Assets
import kotlinx.android.synthetic.main.activity_add_marker_symbol.*
import kotlinx.android.synthetic.main.activity_add_marker_symbol.mapView
import kotlinx.android.synthetic.main.activity_annotation.*
Expand Down Expand Up @@ -59,7 +58,7 @@ class FillActivity : AppCompatActivity() {
val color = Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256))
fillOptionsList.add(
FillOptions()
.withPoints(createRandomPoints())
.withPoints(Utils.createRandomPointsList())
.withFillColor(ColorUtils.colorToRgbaString(color))
)
}
Expand All @@ -68,7 +67,7 @@ class FillActivity : AppCompatActivity() {
try {
create(
FeatureCollection.fromJson(
Assets.loadStringFromAssets(
Utils.loadStringFromAssets(
this@FillActivity,
"annotations.json"
)
Expand All @@ -81,26 +80,11 @@ class FillActivity : AppCompatActivity() {
}

deleteAll.setOnClickListener { fillManager?.deleteAll() }
}

private fun createRandomPoints(): List<List<Point>> {
val points = mutableListOf<Point>()
val firstLast = Point.fromLngLat(
random.nextDouble() * -360.0 + 180.0,
random.nextDouble() * -180.0 + 90.0
)
points.add(firstLast)
for (i in 0 until random.nextInt(10)) {
points.add(
Point.fromLngLat(
random.nextDouble() * -360.0 + 180.0,
random.nextDouble() * -180.0 + 90.0
)
)
changeStyle.setOnClickListener {
mapView.getMapboxMap().loadStyleUri(Utils.nextStyle)
}
points.add(firstLast)
return listOf(points)
}

override fun onStart() {
super.onStart()
mapView.onStart()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import com.mapbox.maps.extension.style.utils.ColorUtils
import com.mapbox.maps.plugin.annotation.generated.*
import com.mapbox.maps.plugin.annotation.getAnnotationPlugin
import com.mapbox.maps.testapp.R
import com.mapbox.maps.testapp.utils.Assets
import kotlinx.android.synthetic.main.activity_add_marker_symbol.*
import kotlinx.android.synthetic.main.activity_add_marker_symbol.mapView
import kotlinx.android.synthetic.main.activity_annotation.*
Expand Down Expand Up @@ -57,7 +56,7 @@ class LineActivity : AppCompatActivity() {
// random add lines across the globe
val lists: MutableList<List<Point>> = ArrayList<List<Point>>()
for (i in 0..99) {
lists.add(createRandomPoints())
lists.add(Utils.createRandomPoints())
}
val lineOptionsList = lists.map {
val color = Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256))
Expand All @@ -71,7 +70,7 @@ class LineActivity : AppCompatActivity() {
try {
create(
FeatureCollection.fromJson(
Assets.loadStringFromAssets(
Utils.loadStringFromAssets(
this@LineActivity,
"annotations.json"
)
Expand All @@ -84,19 +83,9 @@ class LineActivity : AppCompatActivity() {
}

deleteAll.setOnClickListener { lineManager?.deleteAll() }
}

private fun createRandomPoints(): List<Point> {
val points: MutableList<Point> = ArrayList<Point>()
for (i in 0 until random.nextInt(10)) {
points.add(
Point.fromLngLat(
random.nextDouble() * -360.0 + 180.0,
random.nextDouble() * -180.0 + 90.0
)
)
changeStyle.setOnClickListener {
mapView.getMapboxMap().loadStyleUri(Utils.nextStyle)
}
return points
}

override fun onStart() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mapbox.maps.testapp.examples.annotation

import android.animation.ValueAnimator
import android.graphics.Bitmap
import android.graphics.Color
import android.os.Bundle
import android.view.Menu
Expand All @@ -25,7 +26,6 @@ import com.mapbox.maps.plugin.annotation.generated.*
import com.mapbox.maps.plugin.annotation.getAnnotationPlugin
import com.mapbox.maps.plugin.location.utils.BitmapUtils
import com.mapbox.maps.testapp.R
import com.mapbox.maps.testapp.utils.Assets
import kotlinx.android.synthetic.main.activity_add_marker_symbol.*
import kotlinx.android.synthetic.main.activity_add_marker_symbol.mapView
import kotlinx.android.synthetic.main.activity_annotation.*
Expand All @@ -40,22 +40,21 @@ class SymbolActivity : AppCompatActivity() {
private var symbolManager: SymbolManager? = null
private var symbol: Symbol? = null
private val animators: MutableList<ValueAnimator> = mutableListOf()

private var airPortImage: Bitmap? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_annotation)
airPortImage = BitmapUtils.getBitmapFromDrawable(
ResourcesCompat.getDrawable(
resources,
R.drawable.ic_airplanemode_active_black_24dp,
this@SymbolActivity.theme
)
)

mapView.getMapboxMap().loadStyleUri(Style.MAPBOX_STREETS) { style ->
BitmapUtils.getBitmapFromDrawable(
ResourcesCompat.getDrawable(
resources,
R.drawable.ic_airplanemode_active_black_24dp,
this@SymbolActivity.theme
)
)?.let {
style.addImage(
ID_ICON_AIRPORT,
it, true
)
airPortImage?.let {
style.addImage(ID_ICON_AIRPORT, it, true)
}

val annotationPlugin = mapView.getAnnotationPlugin()
Expand Down Expand Up @@ -91,7 +90,7 @@ class SymbolActivity : AppCompatActivity() {

// create nearby symbols
val nearbyOptions: SymbolOptions = SymbolOptions()
.withPoint(Point.fromLngLat(0.367099, 6.626384))
.withPoint(Point.fromLngLat(0.367099, 6.526384))
.withIconImage(MAKI_ICON_CIRCLE)
.withIconColor(ColorUtils.colorToRgbaString(Color.YELLOW))
.withIconSize(2.5)
Expand All @@ -104,7 +103,7 @@ class SymbolActivity : AppCompatActivity() {
for (i in 0..20) {
symbolOptionsList.add(
SymbolOptions()
.withPoint(createRandomPoints())
.withPoint(Utils.createRandomPoint())
.withIconImage(MAKI_ICON_CAR)
.withDraggable(true)
)
Expand All @@ -114,7 +113,7 @@ class SymbolActivity : AppCompatActivity() {
try {
create(
FeatureCollection.fromJson(
Assets.loadStringFromAssets(
Utils.loadStringFromAssets(
this@SymbolActivity,
"annotations.json"
)
Expand All @@ -127,6 +126,13 @@ class SymbolActivity : AppCompatActivity() {
}

deleteAll.setOnClickListener { symbolManager?.deleteAll() }
changeStyle.setOnClickListener {
mapView.getMapboxMap().loadStyleUri(Utils.nextStyle) { style ->
airPortImage?.let { bitMap ->
style.addImage(ID_ICON_AIRPORT, bitMap, true)
}
}
}
}

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
Expand Down Expand Up @@ -238,13 +244,6 @@ class SymbolActivity : AppCompatActivity() {
mapView.onDestroy()
}

private fun createRandomPoints(): Point {
return Point.fromLngLat(
random.nextDouble() * -360.0 + 180.0,
random.nextDouble() * -180.0 + 90.0
)
}

companion object {
private const val ID_ICON_AIRPORT = "airport"
private const val MAKI_ICON_CAR = "car-15"
Expand Down
114 changes: 114 additions & 0 deletions app/src/main/java/com/mapbox/maps/testapp/examples/annotation/Utils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.mapbox.maps.testapp.examples.annotation

import android.content.Context
import com.mapbox.core.utils.TextUtils
import com.mapbox.geojson.Point
import com.mapbox.maps.Style
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
import java.io.Reader
import java.nio.charset.Charset
import java.util.*

/**
* Useful utilities used throughout the testapp.
*/
object Utils {

private val STYLES =
arrayOf(Style.MAPBOX_STREETS, Style.OUTDOORS, Style.LIGHT, Style.DARK, Style.SATELLITE_STREETS)

private var index: Int = 0

/**
* Utility to cycle through map styles. Useful to test if runtime styling source and layers transfer over to new
* style.
*
* @return a string ID representing the map style
*/
val nextStyle: String
get() {
index++
if (index == STYLES.size) {
index = 0
}
return STYLES[index]
}

/**
* Utility for getting a list of random points.
*
* @return a a list of random points
*/
fun createRandomPoints(): List<Point> {
val random = Random()
val points: MutableList<Point> = ArrayList<Point>()
for (i in 0 until random.nextInt(10)) {
points.add(
Point.fromLngLat(
random.nextDouble() * -360.0 + 180.0,
random.nextDouble() * -180.0 + 90.0
)
)
}
return points
}

/**
* Utility for getting a list of lists of random points
*
* @return a list of lists of random points
*/
fun createRandomPointsList(): List<List<Point>> {
val random = Random()
val points = mutableListOf<Point>()
val firstLast = Point.fromLngLat(
random.nextDouble() * -360.0 + 180.0,
random.nextDouble() * -180.0 + 90.0
)
points.add(firstLast)
for (i in 0 until random.nextInt(10)) {
points.add(
Point.fromLngLat(
random.nextDouble() * -360.0 + 180.0,
random.nextDouble() * -180.0 + 90.0
)
)
}
points.add(firstLast)
return listOf(points)
}

/**
* Utility for getting a random point
*
* @return a random point
*/
fun createRandomPoint(): Point {
val random = Random()
return Point.fromLngLat(
random.nextDouble() * -360.0 + 180.0,
random.nextDouble() * -180.0 + 90.0
)
}

@Throws(IOException::class)
fun loadStringFromAssets(context: Context, fileName: String): String {
if (TextUtils.isEmpty(fileName)) {
throw NullPointerException("No GeoJSON File Name passed in.")
}
val `is` = context.assets.open(fileName)
val rd = BufferedReader(InputStreamReader(`is`, Charset.forName("UTF-8")))
return readAll(rd)
}

@Throws(IOException::class)
private fun readAll(rd: Reader): String {
val sb = StringBuilder()
rd.forEachLine {
sb.append(it)
}
return sb.toString()
}
}
29 changes: 0 additions & 29 deletions app/src/main/java/com/mapbox/maps/testapp/utils/Assets.kt

This file was deleted.

Loading

0 comments on commit be1bbbe

Please sign in to comment.