Skip to content

Commit

Permalink
feat : add method to draw multiRoad and clearAllRoads Android Side (l…
Browse files Browse the repository at this point in the history
…iodali#224)

* create method drawMultiRoad simultaneously
* create method clearAllRoad
* create RoadConfig data class in android side
* move some fine to models package in android project
  • Loading branch information
liodali committed Feb 4, 2022
1 parent eb89ea4 commit 9c290aa
Show file tree
Hide file tree
Showing 12 changed files with 219 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import hamza.dali.flutter_osm_plugin.FlutterOsmPlugin.Companion.PAUSED
import hamza.dali.flutter_osm_plugin.FlutterOsmPlugin.Companion.STARTED
import hamza.dali.flutter_osm_plugin.FlutterOsmPlugin.Companion.STOPPED
import hamza.dali.flutter_osm_plugin.FlutterOsmPlugin.Companion.mapSnapShots
import hamza.dali.flutter_osm_plugin.models.FlutterMarker
import hamza.dali.flutter_osm_plugin.models.FlutterRoad
import hamza.dali.flutter_osm_plugin.models.RoadConfig
import hamza.dali.flutter_osm_plugin.utilities.*
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding.OnSaveInstanceStateListener
import io.flutter.plugin.common.BinaryMessenger
Expand Down Expand Up @@ -440,6 +443,12 @@ class FlutterOsmView(
"road" -> {
drawRoad(call, result)
}
"draw#multi#road" -> {
drawMultiRoad(call, result)
}
"clear#roads" -> {
clearAllRoad(result)
}
"marker#icon" -> {
changeIcon(call, result)
}
Expand Down Expand Up @@ -606,7 +615,7 @@ class FlutterOsmView(
val polyLine = Polyline(map!!)
polyLine.setPoints(lastRoad.roadPoints)
//customRoadMarkerIcon.p
createRoad(
flutterRoad = createRoad(
polyLine = polyLine,
colorRoad = lastRoad.roadColor,
roadWidth = lastRoad.roadWith,
Expand All @@ -618,7 +627,24 @@ class FlutterOsmView(
}

}

mapSnapShot.cachedRoads().forEach { road ->
if (road.roadPoints.isNotEmpty()) {
if (!map!!.overlayManager.contains(folderRoad)) {
map!!.overlayManager.add(folderRoad)
}
val polyLine = Polyline(map!!)
polyLine.setPoints(road.roadPoints)
//customRoadMarkerIcon.p
flutterRoad = createRoad(
polyLine = polyLine,
colorRoad = road.roadColor,
roadWidth = road.roadWith,
listInterestPoints = road.listInterestPoints,
showPoiMarker = road.showIcons,
)
}
}
map!!.invalidate()
resetAdvPickerOrTrackLocation(mapSnapShot)
clearCacheMap()
methodChannel.invokeMethod("map#restored", null)
Expand Down Expand Up @@ -1302,6 +1328,126 @@ class FlutterOsmView(
result.success(null)
}

private fun clearAllRoad(result: MethodChannel.Result) {
val cachedRoads = map!!.overlays.filterIsInstance<Polyline>()
if (cachedRoads.isNotEmpty()) {
map!!.overlays.removeAll(cachedRoads)
}
map!!.invalidate()
result.success(200)
}

private fun drawMultiRoad(call: MethodCall, result: MethodChannel.Result) {
val args = call.arguments!! as List<HashMap<String, Any>>
val listConfigRoad = emptyList<RoadConfig>().toMutableList()

for (arg in args) {
val waypoints = (arg["wayPoints"] as List<HashMap<String, Double>>).map { map ->
GeoPoint(map["lat"]!!, map["lon"]!!)
}.toList()
listConfigRoad.add(
RoadConfig(
meanUrl = when (arg["roadType"] as String) {
"car" -> OSRMRoadManager.MEAN_BY_CAR
"bike" -> OSRMRoadManager.MEAN_BY_BIKE
"foot" -> OSRMRoadManager.MEAN_BY_FOOT
else -> OSRMRoadManager.MEAN_BY_CAR
},
colorRoad = when (arg.containsKey("roadColor")) {
true -> {
val colors = (arg["roadColor"] as List<Int>)
Color.rgb(colors.first(), colors.last(), colors[1])
}
else -> roadColor
},
roadWidth = when (arg.containsKey("roadWidth")) {
true -> (arg["roadWidth"] as Double).toFloat()
else -> 5f
},
wayPoints = waypoints,
interestPoints = when (arg.containsKey("middlePoints")) {
true -> arg["middlePoints"] as List<HashMap<String, Double>>
false -> emptyList()
}.map { g ->
GeoPoint(g["lat"]!!, g["lon"]!!)
}.toList()
)
)
}


//val showPoiMarker = args["showMarker"] as Boolean

flutterRoad?.road?.let {
map!!.overlays.remove(it)
}
if (!map!!.overlays.contains(folderRoad)) {
map!!.overlays.add(folderRoad)
}
folderRoad.items.clear()
val cachedRoads = map!!.overlays.filterIsInstance<Polyline>()
if (cachedRoads.isNotEmpty()) {
map!!.overlays.removeAll(cachedRoads)
}
map!!.invalidate()

if (roadManager == null)
roadManager = OSRMRoadManager(context, "json/application")
val resultRoads = emptyList<HashMap<String, Any>>().toMutableList();
for (config in listConfigRoad) {
roadManager?.let { manager ->
manager.setMean(config.meanUrl)
var routePointsEncoded = ""
job = scope?.launch(Default) {
withContext(Main) {
folderMarkers.items.removeAll {
(it is FlutterMarker && config.wayPoints.contains(it.position)) ||
(it is FlutterMarker && config.interestPoints.contains(it.position))
}
mapSnapShot().removeMarkersFromSnapShot(config.wayPoints)
}
val roadPoints = ArrayList(config.wayPoints)
if (config.interestPoints.isNotEmpty()) {
roadPoints.addAll(1, config.interestPoints)
}
val road = manager.getRoad(roadPoints)
withContext(Main) {
if (road.mRouteHigh.size > 2) {
routePointsEncoded = PolylineEncoder.encode(road.mRouteHigh, 10)
val polyLine = RoadManager.buildRoadOverlay(road)
createRoad(
polyLine = polyLine,
colorRoad = config.colorRoad,
roadWidth = config.roadWidth,
showPoiMarker = false,
listInterestPoints = config.interestPoints,
)

mapSnapShot().cacheListRoad(
RoadSnapShot(
roadPoints = road.mRouteHigh,
roadColor = config.colorRoad,
roadWith = config.roadWidth,
listInterestPoints = config.interestPoints,
showIcons = false
)
)
resultRoads.add(HashMap<String, Any>().apply {
this["duration"] = road.mDuration
this["distance"] = road.mLength
this["routePoints"] = routePointsEncoded
})
}
}

}
}
}
map!!.invalidate()
result.success(resultRoads)

}

private fun drawRoad(call: MethodCall, result: MethodChannel.Result) {
val args = call.arguments!! as HashMap<String, Any>

Expand Down Expand Up @@ -1368,14 +1514,13 @@ class FlutterOsmView(
if (road.mRouteHigh.size > 2) {
routePointsEncoded = PolylineEncoder.encode(road.mRouteHigh, 10)
val polyLine = RoadManager.buildRoadOverlay(road)
createRoad(
flutterRoad = createRoad(
polyLine = polyLine,
colorRoad = colorRoad,
roadWidth = roadWidth,
showPoiMarker = showPoiMarker,
listInterestPoints = listInterestPoints,
)

mapSnapShot().cacheRoad(
RoadSnapShot(
roadPoints = road.mRouteHigh,
Expand Down Expand Up @@ -1412,7 +1557,7 @@ class FlutterOsmView(
showPoiMarker: Boolean,
listInterestPoints: List<GeoPoint>,
roadWidth: Float,
) {
): FlutterRoad {
polyLine.setOnClickListener { _, _, eventPos ->
methodChannel.invokeMethod("receiveSinglePress", eventPos?.toHashMap())
true
Expand All @@ -1421,13 +1566,13 @@ class FlutterOsmView(
polyLine.outlinePaint.color = colorRoad ?: Color.GREEN


flutterRoad = FlutterRoad(
val flutterRoad = FlutterRoad(
context,
map!!,
interestPoint = if (showPoiMarker) listInterestPoints else emptyList()
)

flutterRoad?.let { roadF ->
flutterRoad.let { roadF ->
roadF.markersIcons = customRoadMarkerIcon
polyLine.outlinePaint.strokeWidth = roadWidth

Expand All @@ -1446,6 +1591,8 @@ class FlutterOsmView(

folderRoad.items.add(roadF.road!!)
}

return flutterRoad
}

private fun staticPositionIconMaker(call: MethodCall, result: MethodChannel.Result) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package hamza.dali.flutter_osm_plugin
package hamza.dali.flutter_osm_plugin.models

import com.squareup.moshi.Json

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package hamza.dali.flutter_osm_plugin
package hamza.dali.flutter_osm_plugin.models

import android.util.Log
import android.view.View
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package hamza.dali.flutter_osm_plugin
package hamza.dali.flutter_osm_plugin.models

import android.content.Context
import android.graphics.Bitmap
Expand All @@ -11,6 +11,7 @@ import android.view.View
import androidx.core.content.ContextCompat
import androidx.core.graphics.BlendModeColorFilterCompat
import androidx.core.graphics.BlendModeCompat
import hamza.dali.flutter_osm_plugin.R
import kotlinx.coroutines.CoroutineScope
import org.osmdroid.util.GeoPoint
import org.osmdroid.views.MapView
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package hamza.dali.flutter_osm_plugin
package hamza.dali.flutter_osm_plugin.models

import android.app.Application
import android.content.Context
import android.graphics.Bitmap
import org.osmdroid.util.GeoPoint
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package hamza.dali.flutter_osm_plugin
package hamza.dali.flutter_osm_plugin.models

import android.content.Context
import android.graphics.Bitmap
import android.util.Log
import hamza.dali.flutter_osm_plugin.utilities.Constants
import org.osmdroid.util.GeoPoint
import org.osmdroid.views.MapView

class FlutterRoadMarker(context: Context, mapView: MapView, point: GeoPoint) :
FlutterMarker(context, mapView, point) {
var mapIconsBitmaps: HashMap<String, Bitmap> = HashMap()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package hamza.dali.flutter_osm_plugin.models

import org.osmdroid.bonuspack.routing.OSRMRoadManager
import org.osmdroid.util.GeoPoint

data class RoadConfig(
val wayPoints:List<GeoPoint>,
val interestPoints : List<GeoPoint>,
val meanUrl :String = OSRMRoadManager.MEAN_BY_CAR,
val colorRoad :Int? = null,
val roadWidth :Float = 5f,
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package hamza.dali.flutter_osm_plugin.network

import hamza.dali.flutter_osm_plugin.Adresse
import retrofit2.http.Field
import hamza.dali.flutter_osm_plugin.models.Adresse
import retrofit2.http.GET
import retrofit2.http.Headers
import retrofit2.http.Query
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package hamza.dali.flutter_osm_plugin
package hamza.dali.flutter_osm_plugin.utilities

import android.view.View

Expand Down
Loading

0 comments on commit 9c290aa

Please sign in to comment.