修改点击聚合marker崩溃问题

This commit is contained in:
squallzhjch
2023-07-18 15:13:01 +08:00
parent 71948187c3
commit 4ef07f470d
4 changed files with 130 additions and 178 deletions

View File

@@ -1,16 +1,13 @@
package com.navinfo.collect.library.map.handler
import android.content.Context
import android.content.SharedPreferences
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Color
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.res.ResourcesCompat
import androidx.lifecycle.lifecycleScope
import com.navinfo.collect.library.R
import com.navinfo.collect.library.data.dao.impl.TraceDataBase
import com.navinfo.collect.library.data.entity.NiLocation
import com.navinfo.collect.library.data.entity.NoteBean
import com.navinfo.collect.library.data.entity.QsRecordBean
@@ -21,11 +18,6 @@ import com.navinfo.collect.library.map.layers.MyItemizedLayer
import com.navinfo.collect.library.map.layers.NoteLineLayer
import com.navinfo.collect.library.utils.GeometryTools
import com.navinfo.collect.library.utils.StringUtil
import io.realm.Realm
import io.realm.kotlin.where
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.locationtech.jts.geom.Geometry
import org.locationtech.jts.geom.LineString
import org.locationtech.jts.geom.Polygon
@@ -34,19 +26,11 @@ import org.oscim.backend.CanvasAdapter
import org.oscim.backend.canvas.Bitmap
import org.oscim.backend.canvas.Paint
import org.oscim.core.GeoPoint
import org.oscim.layers.marker.ItemizedLayer
import org.oscim.layers.marker.*
import org.oscim.layers.marker.ItemizedLayer.OnItemGestureListener
import org.oscim.layers.marker.MarkerInterface
import org.oscim.layers.marker.MarkerItem
import org.oscim.layers.marker.MarkerRendererFactory
import org.oscim.layers.marker.MarkerSymbol
import org.oscim.layers.vector.geometries.Drawable
import org.oscim.layers.vector.geometries.LineDrawable
import org.oscim.layers.vector.geometries.PointDrawable
import org.oscim.layers.vector.geometries.PolygonDrawable
import org.oscim.layers.vector.geometries.Style
import org.oscim.layers.vector.geometries.*
import org.oscim.map.Map
import java.util.Locale
import java.util.*
/**
* marker 操作
@@ -74,9 +58,7 @@ class MarkHandler(context: AppCompatActivity, mapView: NIMapView) :
/**
* 画布
*/
private val canvas: org.oscim.backend.canvas.Canvas by lazy {
CanvasAdapter.newCanvas()
}
private val canvas: org.oscim.backend.canvas.Canvas = CanvasAdapter.newCanvas()
/**
* 默认marker图层
@@ -88,36 +70,72 @@ class MarkHandler(context: AppCompatActivity, mapView: NIMapView) :
val markerSymbol = MarkerSymbol(
mDefaultBitmap,
MarkerSymbol.HotspotPlace.BOTTOM_CENTER
MarkerSymbol.HotspotPlace.CENTER
)
val layer = ItemizedLayer(
mapView.vtmMap,
ArrayList(),
markerSymbol,
object : OnItemGestureListener<MarkerInterface> {
override fun onItemSingleTapUp(index: Int, item: MarkerInterface?): Boolean {
return false
}
override fun onItemLongPress(index: Int, item: MarkerInterface?): Boolean {
return false
}
}
)
addLayer(layer, NIMapView.LAYER_GROUPS.OPERATE_MARKER)
layer
}
private var niLocationBitmap: Bitmap? = null
private var niLocationBitmap1: Bitmap? = null
private var niLocationBitmap2: Bitmap? = null
private var niLocationBitmap3: Bitmap? = null
private val niLocationBitmap: Bitmap by lazy {
AndroidBitmap(
BitmapFactory.decodeResource(
context.resources,
R.mipmap.icon_gps
)
)
}
private val niLocationBitmap1: Bitmap by lazy {
AndroidBitmap(
BitmapFactory.decodeResource(
context.resources,
R.mipmap.icon_gps_1
)
)
}
private val niLocationBitmap2: Bitmap by lazy {
AndroidBitmap(
BitmapFactory.decodeResource(
context.resources,
R.mipmap.icon_nilocation
)
)
}
private val niLocationBitmap3: Bitmap by lazy {
AndroidBitmap(
BitmapFactory.decodeResource(
context.resources,
R.mipmap.icon_nilocation_1
)
)
}
/**
* 评测数据marker 图层
*/
private val qsRecordItemizedLayer: MyItemizedLayer by lazy {
val bitmapPoi: Bitmap = AndroidBitmap(
BitmapFactory.decodeResource(
mContext.resources,
R.mipmap.map_icon_blue2
)
)
val symbol = MarkerSymbol(bitmapPoi, MarkerSymbol.HotspotPlace.BOTTOM_CENTER)
val markerRendererFactory = MarkerRendererFactory { markerLayer ->
object : ClusterMarkerRenderer(
mContext,
markerLayer,
symbol,
ClusterStyle(
org.oscim.backend.canvas.Color.WHITE,
org.oscim.backend.canvas.Color.BLUE
)
) {
}
}
val layer = MyItemizedLayer(
mMapView.vtmMap,
mutableListOf(),
@@ -160,37 +178,24 @@ class MarkHandler(context: AppCompatActivity, mapView: NIMapView) :
*/
private val niLocationItemizedLayer: ItemizedLayer by lazy {
niLocationBitmap =
AndroidBitmap(BitmapFactory.decodeResource(context.resources, R.mipmap.icon_gps))
niLocationBitmap1 =
AndroidBitmap(BitmapFactory.decodeResource(context.resources, R.mipmap.icon_gps_1))
niLocationBitmap2 =
AndroidBitmap(BitmapFactory.decodeResource(context.resources, R.mipmap.icon_nilocation))
niLocationBitmap3 = AndroidBitmap(
BitmapFactory.decodeResource(
context.resources,
R.mipmap.icon_nilocation_1
)
)
val symbol = MarkerSymbol(niLocationBitmap, MarkerSymbol.HotspotPlace.CENTER)
val layer = ItemizedLayer(
mapView.vtmMap,
mutableListOf(),
markerRendererFactory,
object : OnItemGestureListener<MarkerInterface> {
override fun onItemSingleTapUp(index: Int, item: MarkerInterface?): Boolean {
itemListener?.let {
it.onNiLocation((niLocationItemizedLayer.itemList[index] as MarkerItem).uid as NiLocation)
}
return true
symbol,
)
layer.setOnItemGestureListener(object : OnItemGestureListener<MarkerInterface> {
override fun onItemSingleTapUp(index: Int, item: MarkerInterface?): Boolean {
itemListener?.let {
it.onNiLocation((niLocationItemizedLayer.itemList[index] as MarkerItem).uid as NiLocation)
}
return true
}
override fun onItemLongPress(index: Int, item: MarkerInterface?): Boolean {
return true
}
})
override fun onItemLongPress(index: Int, item: MarkerInterface?): Boolean {
return true
}
})
addLayer(layer, NIMapView.LAYER_GROUPS.OPERATE_MARKER)
layer
}
@@ -207,69 +212,33 @@ class MarkHandler(context: AppCompatActivity, mapView: NIMapView) :
/**
* 便签图标图层
*/
private val noteLayer: MyItemizedLayer by lazy {
val layer = MyItemizedLayer(
private val noteLayer: ItemizedLayer by lazy {
val bitmap =
AndroidBitmap(BitmapFactory.decodeResource(context.resources, noteResId))
val symbol = MarkerSymbol(bitmap, MarkerSymbol.HotspotPlace.CENTER)
val layer = ItemizedLayer(
mMapView.vtmMap,
mutableListOf(),
markerRendererFactory,
object : MyItemizedLayer.OnItemGestureListener {
override fun onItemSingleTapUp(
list: MutableList<Int>,
nearest: Int
): Boolean {
itemListener?.let {
val idList = mutableListOf<String>()
if (list.size == 0) {
} else {
for (i in list) {
val markerInterface: MarkerInterface =
noteLayer.itemList[i]
if (markerInterface is MarkerItem) {
idList.add(markerInterface.title)
}
}
it.onNoteList(idList.distinct().toMutableList())
}
}
return true
symbol,
)
layer.setOnItemGestureListener(object : OnItemGestureListener<MarkerInterface> {
override fun onItemSingleTapUp(index: Int, item: MarkerInterface?): Boolean {
itemListener?.let {
val marker = layer.itemList[index]
if (marker is MarkerItem)
it.onNote(marker.title)
}
return true
}
override fun onItemLongPress(
list: MutableList<Int>?,
nearest: Int
): Boolean {
return true
}
})
override fun onItemLongPress(index: Int, item: MarkerInterface?): Boolean {
return true
}
})
addLayer(layer, NIMapView.LAYER_GROUPS.OPERATE_MARKER)
layer
}
private val markerRendererFactory: MarkerRendererFactory by lazy {
val bitmapPoi: Bitmap = AndroidBitmap(
BitmapFactory.decodeResource(
mContext.resources,
R.mipmap.map_icon_blue2
)
)
val symbol = MarkerSymbol(bitmapPoi, MarkerSymbol.HotspotPlace.BOTTOM_CENTER)
MarkerRendererFactory { markerLayer ->
object : ClusterMarkerRenderer(
mContext,
markerLayer,
symbol,
ClusterStyle(
org.oscim.backend.canvas.Color.WHITE,
org.oscim.backend.canvas.Color.BLUE
)
) {
}
}
}
private val resId = R.mipmap.map_icon_report
private val noteResId = R.drawable.icon_note_marker
private var itemListener: OnQsRecordItemClickListener? = null
@@ -436,26 +405,6 @@ class MarkHandler(context: AppCompatActivity, mapView: NIMapView) :
}
// /**
// * 初始话质检数据图层
// */
// private fun initQsRecordDataLayer() {
//
// mContext.lifecycleScope.launch(Dispatchers.IO) {
// var list = mutableListOf<QsRecordBean>()
// val realm = Realm.getDefaultInstance()
// realm.executeTransaction {
// val objects = realm.where<QsRecordBean>().findAll()
// list = realm.copyFromRealm(objects)
// }
// for (item in list) {
// createMarkerItem(item)
// }
// }
//
// }
/**
* 添加质检数据marker
*/
@@ -808,6 +757,6 @@ class MarkHandler(context: AppCompatActivity, mapView: NIMapView) :
interface OnQsRecordItemClickListener {
fun onQsRecordList(list: MutableList<String>)
fun onNoteList(list: MutableList<String>)
fun onNote(noteId: String)
fun onNiLocation(it: NiLocation)
}