Skip to content

Commit

Permalink
Merge branch 'main' of github.com:rnmapbox/maps
Browse files Browse the repository at this point in the history
  • Loading branch information
naftalibeder committed Jan 31, 2024
2 parents 8cc312b + 13ea927 commit bd74cbc
Show file tree
Hide file tree
Showing 59 changed files with 1,075 additions and 868 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ _See [iOS](ios/install.md) & [Android](android/install.md) setup guide for using

- [node](https://nodejs.org)
- [npm](https://www.npmjs.com/)
- [React Native](https://facebook.github.io/react-native/) (0.64+)
- [React Native](https://facebook.github.io/react-native/) (0.70+, older versions from 0.64+ might or might not work)


## Installation

Check our [Installation insructions](https://rnmapbox.github.io/docs/install)
Check our [Installation instructions](https://rnmapbox.github.io/docs/install)

### Getting Started
For more information, check out our [Getting Started](/docs/GettingStarted.md) section
Expand Down Expand Up @@ -154,7 +154,7 @@ const styles = StyleSheet.create({
- [Callout](/docs/Callout.md)
- [Camera](docs/Camera.md)
- [UserLocation](docs/UserLocation.md)
- [LocaitonPuck](docs/LocationPuck.md)
- [LocationPuck](docs/LocationPuck.md)
- [Images](docs/Images.md)
- [Image](docs/Image.md)
- [Models](docs/Models.md)
Expand Down
19 changes: 17 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
def defaultMapboxMapsImpl = "mapbox"
def defaultMapboxMapsVersion = "10.16.2"
def defaultMapboxMapsVersion = "10.16.4"

def safeExtGet(prop, fallback) {
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
Expand Down Expand Up @@ -43,7 +43,11 @@ if (safeExtGet("RNMapboxMapsImpl", defaultMapboxMapsImpl) == "mapbox") {
}

android {
namespace = "com.rnmapbox.rnmbx"
def agpVersion = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION
// Check AGP version for backward compatibility reasons
if (agpVersion.tokenize('.')[0].toInteger() >= 7) {
namespace = "com.rnmapbox.rnmbx"
}
if (safeExtGet("RNMapboxMapsImpl", defaultMapboxMapsImpl) == "maplibre") {
msg = '@rnmapbox/maps: Maplibre implementation has been removed, set RNMapboxMapsImpl to mapbox- see https://github.com/rnmapbox/maps/wiki/Deprecated-RNMapboxImpl-Maplibre#android'
logger.error(msg)
Expand Down Expand Up @@ -161,4 +165,15 @@ dependencies {
implementation "com.squareup.okhttp3:okhttp:4.9.0"
implementation "com.squareup.okhttp3:okhttp-urlconnection:4.9.0"
}

testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
testImplementation 'org.junit.jupiter:junit-jupiter:5.8.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
testImplementation 'org.jetbrains.kotlin:kotlin-test'
testImplementation "org.mockito.kotlin:mockito-kotlin:5.2.1"
}


tasks.withType(Test).configureEach {
useJUnitPlatform()
}
2 changes: 1 addition & 1 deletion android/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Set `RNMapboxMapsVersion` in `android/build.gradle > buildscript > ext` section
```groovy
buildscript {
ext {
RNMapboxMapsVersion = '11.0.0'
RNMapboxMapsVersion = '11.1.0'
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import com.mapbox.maps.ScreenCoordinate
import com.mapbox.maps.viewannotation.ViewAnnotationManager
import com.rnmapbox.rnmbx.components.mapview.RNMBXMapView
import com.rnmapbox.rnmbx.v11compat.annotation.*
import com.rnmapbox.rnmbx.utils.LatLng
import com.rnmapbox.rnmbx.utils.GeoJSONUtils.toGNPointGeometry

class RNMBXMarkerViewManager(reactApplicationContext: ReactApplicationContext) :
AbstractEventEmitter<RNMBXMarkerView>(reactApplicationContext),
Expand All @@ -35,8 +37,9 @@ class RNMBXMarkerViewManager(reactApplicationContext: ReactApplicationContext) :
}

@ReactProp(name = "coordinate")
override fun setCoordinate(markerView: RNMBXMarkerView, geoJSONStr: Dynamic) {
markerView.setCoordinate(toPointGeometry(geoJSONStr.asString()))
override fun setCoordinate(markerView: RNMBXMarkerView, value: Dynamic) {
val array = value.asArray()
markerView.setCoordinate(toGNPointGeometry(LatLng(array.getDouble(1), array.getDouble(0))))
}

@ReactProp(name = "anchor")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import java.util.*
import com.rnmapbox.rnmbx.v11compat.annotation.*;

class RNMBXPointAnnotation(private val mContext: Context, private val mManager: RNMBXPointAnnotationManager) : AbstractMapFeature(mContext), View.OnLayoutChangeListener {

var pointAnnotations: RNMBXPointAnnotationCoordinator? = null
var annotation: PointAnnotation? = null
private set
private var mMap: MapboxMap? = null
Expand Down Expand Up @@ -79,6 +81,7 @@ class RNMBXPointAnnotation(private val mContext: Context, private val mManager:
override fun addToMap(mapView: RNMBXMapView) {
super.addToMap(mapView)
mMap = mapView.getMapboxMap()
pointAnnotations = mapView.pointAnnotations
makeMarker()
if (mChildView != null) {
if (!mChildView!!.isAttachedToWindow) {
Expand All @@ -96,16 +99,13 @@ class RNMBXPointAnnotation(private val mContext: Context, private val mManager:
}

override fun removeFromMap(mapView: RNMBXMapView, reason: RemovalReason): Boolean {
val map = (if (mMapView != null) mMapView else mapView) ?: return true
if (annotation != null) {
map.pointAnnotationManager?.delete(annotation!!)
}
if (mChildView != null) {
map.offscreenAnnotationViewContainer?.removeView(mChildView)
}
if (calloutView != null) {
map.offscreenAnnotationViewContainer?.removeView(calloutView)
}
val map = mMapView ?: mapView

annotation?.let { map.pointAnnotations?.delete(it) }

mChildView?.let { map.offscreenAnnotationViewContainer?.removeView(it) }
calloutView?.let { map.offscreenAnnotationViewContainer?.removeView(it)}

return super.removeFromMap(mapView, reason)
}

Expand Down Expand Up @@ -138,33 +138,36 @@ class RNMBXPointAnnotation(private val mContext: Context, private val mManager:
val latLng: LatLng?
get() = mCoordinate?.let { GeoJSONUtils.toLatLng(it) }
val mapboxID: AnnotationID
get() = if (annotation == null) INVALID_ANNOTATION_ID else annotation!!.id
get() = annotation?.id ?: INVALID_ANNOTATION_ID

val calloutMapboxID: AnnotationID
get() = mCalloutSymbol?.id ?: INVALID_ANNOTATION_ID

fun setCoordinate(point: Point) {
mCoordinate = point
annotation?.let {
it.point = point
mMapView?.pointAnnotationManager?.update(it)
pointAnnotations?.update(it)
}
mCalloutSymbol?.let {
it.point = point
mMapView?.pointAnnotationManager?.update(it)
pointAnnotations?.update(it)
}
}

fun setAnchor(x: Float, y: Float) {
mAnchor = arrayOf(x, y)
if (annotation != null) {
annotation?.let { annotation ->
updateAnchor()
mMapView?.pointAnnotationManager?.update(annotation!!)
pointAnnotations?.update(annotation)
}
}

fun setDraggable(draggable: Boolean) {
mDraggable = draggable
annotation?.let {
it.isDraggable = draggable
mMapView?.pointAnnotationManager?.update(it)
annotation?.let { annotation ->
annotation.isDraggable = draggable
pointAnnotations?.update(annotation)
}
}

Expand All @@ -179,8 +182,8 @@ class RNMBXPointAnnotation(private val mContext: Context, private val mManager:

fun doDeselect() {
mManager.handleEvent(makeEvent(false))
if (mCalloutSymbol != null) {
mMapView?.pointAnnotationManager?.delete(mCalloutSymbol!!)
mCalloutSymbol?.let { mCalloutSymbol ->
pointAnnotations?.delete(mCalloutSymbol)
}
}

Expand All @@ -207,19 +210,18 @@ class RNMBXPointAnnotation(private val mContext: Context, private val mManager:
.withIconSize(1.0)
.withSymbolSortKey(10.0)
}
mMapView?.pointAnnotationManager?.let { annotationManager ->
options?.let {
annotation = annotationManager.create(options)
updateOptions()
}
annotation = null
options?.let {
annotation = pointAnnotations?.create(it)
updateOptions()
}
}

private fun updateOptions() {
if (annotation != null) {
annotation?.let {
updateIconImage()
updateAnchor()
mMapView?.pointAnnotationManager?.update(annotation!!)
pointAnnotations?.update(it)
}
}

Expand Down Expand Up @@ -268,7 +270,7 @@ class RNMBXPointAnnotation(private val mContext: Context, private val mManager:
.withDraggable(false)
}
}
val symbolManager = mMapView?.pointAnnotationManager
val symbolManager = pointAnnotations
if (symbolManager != null && options != null) {
mCalloutSymbol = symbolManager.create(options)
}
Expand Down
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";
}
}
Loading

0 comments on commit bd74cbc

Please sign in to comment.