|
|
|
@ -1,25 +1,23 @@
|
|
|
|
|
package uca.baptistearthur.geocaching
|
|
|
|
|
|
|
|
|
|
import android.graphics.*
|
|
|
|
|
import android.graphics.drawable.shapes.Shape
|
|
|
|
|
import android.location.Location
|
|
|
|
|
import android.view.GestureDetector
|
|
|
|
|
import android.view.MotionEvent
|
|
|
|
|
import androidx.appcompat.widget.AppCompatDrawableManager
|
|
|
|
|
import androidx.core.content.ContextCompat
|
|
|
|
|
import org.osmdroid.util.GeoPoint
|
|
|
|
|
import org.osmdroid.views.MapView
|
|
|
|
|
import org.osmdroid.views.overlay.Overlay
|
|
|
|
|
import org.osmdroid.views.overlay.mylocation.IMyLocationConsumer
|
|
|
|
|
import org.osmdroid.views.overlay.mylocation.IMyLocationProvider
|
|
|
|
|
import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RecenterOverlay(val myLocationProvider: IMyLocationProvider, val mapView: MapView) : Overlay(), GestureDetector.OnGestureListener, IMyLocationConsumer {
|
|
|
|
|
|
|
|
|
|
private val gestureDetector: GestureDetector = GestureDetector(mapView.context, this)
|
|
|
|
|
private var circleRectF=RectF();
|
|
|
|
|
|
|
|
|
|
class RecenterOverlay(val myLocationProvider: IMyLocationProvider, val mapView: MapView) : Overlay(), IMyLocationConsumer {
|
|
|
|
|
|
|
|
|
|
private var circleRectF=RectF()
|
|
|
|
|
private var mIsFollowing = false
|
|
|
|
|
private var mLocation: Location? = null
|
|
|
|
|
override fun draw(canvas: Canvas, mapView: MapView, shadow: Boolean) {
|
|
|
|
|
|
|
|
|
|
val circleSize = 300f
|
|
|
|
@ -49,20 +47,47 @@ class RecenterOverlay(val myLocationProvider: IMyLocationProvider, val mapView:
|
|
|
|
|
icon?.draw(canvas)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onLocationChanged(location: Location, source: IMyLocationProvider) {
|
|
|
|
|
mLocation = myLocationProvider.lastKnownLocation
|
|
|
|
|
if (mIsFollowing) {
|
|
|
|
|
mapView.controller.animateTo(GeoPoint(location.latitude, location.longitude));
|
|
|
|
|
} else {
|
|
|
|
|
mapView.postInvalidate();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun enableMyLocation() {
|
|
|
|
|
mLocation = myLocationProvider.lastKnownLocation
|
|
|
|
|
mIsFollowing = true;
|
|
|
|
|
if(mLocation!=null) {
|
|
|
|
|
mapView.controller?.animateTo(GeoPoint(mLocation!!.latitude, mLocation!!.longitude))
|
|
|
|
|
}
|
|
|
|
|
myLocationProvider.startLocationProvider(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun disableMyLocation(){
|
|
|
|
|
mapView.controller?.stopAnimation(false)
|
|
|
|
|
mIsFollowing = false
|
|
|
|
|
myLocationProvider.stopLocationProvider();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onTouchEvent(event: MotionEvent?, mapView: MapView?): Boolean {
|
|
|
|
|
val isSingleFingerDrag =
|
|
|
|
|
event!!.action == MotionEvent.ACTION_MOVE && event.pointerCount == 1
|
|
|
|
|
if (event.action == MotionEvent.ACTION_DOWN) {
|
|
|
|
|
disableMyLocation()
|
|
|
|
|
} else if (isSingleFingerDrag && mIsFollowing) {
|
|
|
|
|
return true // prevent the pan
|
|
|
|
|
}
|
|
|
|
|
return super.onTouchEvent(event, mapView)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onSingleTapConfirmed(e: MotionEvent?, mapView: MapView?) =
|
|
|
|
|
myLocationProvider.lastKnownLocation?.let {
|
|
|
|
|
if (e != null && circleRectF.contains(e.x, e.y)) {
|
|
|
|
|
mapView?.controller?.setCenter(GeoPoint(it.latitude, it.longitude))
|
|
|
|
|
mapView?.controller?.setZoom(20.0);
|
|
|
|
|
|
|
|
|
|
mapView?.controller?.setZoom(21.0);
|
|
|
|
|
enableMyLocation()
|
|
|
|
|
}
|
|
|
|
|
true
|
|
|
|
|
} ?: false
|
|
|
|
@ -82,12 +107,4 @@ class RecenterOverlay(val myLocationProvider: IMyLocationProvider, val mapView:
|
|
|
|
|
disableMyLocation()
|
|
|
|
|
super.onDetach(mapView)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onDown(p0: MotionEvent) = true
|
|
|
|
|
override fun onShowPress(p0: MotionEvent) = Unit
|
|
|
|
|
override fun onSingleTapUp(p0: MotionEvent) = true
|
|
|
|
|
override fun onScroll(p0: MotionEvent, p1: MotionEvent, p2: Float, p3: Float) = true;
|
|
|
|
|
override fun onLongPress(p0: MotionEvent) = Unit
|
|
|
|
|
override fun onFling(p0: MotionEvent, p1: MotionEvent, p2: Float, p3: Float) = true
|
|
|
|
|
override fun onLocationChanged(location: Location?, source: IMyLocationProvider?) = Unit
|
|
|
|
|
}
|
|
|
|
|