Reputation: 103
Hello Stackoverflow community, I am trying to develop an Android application with Mapbox. I followed this guide to create markers on the map.
https://docs.mapbox.com/android/maps/examples/default-point-annotation/
Thus my code is the following:
public fun createMarker(id: String, lon: Double, lat: Double) {
// Create an instance of the Annotation API and get the PointAnnotationManager.
var marker: PointAnnotation? = bitmapFromDrawableRes(
drawercontext,
R.drawable.red_marker
)?.let {
val annotationApi = binding.mapBoxView.mapView?.annotations
val pointAnnotationManager =
annotationApi?.createPointAnnotationManager(binding.mapBoxView.mapView!!)
// Set options for the resulting symbol layer.
val pointAnnotationOptions: PointAnnotationOptions = PointAnnotationOptions()
// Define a geographic coordinate.
.withPoint(Point.fromLngLat(lon, lat))
// Specify the bitmap you assigned to the point annotation
// The bitmap will be added to map style automatically.
.withIconImage(it)
// Add the resulting pointAnnotation to the map.
pointAnnotationManager?.create(pointAnnotationOptions)
}
}
Unfortunately, I can not find any solution to add a click listener to markers (to show extra information outside of the map). In my opinion, this should be an important event, so I don't get why there is so little support. I want to replicate something like this:
https://bl.ocks.org/chriswhong/8977c0d4e869e9eaf06b4e9fda80f3ab
But in Android Studio with Kotlin. One workaround I have seen is to add a click listener to the map and from there determine the marker with the closest coordinates, but I think that would not be as nice of a solution. Do you know any solutions or workarounds to my problem? Thanks for the help in advance!
Upvotes: 1
Views: 1616
Reputation: 180
Here is what I saw in example : 11.10.0 SDK version from github
val annotationApi = mMapView?.annotations
val pointAnnotationManager = annotationApi?.createPointAnnotationManager()
val pointAnnotationOptions: PointAnnotationOptions = PointAnnotationOptions()
.withPoint(Point.fromLngLat(18.06, 59.31))
.withIconImage(
ContextCompat.getDrawable(requireContext(), R.drawable.map_pin_faehren_ueber_die_weser)?.toBitmap()!!
)
pointAnnotationManager?.create(pointAnnotationOptions)
Upvotes: 0
Reputation: 461
try this:
pointAnnotationManager.apply {
addClickListener(
OnPointAnnotationClickListener {
Toast.makeText(this@MainActivity, "id: ${it.id}", Toast.LENGTH_LONG).show()
false
}
)
}
Upvotes: 1