Comments (4)
It's possible to drag markers, but it's definitely less straightforward than with MapCompose. I'll look for an example and post it here.
from mapview.
Thank you very much. You are a selfless and handsome man
from mapview.
Here it is:
import android.view.GestureDetector
import android.view.GestureDetector.SimpleOnGestureListener
import android.view.MotionEvent
import android.view.View
import android.view.View.OnTouchListener
import ovh.plrapps.mapview.MapView
import ovh.plrapps.mapview.ReferentialData
import ovh.plrapps.mapview.ReferentialListener
import ovh.plrapps.mapview.api.getConstrainedX
import ovh.plrapps.mapview.api.getConstrainedY
import ovh.plrapps.mapview.core.CoordinateTranslater
import kotlin.math.cos
import kotlin.math.sin
/**
* A touch listener that enables touch-moves of a view (also called marker) on a [MapView].
* The logic of moving the marker is delegated to the provided [MarkerMoveAgent].
* It can also react to single-tap event. To be notified, provide a [ClickCallback] to
* the overloaded constructor.
*
* Example of usage :
* ```
* MarkerMoveAgent agent = new ClassImplementsMoveAgent();
* TouchMoveListener markerTouchListener = new TouchMoveListener(mapView, agent);
* View marker = new CustomMarker(context);
* marker.setOnTouchListener(markerTouchListener);
* mapView.addMarker(marker, ...);
* ```
*/
class TouchMoveListener @JvmOverloads constructor(private val mapView: MapView,
private val markerClickCallback: ClickCallback? = null,
private val markerMarkerMoveAgent: MarkerMoveAgent) : SimpleOnGestureListener(), OnTouchListener, ReferentialListener {
private val mGestureDetector = GestureDetector(mapView.context, this)
private var deltaX = 0f
private var deltaY = 0f
private var referentialData: ReferentialData? = null
override fun onReferentialChanged(refData: ReferentialData) {
this.referentialData = refData
}
override fun onTouch(view: View, event: MotionEvent): Boolean {
if (mGestureDetector.onTouchEvent(event)) {
return true
}
val rd = referentialData
var angle = 0.0
if (rd != null) {
angle = -Math.toRadians(rd.angle.toDouble())
}
when (event.action) {
MotionEvent.ACTION_DOWN -> {
deltaX = event.x
deltaY = event.y
}
MotionEvent.ACTION_MOVE -> {
val dX: Float
val dY: Float
if (rd != null && rd.rotationEnabled) {
dX = ((event.x - deltaX) * cos(angle) - (event.y - deltaY) * sin(angle)).toFloat()
dY = ((event.x - deltaX) * sin(angle) + (event.y - deltaY) * cos(angle)).toFloat()
} else {
dX = event.x - deltaX
dY = event.y - deltaY
}
val X: Double
val Y: Double
val ct = mapView.coordinateTranslater ?: return true
if (rd != null && rd.rotationEnabled) {
val Xorig = ct.reverseRotationX(rd, view.x + (view.width shr 1), view.y + (view.height shr 1))
val Yorig = ct.reverseRotationY(rd, view.x + (view.width shr 1), view.y + (view.height shr 1))
X = getRelativeX(ct, Xorig.toFloat() + dX)
Y = getRelativeY(ct, Yorig.toFloat() + dY)
} else {
X = getRelativeX(ct, view.x + dX + (view.width shr 1))
Y = getRelativeY(ct, view.y + dY + (view.height shr 1))
}
markerMarkerMoveAgent.onMarkerMove(mapView, view, getConstrainedX(X)
?: return true, getConstrainedY(Y) ?: return true)
}
else -> return false
}
return true
}
override fun onSingleTapConfirmed(e: MotionEvent): Boolean {
markerClickCallback?.onMarkerClick()
return true
}
private fun getRelativeX(ct: CoordinateTranslater, x: Float): Double {
return ct.translateAndScaleAbsoluteToRelativeX(x.toInt(), mapView.scale)
}
private fun getRelativeY(ct: CoordinateTranslater, y: Float): Double {
return ct.translateAndScaleAbsoluteToRelativeY(y.toInt(), mapView.scale)
}
private fun getConstrainedX(x: Double): Double? {
return mapView.getConstrainedX(x)
}
private fun getConstrainedY(y: Double): Double? {
return mapView.getConstrainedY(y)
}
/**
* A [MarkerMoveAgent] is given the "relative coordinates" of the view added to the [MapView].
* Most of the time, the callee sets the given coordinates of the view on the [MapView].
*/
fun interface MarkerMoveAgent {
fun onMarkerMove(mapView: MapView, view: View, x: Double, y: Double)
}
fun interface ClickCallback {
fun onMarkerClick()
}
}
from mapview.
fix
from mapview.
Related Issues (20)
- Redraw map tiles HOT 3
- Marker callout disappeared when clicking or moving map view HOT 1
- No tiles visible, ability to center? HOT 1
- Marker size when changing the zoom level HOT 3
- BufferOverflow on Channel coroutine HOT 3
- MarkerView is not removed from the MapView HOT 5
- rectangular tiles HOT 1
- scrollTo / scrollToAndCenter using different coordinates after scaling HOT 6
- Can you support drawing points? HOT 2
- Draw polygon HOT 1
- BitmapFactory: bitmap marked for reuse HOT 3
- Non-existent zoom on an overlay. HOT 21
- Clear memory HOT 2
- maker cant be moved as MapCompose HOT 1
- Mini gaps between tiles HOT 2
- How to click or long click on map to add marker.
- Support for .png deep zoom map
- bitmap marked for reuse can't fit new bitmap error (100% reproducible) for Remote HTTP mapview HOT 5
- Possibility to zoom farther out? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mapview.