MapTouch.kt 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package com.cr.map
  2. import android.content.Context
  3. import android.view.MotionEvent
  4. import com.cr.dialog.DialogNormal
  5. import com.esri.arcgisruntime.ArcGISRuntimeException
  6. import com.esri.arcgisruntime.data.Feature
  7. import com.esri.arcgisruntime.geometry.Point
  8. import com.esri.arcgisruntime.geometry.SpatialReference
  9. import com.esri.arcgisruntime.layers.FeatureLayer
  10. import com.esri.arcgisruntime.layers.Layer
  11. import com.esri.arcgisruntime.mapping.GeoElement
  12. import com.esri.arcgisruntime.mapping.view.DefaultMapViewOnTouchListener
  13. import com.esri.arcgisruntime.mapping.view.MapView
  14. /**
  15. * 操作系统:MAC系统
  16. * 创建者:王成
  17. * 创建日期:2023/4/18 08:44
  18. * 描述:地图Touch
  19. */
  20. class MapTouch constructor(context: Context, mapView: MapView) :
  21. DefaultMapViewOnTouchListener(context, mapView) {
  22. /**
  23. * Touch回调
  24. */
  25. interface TouchListener {
  26. // todo: 2023/4/18 涂鸦选择
  27. fun onDoodleSelect(feature: Feature?)
  28. }
  29. // define: 2023/4/18 定义变量
  30. private var context: Context? = null
  31. private var mapView: MapView? = null
  32. private var queryLayer: Layer? = null // define: 2023/4/18 查询的图层
  33. private var action: MapAction? = null
  34. // todo: 2023/4/18 监听
  35. private var listener: TouchListener? = null
  36. // todo: 2023/4/13 静态
  37. companion object {
  38. // define: 2023/4/13 WGS84坐标系框架
  39. val wgs84SpatialReference: SpatialReference = SpatialReference.create(4326)
  40. }
  41. /**
  42. * 初始化
  43. */
  44. init {
  45. this.context = context
  46. this.mapView = mapView
  47. }
  48. // todo: 2023/4/18 重写旋转事件
  49. override fun onRotate(event: MotionEvent?, rotationAngle: Double): Boolean {
  50. return false
  51. }
  52. // todo: 2023/4/18 重写点击事件
  53. override fun onSingleTapUp(e: MotionEvent?): Boolean {
  54. when (action) {
  55. MapAction.MapTapSelectDoodle -> {
  56. identifyLayer(e!!.x.toInt(), e.y.toInt())
  57. }
  58. }
  59. return true
  60. }
  61. // todo: 2023/4/18 内部函数
  62. private fun identifyLayer(sx: Int, sy: Int) {
  63. var queryPoint = android.graphics.Point(sx, sy)
  64. var iAsync = mapView?.identifyLayerAsync(queryLayer, queryPoint, 5.0, false)
  65. iAsync?.addDoneListener(Runnable {
  66. var feature:Feature ? = null
  67. try {
  68. var result = iAsync.get()
  69. if (result.elements.size >= 1) {
  70. feature = result.elements[0] as Feature
  71. } else {
  72. showWarning("未查询到任何数据!")
  73. }
  74. } catch (e: java.lang.IllegalArgumentException) {
  75. showError("查询错误!")
  76. } catch (e: ArcGISRuntimeException) {
  77. showError("查询错误!")
  78. }finally {
  79. if (listener != null) listener?.onDoodleSelect(feature)
  80. }
  81. })
  82. }
  83. /**
  84. * 显示错误消息
  85. * @param error String 错误消息
  86. */
  87. private fun showError(error: String) {
  88. DialogNormal(context!!, "错误", error).show()
  89. }
  90. /**
  91. * 显示警告信息
  92. * @param warning String 警告信息
  93. */
  94. private fun showWarning(warning: String) {
  95. DialogNormal(context!!, "警告", warning).show()
  96. }
  97. /**
  98. * 设置查询图层
  99. * @param layer Layer 查询图层
  100. */
  101. fun setQueryLayer(layer: Layer) {
  102. this.queryLayer = layer
  103. }
  104. /**
  105. * 设置查询图层 用于点击查询
  106. * @param layer Layer 图层
  107. * @param action MapAction 动作
  108. */
  109. fun setQueryLayer(layer: Layer?, action: MapAction?) {
  110. this.queryLayer = layer
  111. this.action = action
  112. }
  113. /**
  114. * 设置监听
  115. * @param listener TouchListener 监听
  116. */
  117. fun setListener(listener: TouchListener) {
  118. this.listener = listener
  119. }
  120. }