ArcGIS Android(Beta版)标记点击事件处理方法求助
ArcGIS Android Kotlin Beta版本标记点击事件问题
使用的依赖
implementation "com.esri:arcgis-maps-kotlin:200.0.0-beta01"
已实现的带Drawable图标标记代码
我已经成功实现带自定义图标的标记显示,代码如下:
val graphicsOverlay = GraphicsOverlay() mapView.graphicsOverlays.add(graphicsOverlay) val point = Point( -110.80518769034454, 39.01637367273824, SpatialReference.wgs84() ) val bitmapDrawable: BitmapDrawable = ContextCompat.getDrawable(mContext, R.drawable.ic_bridge_red) as BitmapDrawable val symbol: PictureMarkerSymbol = PictureMarkerSymbol(bitmapDrawable) symbol.height = 30f symbol.width = 30f val markerGraphic = Graphic(point, symbol) graphicsOverlay.graphics.add(markerGraphic)
问题:标记点击事件未生效
我有多个上述类型的标记,希望用户点击时弹出视图,但尝试以下代码后没有效果,请问该如何正确处理标记的点击事件?
尝试的点击事件代码:
mapView.setOnTouchListener { v, event -> val screenCoordinate = ScreenCoordinate(event.x.toDouble(), event.y.toDouble()) val mapPoint = mapView.screenToLocation(screenCoordinate)?.let { if (it.spatialReference != mapView.map?.spatialReference) { mapView.map?.spatialReference?.let { it1 -> GeometryEngine.project(it, it1) } } else { it } } GlobalScope.launch { val result= mapView.identifyGraphicsOverlay(graphicsOverlay,screenCoordinate, 30.0,true) loggerE("result:${result.isSuccess}") } true }
解决方案:正确处理标记点击事件
在ArcGIS Maps SDK for Android(Kotlin Beta版)中,不建议直接用setOnTouchListener处理标记点击,更推荐用MapView的官方回调或结合图形选择监听来实现,以下是两种可行方案:
方案1:使用MapView的单次点击回调
替换触摸监听,改用onSingleTapConfirmed响应单次点击,避免和地图自身手势(平移、缩放)冲突:
mapView.onSingleTapConfirmed = { screenCoordinate -> // 用lifecycleScope替代GlobalScope,避免内存泄漏 lifecycleScope.launch { val identifyResult = mapView.identifyGraphicsOverlay( graphicsOverlay, screenCoordinate, 30.0, // 点击容差(屏幕像素),建议20-30 false // 是否返回所有重叠图形,false取最上层的 ) if (identifyResult.isSuccess && identifyResult.graphics.isNotEmpty()) { val tappedGraphic = identifyResult.graphics.first() // 这里写点击后的逻辑,比如弹出视图 // 示例:Toast.makeText(mContext, "点击了标记", Toast.LENGTH_SHORT).show() } } true // 消费该点击事件 }
方案2:结合图形选择监听
如果需要支持选中状态切换,可以先通过识别操作选中图形,再监听选择变化:
// 给GraphicsOverlay添加选择变化监听 graphicsOverlay.addSelectionChangedListener { event -> val selectedGraphics = event.addedGraphics if (selectedGraphics.isNotEmpty()) { val selectedGraphic = selectedGraphics.first() // 处理选中后的逻辑,比如展示详情视图 } } // 处理点击事件 mapView.onSingleTapConfirmed = { screenCoordinate -> lifecycleScope.launch { val identifyResult = mapView.identifyGraphicsOverlay( graphicsOverlay, screenCoordinate, 30.0, false ) if (identifyResult.isSuccess && identifyResult.graphics.isNotEmpty()) { // 清除之前的选中状态 graphicsOverlay.clearSelection() // 选中当前点击的图形 graphicsOverlay.selectGraphic(identifyResult.graphics.first()) } } true }
常见问题排查
- 容差设置过小:
identifyGraphicsOverlay的第三个参数是屏幕像素容差,太小会导致点击不触发,建议设为20-30。 - GlobalScope风险:
GlobalScope不受页面生命周期管理,容易引发内存泄漏,推荐用lifecycleScope(需导入androidx.lifecycle:lifecycle-runtime-ktx依赖)。 - 空间参考冲突:默认
GraphicsOverlay会继承地图的空间参考,无需手动处理投影,多余的投影操作可能导致识别失败。 - 触摸事件冲突:直接用
setOnTouchListener会干扰地图自身手势,onSingleTapConfirmed是官方推荐的点击处理方式。
内容的提问来源于stack exchange,提问作者Aayush Shrestha
相关产品推荐
相关产品推荐

