'Custom markers not visible in mapbox-android
I'm using mapbox-sdk android for location tracking. I want to add few custom markers to the map in specified location. But the below code doesn't works for me.
MarkerOptions options = new MarkerOptions();
options.title("pos");
IconFactory iconFactory = IconFactory.getInstance(MainActivity.this);
Icon icon = iconFactory.fromResource(R.drawable.home);
options.icon(icon);
options.position(new LatLng(80.27, 13.09));
mapboxMap.addMarker(options);
I use mapox-sdk:6.0.1
Solution 1:[1]
use this:
private fun addMarkerIconsToMap(loadedMapStyle: Style) {
BitmapUtils.getBitmapFromDrawable(getDrawable(R.drawable.red_marker))?.let {
loadedMapStyle.addImage("icon-id", it)
}
if(!locationList.isNullOrEmpty()){
val feature: ArrayList<Feature> = ArrayList()
for (x in 0 until locationList.size){
feature.add(Feature.fromGeometry(Point.fromLngLat(locationList[x].long, locationList[x].lat)))
}
loadedMapStyle.addSource(GeoJsonSource("source-id",
FeatureCollection.fromFeatures(feature)
)
)
loadedMapStyle.addLayer(SymbolLayer("layer-id", "source-id").withProperties(
iconImage("icon-id"),
iconOffset(arrayOf(0f, -8f))
)
)
}
}
inside locationList is Array of long: Double and lat: Double
YourActivity :
class YourActivity : AppCompatActivity(), OnMapReadyCallback {
private lateinit var mapboxMap: MapboxMap
...
}
and then override :
override fun onMapReady(mapboxMap: MapboxMap) {
mapboxMap.setStyle(Style.MAPBOX_STREETS){
addMarkerIconsToMap(it)
}
this.mapboxMap = mapboxMap
}
Solution 2:[2]
This is how I would add a custom marker (from a Compose perspective). (You can either do it from the viewModel depending on your program structure)
AndroidView(
factory = { mapView }, modifier = Modifier.fillMaxWidth()
) { mView ->
val bitmap = bitmapFromDrawableRes(context, R.drawable.chicken_marker)!!
val annotation = mView.annotations
val pointManager = annotation.createPointAnnotationManager()
list.forEach { point ->
val pointOptions = PointAnnotationOptions()
.withPoint(point)
.withIconImage(bitmap)
pointManager.create(pointOptions.apply { iconSize = 0.8 })
}
Here's the functions used to get the drawable(".svg",".png",".jpeg")
private fun bitmapFromDrawableRes(context: Context, @DrawableRes resourceId: Int): Bitmap? =
convertDrawableToBitmap(AppCompatResources.getDrawable(context, resourceId))
private fun convertDrawableToBitmap(sourceDrawable: Drawable?): Bitmap? {
if (sourceDrawable == null) {
return null
}
return if (sourceDrawable is BitmapDrawable) {
sourceDrawable.bitmap
} else {
// copying drawable object to not manipulate on the same reference
val constantState = sourceDrawable.constantState ?: return null
val drawable = constantState.newDrawable().mutate()
val bitmap: Bitmap = Bitmap.createBitmap(
drawable.intrinsicWidth, drawable.intrinsicHeight,
Bitmap.Config.ARGB_8888
)
val canvas = Canvas(bitmap)
drawable.setBounds(0, 0, canvas.width, canvas.height)
drawable.draw(canvas)
bitmap
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dani Andhika |
| Solution 2 | Koch |
