HelloStackoverflow社区, 我正在尝试使用MAPBOX开发Android应用程序。 我遵循本指南在地图上创建标记。
https://docs.mapbox.com/android/maps/examples/default-point-annotation/
我的代码如下:
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)
}
}
不幸的是,我找不到任何解决方案来将单击侦听器添加到标记中(在地图外显示额外的信息)。 我认为,这应该是一个重要的事件,所以我不明白为什么支持如此之少。我想复制这样的东西:https://bl.ocks.org/chriswhong/8977c0d4e869eef06b4e9fda80f3ab
但在Android Studio中与Kotlin一起。 我看到的一个解决方法是将单击侦听器添加到地图上,然后从那里确定最接近坐标的标记,但我认为这不是解决方案的好。您知道我的问题有任何解决方案或解决方法吗? 感谢提前的帮助!
trone this:
pointAnnotationManager.apply {
addClickListener(
OnPointAnnotationClickListener {
Toast.makeText(this@MainActivity, "id: ${it.id}", Toast.LENGTH_LONG).show()
false
}
)
}
我在示例中看到的是:11.10.0 SDK版本来自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)