Adding Start-Finish icons and titles to markers

pull/7/head
Arthur VALIN 2 years ago
parent 5e44f01779
commit fd33b41dd2

@ -1,7 +1,9 @@
package uca.baptistearthur.geocaching.ui.overlay
import android.content.Context
import android.util.Log
import android.view.MotionEvent
import androidx.core.content.ContextCompat
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@ -11,11 +13,12 @@ import org.osmdroid.util.GeoPoint
import org.osmdroid.views.MapView
import org.osmdroid.views.overlay.Overlay
import org.osmdroid.views.overlay.Polyline
import uca.baptistearthur.geocaching.R
class AddMarkerOverlay(val roadManager: RoadManager) : Overlay() {
private var locations: MutableSet<GeoPoint> = mutableSetOf()
private var locations: MutableSet<PlaceMarker> = mutableSetOf()
private lateinit var roadOverlay: Polyline
private var newRoadtripOverlayVisible = false;
override fun onDoubleTap(e: MotionEvent?, mapView: MapView?): Boolean {
@ -23,25 +26,36 @@ class AddMarkerOverlay(val roadManager: RoadManager) : Overlay() {
val proj = mapView?.projection;
if(proj!=null){
val loc = proj.fromPixels(e?.x?.toInt()!!, e.y.toInt() ) as GeoPoint
locations.add(loc)
val marker = PlaceMarker(mapView, locations, this)
val marker = PlaceMarker(mapView, this)
marker.position = loc
if(locations.isNotEmpty()) locations.last().setDefaultIcon()
locations.add(marker)
locations.forEach{ it.closeInfoWindow()}
computeIcons(mapView.context)
mapView.overlays.add(marker)
computeRoad(mapView)
computeNewRoadtripOverlay(mapView);
mapView.invalidate()
}
return true;
}
fun computeIcons(context: Context){
if(locations.isNotEmpty()) {
val flagIcon = ContextCompat.getDrawable(context, R.drawable.roadtrip_marker)!!
locations.last().icon = flagIcon
locations.first().icon = flagIcon
}
}
fun computeRoad(mapView: MapView) {
mapView.overlays.remove(mapView.overlays.find { it is Polyline})
if (locations.size > 1) {
CoroutineScope(Dispatchers.IO).launch {
val road = roadManager.getRoad(ArrayList(locations))
val road = roadManager.getRoad(ArrayList(locations.map{it -> it.position}))
withContext(Dispatchers.Main) {
roadOverlay = RoadManager.buildRoadOverlay(road)
mapView.overlays.add(roadOverlay)
mapView.invalidate()
}
}
}
@ -60,7 +74,18 @@ class AddMarkerOverlay(val roadManager: RoadManager) : Overlay() {
newRoadtripOverlayVisible=false
}
}
fun removeMarker(placeMarker: PlaceMarker) = locations.remove(placeMarker);
fun getMarkerLabel(placeMarker: PlaceMarker) =
when (placeMarker) {
locations.first() -> {
"Start"
}
locations.last() -> {
"Finish"
}
else -> {
"Step " + locations.indexOf(placeMarker);
}
}
}

@ -14,7 +14,7 @@ import org.osmdroid.views.overlay.Overlay
import uca.baptistearthur.geocaching.R
class NewRoadtripOverlay(val points: Collection<GeoPoint>) : Overlay() {
class NewRoadtripOverlay(val points: Collection<PlaceMarker>) : Overlay() {
private var circleRectF=RectF()

@ -1,26 +1,32 @@
package uca.baptistearthur.geocaching.ui.overlay
import android.graphics.drawable.Drawable
import android.util.Log
import android.view.MotionEvent
import androidx.core.content.ContextCompat
import org.osmdroid.bonuspack.routing.OSRMRoadManager
import org.osmdroid.bonuspack.routing.RoadManager
import org.osmdroid.util.GeoPoint
import org.osmdroid.views.MapView
import org.osmdroid.views.overlay.Marker
import org.osmdroid.views.overlay.infowindow.InfoWindow
import uca.baptistearthur.geocaching.R
class PlaceMarker(val mapView: MapView, val locations: MutableSet<GeoPoint>, val parent : AddMarkerOverlay) : Marker(mapView) {
override fun getTitle(): String {
return "Step " + (locations.indexOf(this.position)+1)
}
class PlaceMarker(val mapView: MapView, val parent : AddMarkerOverlay) : Marker(mapView) {
override fun getTitle() = parent.getMarkerLabel(this)
override fun onLongPress(e: MotionEvent?, mapView: MapView?): Boolean {
if(mapView!=null && this.hitTest(e, mapView)) {
locations.remove(this.position)
parent.removeMarker(this)
this.closeInfoWindow()
mapView.overlays.remove(this)
parent.computeIcons(mapView.context)
parent.computeRoad(mapView)
parent.computeNewRoadtripOverlay(mapView)
mapView.invalidate()
return true
}
return false
}
}

@ -0,0 +1,5 @@
<vector android:height="50dp" android:tint="#FF0000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="50dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M14.4,6L14,4H5v17h2v-7h5.6l0.4,2h7V6z"/>
</vector>
Loading…
Cancel
Save