forked from rnmapbox/maps
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:rnmapbox/maps
- Loading branch information
Showing
59 changed files
with
1,075 additions
and
868 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 168 additions & 0 deletions
168
...src/main/java/com/rnmapbox/rnmbx/components/annotation/RNMBXPointAnnotationCoordinator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
package com.rnmapbox.rnmbx.components.annotation | ||
|
||
import com.mapbox.maps.MapView | ||
import com.mapbox.maps.plugin.annotation.Annotation | ||
import com.mapbox.maps.plugin.annotation.AnnotationConfig | ||
import com.mapbox.maps.plugin.annotation.annotations | ||
import com.mapbox.maps.plugin.annotation.generated.OnPointAnnotationClickListener | ||
import com.mapbox.maps.plugin.annotation.generated.OnPointAnnotationDragListener | ||
import com.mapbox.maps.plugin.annotation.generated.PointAnnotation | ||
import com.mapbox.maps.plugin.annotation.generated.PointAnnotationManager | ||
import com.mapbox.maps.plugin.annotation.generated.PointAnnotationOptions | ||
import com.mapbox.maps.plugin.annotation.generated.createPointAnnotationManager | ||
import com.rnmapbox.rnmbx.components.annotation.RNMBXPointAnnotation | ||
import com.rnmapbox.rnmbx.utils.Logger | ||
|
||
class RNMBXPointAnnotationCoordinator(val mapView: MapView) { | ||
val manager: PointAnnotationManager; | ||
var annotationClicked = false | ||
var annotationDragged = false | ||
|
||
var selected: RNMBXPointAnnotation? = null | ||
|
||
val annotations: MutableMap<String, RNMBXPointAnnotation> = hashMapOf() | ||
val callouts: MutableMap<String, RNMBXPointAnnotation> = hashMapOf() | ||
|
||
init { | ||
manager = mapView.annotations.createPointAnnotationManager(AnnotationConfig(layerId = "RNMBX-mapview-annotations")) | ||
manager.addClickListener(OnPointAnnotationClickListener { pointAnnotation -> | ||
onAnnotationClick(pointAnnotation) | ||
false | ||
}) | ||
} | ||
|
||
fun getAndClearAnnotationClicked(): Boolean { | ||
if (annotationClicked) { | ||
annotationClicked = false | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
fun getAndClearAnnotationDragged(): Boolean { | ||
if (annotationDragged) { | ||
annotationDragged = false | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
fun lookupForClick(point: PointAnnotation): RNMBXPointAnnotation? { | ||
for (annotation in annotations.values) { | ||
if (point.id == annotation.mapboxID) { | ||
return annotation; | ||
} | ||
if (point.id == annotation.calloutMapboxID) { | ||
return null; | ||
} | ||
} | ||
Logger.e(LOG_TAG, "Failed to find RNMBXPointAnnotation for ${point.id}") | ||
return null; | ||
} | ||
|
||
fun onAnnotationClick(pointAnnotation: RNMBXPointAnnotation) { | ||
var oldSelected: RNMBXPointAnnotation? = selected | ||
var newSelected: RNMBXPointAnnotation? = pointAnnotation | ||
|
||
annotationClicked = true | ||
|
||
if (newSelected == oldSelected) { | ||
newSelected = null | ||
} | ||
|
||
manager.addDragListener(object : OnPointAnnotationDragListener { | ||
override fun onAnnotationDragStarted(_annotation: Annotation<*>) { | ||
annotationDragged = true; | ||
var reactAnnotation: RNMBXPointAnnotation? = null | ||
for (key in annotations.keys) { | ||
val annotation = annotations[key] | ||
val curMarkerID = annotation?.mapboxID | ||
if (_annotation.id == curMarkerID) { | ||
reactAnnotation = annotation | ||
} | ||
} | ||
reactAnnotation?.let { it.onDragStart() } | ||
} | ||
|
||
override fun onAnnotationDrag(_annotation: Annotation<*>) { | ||
var reactAnnotation: RNMBXPointAnnotation? = null | ||
for (key in annotations.keys) { | ||
val annotation = annotations[key] | ||
val curMarkerID = annotation?.mapboxID | ||
if (_annotation.id == curMarkerID) { | ||
reactAnnotation = annotation | ||
} | ||
} | ||
reactAnnotation?.let { it.onDrag() } | ||
} | ||
|
||
override fun onAnnotationDragFinished(_annotation: Annotation<*>) { | ||
annotationDragged = false; | ||
var reactAnnotation: RNMBXPointAnnotation? = null | ||
for (key in annotations.keys) { | ||
val annotation = annotations[key] | ||
val curMarkerID = annotation?.mapboxID | ||
if (_annotation.id == curMarkerID) { | ||
reactAnnotation = annotation | ||
} | ||
} | ||
reactAnnotation?.let { it.onDragEnd() } | ||
} | ||
}) | ||
|
||
oldSelected?.let { deselectAnnotation(it) } | ||
newSelected?.let { selectAnnotation(it) } | ||
|
||
} | ||
|
||
fun onAnnotationClick(point: PointAnnotation) { | ||
lookupForClick(point)?.let { | ||
onAnnotationClick(it) | ||
} | ||
} | ||
|
||
fun deselectSelectedAnnotation(): Boolean { | ||
selected?.let { | ||
deselectAnnotation(it) | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
fun selectAnnotation(annotation: RNMBXPointAnnotation) { | ||
selected = annotation | ||
annotation.doSelect(true) | ||
} | ||
|
||
fun deselectAnnotation(annotation: RNMBXPointAnnotation) { | ||
selected = null | ||
annotation.doDeselect() | ||
} | ||
|
||
fun remove(annotation: RNMBXPointAnnotation) { | ||
if (annotation == selected) { | ||
selected = null | ||
} | ||
annotations.remove(annotation.iD) | ||
} | ||
|
||
fun delete(annotation: PointAnnotation) { | ||
manager.delete(annotation) | ||
} | ||
|
||
fun update(annotation: PointAnnotation) { | ||
manager.update(annotation) | ||
} | ||
|
||
fun create(options: PointAnnotationOptions): PointAnnotation { | ||
return manager.create(options) | ||
} | ||
|
||
fun add(annotation: RNMBXPointAnnotation) { | ||
annotations[annotation.iD!!] = annotation | ||
} | ||
|
||
companion object { | ||
const val LOG_TAG = "RNMBXPointAnnotationCoordinator"; | ||
} | ||
} |
Oops, something went wrong.