Skip to content

Commit

Permalink
Enable drag annotation while changing the annotation to draggable in …
Browse files Browse the repository at this point in the history
…long click (#990)
  • Loading branch information
Kevin Li authored Dec 16, 2021
1 parent fe87eab commit 1f5b9f8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class CircleAnnotationActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
binding = ActivityAnnotationBinding.inflate(layoutInflater)
setContentView(binding.root)
Toast.makeText(this, "Long click a circle to start dragging.", Toast.LENGTH_LONG).show()
annotationPlugin = binding.mapView.annotations
circleAnnotationManager = annotationPlugin.createCircleAnnotationManager().apply {
addClickListener(
Expand All @@ -40,6 +41,14 @@ class CircleAnnotationActivity : AppCompatActivity() {
false
}
)
addLongClickListener(
OnCircleAnnotationLongClickListener {
it.isDraggable = true
Toast.makeText(this@CircleAnnotationActivity, "long click: ${it.id}", Toast.LENGTH_SHORT)
.show()
false
}
)
binding.mapView.getMapboxMap().loadStyleUri(nextStyle) {
addInteractionListener(
object : OnCircleAnnotationInteractionListener {
Expand All @@ -64,7 +73,7 @@ class CircleAnnotationActivity : AppCompatActivity() {
.withPoint(Point.fromLngLat(CIRCLE_LONGITUDE, CIRCLE_LATITUDE))
.withCircleColor(Color.YELLOW)
.withCircleRadius(12.0)
.withDraggable(true)
.withDraggable(false)
create(circleAnnotationOptions)

// random add circles across the globe
Expand All @@ -76,7 +85,7 @@ class CircleAnnotationActivity : AppCompatActivity() {
.withPoint(AnnotationUtils.createRandomPoint())
.withCircleColor(color)
.withCircleRadius(8.0)
.withDraggable(true)
.withDraggable(false)
)
}
create(circleAnnotationOptionsList)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,10 @@ abstract class AnnotationManagerImpl<G : Geometry, T : Annotation<G>, S : Annota
dragSource?.let { geoJsonSource ->
dragLayer?.let { layer ->
if (!style.styleSourceExists(geoJsonSource.sourceId) || !style.styleLayerExists(layer.layerId)) {
Logger.e(TAG, "Can't update dragSource: drag source or layer has not been added to style.")
Logger.e(
TAG,
"Can't update dragSource: drag source or layer has not been added to style."
)
return@getStyle
}
addIconToStyle(style, dragAnnotationMap.values)
Expand Down Expand Up @@ -675,19 +678,31 @@ abstract class AnnotationManagerImpl<G : Geometry, T : Annotation<G>, S : Annota
stopDragging()
return true
}
val shiftedGeometry: G? = delegateProvider.let {

if (annotationMap.containsKey(annotation.id)) {
// Delete the dragging annotation from original source and add it to drag source
annotationMap.remove(annotation.id)
dragAnnotationMap[annotation.id] = annotation
updateSource()
}

delegateProvider.let {
annotation.getOffsetGeometry(
it.mapCameraManagerDelegate, moveObject
)
}
shiftedGeometry?.let { geometry ->
}?.let { geometry ->
annotation.geometry = geometry
updateDragSource()
dragListeners.forEach {
it.onAnnotationDrag(annotation)
}
return true
}

/* The dragging annotation has been removed from original source,
update drag source to make sure it is shown in drag layer.
*/
updateDragSource()
}
return false
}
Expand All @@ -701,19 +716,9 @@ abstract class AnnotationManagerImpl<G : Geometry, T : Annotation<G>, S : Annota
}

private fun startDragging(annotation: T): Boolean {
if (annotation.isDraggable) {
dragListeners.forEach { it.onAnnotationDragStarted(annotation) }
draggingAnnotation = annotation
if (annotationMap.containsKey(annotation.id)) {
// Delete the dragging annotation from original source and add it to drag source
annotationMap.remove(annotation.id)
dragAnnotationMap[annotation.id] = annotation
updateSource()
updateDragSource()
}
return true
}
return false
dragListeners.forEach { it.onAnnotationDragStarted(annotation) }
draggingAnnotation = annotation
return true
}

private fun stopDragging() {
Expand Down

0 comments on commit 1f5b9f8

Please sign in to comment.