Computing road between points

pull/7/head
Arthur VALIN 2 years ago
parent 080f5a176b
commit 8163c75932

@ -43,5 +43,6 @@ dependencies {
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
implementation "androidx.fragment:fragment-ktx:1.5.5"
implementation 'org.osmdroid:osmdroid-android:6.1.14'
implementation 'com.github.MKergall:osmbonuspack:6.9.0'
}

@ -19,6 +19,8 @@ import android.location.LocationListener
import android.util.Log
import android.widget.ProgressBar
import androidx.activity.result.contract.ActivityResultContracts
import org.osmdroid.bonuspack.routing.OSRMRoadManager
import org.osmdroid.bonuspack.routing.RoadManager
import org.osmdroid.config.IConfigurationProvider
import org.osmdroid.library.BuildConfig
import org.osmdroid.tileprovider.tilesource.TileSourceFactory
@ -35,6 +37,7 @@ class Map : Fragment() {
private lateinit var map : MapView
private lateinit var spinner: ProgressBar
private lateinit var locationManager: LocationManager
private val userAgent = "RoadTrip"
val defaultPoint = GeoPoint(48.8583, 2.2944)
var isMapCentered = false;
@ -54,7 +57,7 @@ class Map : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Configuration.getInstance().userAgentValue = "RoadTrip"
Configuration.getInstance().userAgentValue = userAgent;
}
@ -90,7 +93,7 @@ class Map : Fragment() {
recenter.enableMyLocation()
map.overlays.add(recenter);
val addMarker = AddMarkerOverlay()
val addMarker = AddMarkerOverlay(OSRMRoadManager(context, userAgent))
map.overlays.add(addMarker);
}

@ -1,31 +1,56 @@
package uca.baptistearthur.geocaching.ui.overlay
import android.graphics.Rect
import android.os.AsyncTask
import android.util.Log
import android.view.MotionEvent
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.osmdroid.bonuspack.routing.Road
import org.osmdroid.bonuspack.routing.RoadManager
import org.osmdroid.util.GeoPoint
import org.osmdroid.views.MapView
import org.osmdroid.views.overlay.Overlay
import org.osmdroid.views.overlay.Polyline
class AddMarkerOverlay : Overlay() {
private var locations: MutableSet<GeoPoint> = mutableSetOf()
class AddMarkerOverlay(val roadManager: RoadManager) : Overlay() {
private var locations: MutableSet<GeoPoint> = mutableSetOf()
private lateinit var roadOverlay: Polyline
override fun onDoubleTap(e: MotionEvent?, mapView: MapView?): Boolean {
Log.d("GeoMap", "Longpress")
val proj = mapView?.projection;
if(proj!=null){
val loc = proj.fromPixels(e?.x?.toInt()!!, e?.y?.toInt() !! ) as GeoPoint
val loc = proj.fromPixels(e?.x?.toInt()!!, e.y.toInt() ) as GeoPoint
locations.add(loc)
val marker = PlaceMarker(mapView, locations)
val marker = PlaceMarker(mapView, locations, this)
marker.position = loc
marker.title = "Step " + locations.size
mapView.overlays.add(marker)
mapView.invalidate()
computeRoad(mapView)
}
return true;
}
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))
withContext(Dispatchers.Main) {
roadOverlay = RoadManager.buildRoadOverlay(road)
roadOverlay.width=10.0f;
mapView.overlays.add(roadOverlay)
mapView.invalidate()
}
}
}
}
}

@ -1,17 +1,20 @@
package uca.baptistearthur.geocaching.ui.overlay
import android.view.MotionEvent
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
class PlaceMarker(val mapView: MapView, val locations: MutableSet<GeoPoint>) : Marker(mapView) {
class PlaceMarker(val mapView: MapView, val locations: MutableSet<GeoPoint>, val parent : AddMarkerOverlay) : Marker(mapView) {
override fun onLongPress(e: MotionEvent?, mapView: MapView?): Boolean {
if(this.hitTest(e, mapView)) {
if(mapView!=null && this.hitTest(e, mapView)) {
locations.remove(this.position)
this.closeInfoWindow()
mapView?.overlays?.remove(this)
mapView.overlays.remove(this)
parent.computeRoad(mapView);
return true
}
return false

@ -3,7 +3,6 @@ pluginManagement {
gradlePluginPortal()
google()
mavenCentral()
}
}
dependencyResolutionManagement {
@ -11,6 +10,7 @@ dependencyResolutionManagement {
repositories {
google()
mavenCentral()
maven { url "https://jitpack.io" }
}
}
rootProject.name = "Geocaching"

Loading…
Cancel
Save