【发布时间】:2020-01-28 10:19:30
【问题描述】:
单击图钉时需要显示自定义信息窗口。
图钉直接在图层的样式中。
当我创建地图时,我通过 url 获取样式:
mapView?.getMapAsync { map ->
map.setStyle(Style.Builder().fromUrl("mapbox://styles/my-style")) {
onMapReady(map)
}
}
然后我定义这个层:
fun onMapReady(mapboxMap: MapboxMap) {
this.mapboxMap = mapboxMap
val layer = mapboxMap.style?.getLayer("my-layer")
layer?.setProperties(visibility(Property.VISIBLE))
mapboxMap.addOnMapClickListener(this@InfoWindowSymbolLayerActivity)
}
OnMapClick 方法:
override fun onMapClick(point: LatLng): Boolean {
return mapboxMap?.projection?.toScreenLocation(point)?.let { handleClickIcon(it) }!!
}
HandleClickIcon 方法:
fun handleClickIcon(screenPoint: PointF): Boolean {
val features = mapboxMap?.queryRenderedFeatures(screenPoint, MARKER_LAYER_ID)
val inflater = LayoutInflater.from(this)
val bubbleLayout = inflater.inflate(R.layout.pin_info, null) as BubbleLayout
val type = features?.get(0)?.getStringProperty(type)
bubbleLayout.tvDefectType.text = type?.let { formatType(it) }
val username = features?.get(0)?.getStringProperty(username)
bubbleLayout.tvDefectInfo.text = username?.let { formatDefectInfo(it) }
val measureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
bubbleLayout.measure(measureSpec, measureSpec)
val measuredWidth = bubbleLayout.measuredWidth.toFloat()
bubbleLayout.arrowPosition = measuredWidth / 2 - 5
val bitmap = SymbolGenerator.generate(bubbleLayout)
type?.let { mapboxMap?.style?.addImage(it, bitmap) }
mapboxMap?.let {
it.getStyle { style ->
setUpInfoWindowLayer(style)
}
}
return true
}
Mapbox 示例使用自定义 GeoJson: https://docs.mapbox.com/android/maps/examples/symbol-layer-info-window/
但我需要在上方显示信息窗口 pin like this点击
【问题讨论】:
-
你有方法
onMarkerClick(docs.mapbox.com/android/api/map-sdk/5.0.2/com/mapbox/mapboxsdk/…)。信息窗口不必放在地图视图中,只需在上面创建自己的并在需要时显示。 -
onMarkerClick在我点击地图时不会触发@Ikazuchi
标签: java android kotlin mapbox mapbox-android