Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private fun setMapObjects(mapObjects: List<*>) {
- polygonMapObjects.mapNotNull { it.tag as? String }
- val newPolygonIds = mapObjects.mapNotNull { (it as Map<*, *>)["map_id"] as? String }
- // Удаление старых полигонов, которых больше нет в новых данных
- val polygonsToRemove = polygonMapObjects.filter { (it.tag as? String) !in newPolygonIds }
- polygonsToRemove.forEach { it.remove() }
- polygonMapObjects.removeAll(polygonsToRemove)
- // Добавление или обновление существующих полигонов
- for (mapObject in mapObjects) {
- val mapId = (mapObject as Map<*, *>)["map_id"] as String
- val strokeColor = mapObject["stroke_color"] as Long
- val strokeWidth = (mapObject["stroke_width"] as Double).toFloat()
- val polygonsList = mapObject["polygon"] as ArrayList<*>
- val points = polygonsList.map {
- LatLng((it as List<*>)[0] as Double, it[1] as Double)
- }
- val existingPolygon = polygonMapObjects.find { it.tag == mapId }
- if (existingPolygon != null) {
- existingPolygon.points = points
- existingPolygon.strokeWidth = strokeWidth
- existingPolygon.strokeColor = setOpacityForPolygonColor(strokeColor.toInt())
- existingPolygon.fillColor = setOpacityForPolygonColor(strokeColor.toInt())
- } else {
- val newPolygon = huaweiMap?.addPolygon(
- PolygonOptions().addAll(points)
- )
- newPolygon?.strokeWidth = strokeWidth
- newPolygon?.strokeColor = setOpacityForPolygonColor(strokeColor.toInt())
- newPolygon?.fillColor = setOpacityForPolygonColor(strokeColor.toInt())
- newPolygon?.tag = mapId
- if (newPolygon != null) {
- polygonMapObjects.add(newPolygon)
- }
- }
- }
- }
- private fun setPoints(points: List<*>) {
- markerMapObjects.mapNotNull { it.tag as? String }
- val newMarkerIds = points.mapNotNull { (it as Map<*, *>)["id"] as? String }
- // Удаление старых маркеров
- val markersToRemove = markerMapObjects.filter { (it.tag as? String) !in newMarkerIds }
- markersToRemove.forEach { it.remove() }
- markerMapObjects.removeAll(markersToRemove)
- // Добавление или обновление существующих маркеров
- for (point in points) {
- val id = (point as Map<*, *>)["id"] as String
- val coordinates = point["coordinates"] as ArrayList<*>
- val position = LatLng(coordinates[0] as Double, coordinates[1] as Double)
- val existingMarker = markerMapObjects.find { it.tag == id }
- if (existingMarker != null) {
- existingMarker.position = position
- } else {
- val customIcon = BitmapDescriptorFactory.fromResource(R.drawable.point)
- val newMarker = huaweiMap?.addMarker(
- MarkerOptions()
- .position(position)
- .icon(customIcon)
- .anchor(0.5f, 1.0f)
- )
- newMarker?.tag = id
- if (newMarker != null) {
- markerMapObjects.add(newMarker)
- }
- }
- }
- /// обработчик нажатия на точку
- huaweiMap?.setOnMarkerClickListener { marker ->
- run {
- huaweiMap?.animateCamera(
- CameraUpdateFactory.newLatLngZoom(marker.position, 19.0F)
- )
- methodChannel?.invokeMethod(
- "on_point_tap", mapOf(
- "latitude" to marker.position.latitude,
- "longitude" to marker.position.longitude,
- "id" to marker.tag
- )
- )
- }
- true
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement