Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions android/src/main/java/com/luggmaps/LuggMapView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ interface LuggMapViewEventDelegate {
gesture: Boolean
)
fun onReady(view: LuggMapView)
fun onPress(view: LuggMapView, latitude: Double, longitude: Double, x: Float, y: Float)
fun onLongPress(view: LuggMapView, latitude: Double, longitude: Double, x: Float, y: Float)
}

@SuppressLint("ViewConstructor")
Expand Down Expand Up @@ -141,6 +143,14 @@ class LuggMapView(private val reactContext: ThemedReactContext) :
eventDelegate?.onCameraIdle(this, latitude, longitude, zoom, gesture)
}

override fun mapProviderDidPress(latitude: Double, longitude: Double, x: Float, y: Float) {
eventDelegate?.onPress(this, latitude, longitude, x, y)
}

override fun mapProviderDidLongPress(latitude: Double, longitude: Double, x: Float, y: Float) {
eventDelegate?.onLongPress(this, latitude, longitude, x, y)
}

// endregion

// region Property Setters
Expand Down
12 changes: 12 additions & 0 deletions android/src/main/java/com/luggmaps/LuggMapViewManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import com.facebook.react.viewmanagers.LuggMapViewManagerInterface
import com.google.android.gms.maps.model.LatLng
import com.luggmaps.events.CameraIdleEvent
import com.luggmaps.events.CameraMoveEvent
import com.luggmaps.events.LongPressEvent
import com.luggmaps.events.PressEvent
import com.luggmaps.events.ReadyEvent
import com.luggmaps.extensions.dispatchEvent

Expand All @@ -35,6 +37,8 @@ class LuggMapViewManager :

override fun getExportedCustomDirectEventTypeConstants(): Map<String, Any> =
mapOf(
"topMapPress" to mapOf("registrationName" to "onMapPress"),
"topMapLongPress" to mapOf("registrationName" to "onMapLongPress"),
"topCameraMove" to mapOf("registrationName" to "onCameraMove"),
"topCameraIdle" to mapOf("registrationName" to "onCameraIdle"),
"topReady" to mapOf("registrationName" to "onReady")
Expand Down Expand Up @@ -64,6 +68,14 @@ class LuggMapViewManager :
view.dispatchEvent(ReadyEvent(view))
}

override fun onPress(view: LuggMapView, latitude: Double, longitude: Double, x: Float, y: Float) {
view.dispatchEvent(PressEvent(view, latitude, longitude, x, y))
}

override fun onLongPress(view: LuggMapView, latitude: Double, longitude: Double, x: Float, y: Float) {
view.dispatchEvent(LongPressEvent(view, latitude, longitude, x, y))
}

@ReactProp(name = "provider")
override fun setProvider(view: LuggMapView, value: String?) {
view.setProvider(value)
Expand Down
3 changes: 3 additions & 0 deletions android/src/main/java/com/luggmaps/LuggPolygonViewManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class LuggPolygonViewManager :
override fun getName(): String = NAME
override fun createViewInstance(context: ThemedReactContext): LuggPolygonView = LuggPolygonView(context)

override fun getExportedCustomDirectEventTypeConstants(): Map<String, Any> =
mapOf("topPolygonPress" to mapOf("registrationName" to "onPolygonPress"))

override fun onDropViewInstance(view: LuggPolygonView) {
super.onDropViewInstance(view)
view.onDropViewInstance()
Expand Down
36 changes: 33 additions & 3 deletions android/src/main/java/com/luggmaps/core/GoogleMapProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class GoogleMapProvider(private val context: Context) :
GoogleMap.OnCameraMoveStartedListener,
GoogleMap.OnCameraMoveListener,
GoogleMap.OnCameraIdleListener,
GoogleMap.OnMapClickListener,
GoogleMap.OnMapLongClickListener,
GoogleMap.OnPolygonClickListener {

override var delegate: MapProviderDelegate? = null
Expand All @@ -50,6 +52,7 @@ class GoogleMapProvider(private val context: Context) :
private val pendingPolygonViews = mutableSetOf<LuggPolygonView>()
private val polylineAnimators = mutableMapOf<LuggPolylineView, PolylineAnimator>()
private val polygonToViewMap = mutableMapOf<Polygon, LuggPolygonView>()
private var tapLocation: LatLng? = null

// Initial camera settings
private var initialLatitude: Double = 0.0
Expand Down Expand Up @@ -107,6 +110,8 @@ class GoogleMapProvider(private val context: Context) :
googleMap?.setOnCameraMoveStartedListener(null)
googleMap?.setOnCameraMoveListener(null)
googleMap?.setOnCameraIdleListener(null)
googleMap?.setOnMapClickListener(null)
googleMap?.setOnMapLongClickListener(null)
googleMap?.setOnPolygonClickListener(null)
googleMap?.clear()
googleMap = null
Expand All @@ -126,8 +131,16 @@ class GoogleMapProvider(private val context: Context) :
map.setOnCameraMoveStartedListener(this)
map.setOnCameraMoveListener(this)
map.setOnCameraIdleListener(this)
map.setOnMapClickListener(this)
map.setOnMapLongClickListener(this)
map.setOnPolygonClickListener(this)

wrapperView?.touchEventHandler = { event ->
if (event.action == android.view.MotionEvent.ACTION_DOWN) {
tapLocation = map.projection.fromScreenLocation(android.graphics.Point(event.x.toInt(), event.y.toInt()))
}
}

applyUiSettings()
applyZoomLimits()
applyEdgeInsets()
Expand Down Expand Up @@ -167,8 +180,25 @@ class GoogleMapProvider(private val context: Context) :
isDragging = false
}

override fun onMapClick(latLng: LatLng) {
val map = googleMap ?: return
val point = map.projection.toScreenLocation(latLng)
delegate?.mapProviderDidPress(latLng.latitude, latLng.longitude, point.x.toFloat(), point.y.toFloat())
}

override fun onMapLongClick(latLng: LatLng) {
val map = googleMap ?: return
val point = map.projection.toScreenLocation(latLng)
delegate?.mapProviderDidLongPress(latLng.latitude, latLng.longitude, point.x.toFloat(), point.y.toFloat())
}

override fun onPolygonClick(polygon: Polygon) {
polygonToViewMap[polygon]?.emitPressEvent()
val polygonView = polygonToViewMap[polygon]
if (polygonView?.tappable == true) {
polygonView.emitPressEvent()
} else {
onMapClick(tapLocation ?: return)
}
}

// endregion
Expand Down Expand Up @@ -465,7 +495,7 @@ class GoogleMapProvider(private val context: Context) :
strokeColor = polygonView.strokeColor
strokeWidth = polygonView.strokeWidth.dpToPx()
zIndex = polygonView.zIndex
isClickable = polygonView.tappable
isClickable = true
}
}

Expand All @@ -484,9 +514,9 @@ class GoogleMapProvider(private val context: Context) :
.strokeColor(polygonView.strokeColor)
.strokeWidth(polygonView.strokeWidth.dpToPx())
.zIndex(polygonView.zIndex)
.clickable(true)

val polygon = map.addPolygon(options)
polygon.isClickable = polygonView.tappable
polygonView.polygon = polygon
polygonToViewMap[polygon] = polygonView
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ interface MapProviderDelegate {
fun mapProviderDidReady()
fun mapProviderDidMoveCamera(latitude: Double, longitude: Double, zoom: Float, gesture: Boolean)
fun mapProviderDidIdleCamera(latitude: Double, longitude: Double, zoom: Float, gesture: Boolean)
fun mapProviderDidPress(latitude: Double, longitude: Double, x: Float, y: Float)
fun mapProviderDidLongPress(latitude: Double, longitude: Double, x: Float, y: Float)
}

interface MapProvider {
Expand Down
34 changes: 34 additions & 0 deletions android/src/main/java/com/luggmaps/events/LongPressEvent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.luggmaps.events

import android.view.View
import com.facebook.react.bridge.Arguments
import com.facebook.react.uimanager.UIManagerHelper
import com.facebook.react.uimanager.events.Event

class LongPressEvent(
view: View,
private val latitude: Double,
private val longitude: Double,
private val x: Float,
private val y: Float
) : Event<LongPressEvent>(UIManagerHelper.getSurfaceId(view), view.id) {
override fun getEventName() = "topMapLongPress"

override fun getEventData() =
Arguments.createMap().apply {
putMap(
"coordinate",
Arguments.createMap().apply {
putDouble("latitude", latitude)
putDouble("longitude", longitude)
}
)
putMap(
"point",
Arguments.createMap().apply {
putDouble("x", x.toDouble())
putDouble("y", y.toDouble())
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.facebook.react.uimanager.UIManagerHelper
import com.facebook.react.uimanager.events.Event

class PolygonPressEvent(view: View) : Event<PolygonPressEvent>(UIManagerHelper.getSurfaceId(view), view.id) {
override fun getEventName() = "topPress"
override fun getEventName() = "topPolygonPress"

override fun getEventData() = Arguments.createMap()
}
34 changes: 34 additions & 0 deletions android/src/main/java/com/luggmaps/events/PressEvent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.luggmaps.events

import android.view.View
import com.facebook.react.bridge.Arguments
import com.facebook.react.uimanager.UIManagerHelper
import com.facebook.react.uimanager.events.Event

class PressEvent(
view: View,
private val latitude: Double,
private val longitude: Double,
private val x: Float,
private val y: Float
) : Event<PressEvent>(UIManagerHelper.getSurfaceId(view), view.id) {
override fun getEventName() = "topMapPress"

override fun getEventData() =
Arguments.createMap().apply {
putMap(
"coordinate",
Arguments.createMap().apply {
putDouble("latitude", latitude)
putDouble("longitude", longitude)
}
)
putMap(
"point",
Arguments.createMap().apply {
putDouble("x", x.toDouble())
putDouble("y", y.toDouble())
}
)
}
}
15 changes: 15 additions & 0 deletions example/shared/src/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
MapProvider,
type MapProviderType,
type MapCameraEvent,
type MapPressEvent,
} from '@lugg/maps';
import {
TrueSheet,
Expand Down Expand Up @@ -94,6 +95,18 @@ function HomeContent() {
[getSheetBottom]
);

const formatPressEvent = useCallback(
(event: MapPressEvent, label: string) => {
const { coordinate, point } = event.nativeEvent;
const lat = coordinate.latitude.toFixed(5);
const lng = coordinate.longitude.toFixed(5);
const px = point.x.toFixed(0);
const py = point.y.toFixed(0);
setStatusText(`${label}: ${lat}, ${lng} (${px}, ${py})`);
},
[]
);

const formatCameraEvent = useCallback(
(event: MapCameraEvent, idle: boolean) => {
const { coordinate, zoom, gesture } = event.nativeEvent;
Expand Down Expand Up @@ -166,6 +179,8 @@ function HomeContent() {
animatedPosition={animatedPosition}
userLocationEnabled={locationPermission}
onReady={handleMapReady}
onPress={(e) => formatPressEvent(e, 'Press')}
onLongPress={(e) => formatPressEvent(e, 'Long press')}
onCameraMove={(e) => formatCameraEvent(e, false)}
onCameraIdle={(e) => formatCameraEvent(e, true)}
onPolygonPress={() => setStatusText('Polygon pressed')}
Expand Down
4 changes: 4 additions & 0 deletions example/shared/src/components/Map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ export const Map = forwardRef<MapView, MapProps>(
animatedPosition,
onCameraIdle,
onCameraMove,
onPress,
onLongPress,
onPolygonPress,
...props
},
Expand Down Expand Up @@ -148,6 +150,8 @@ export const Map = forwardRef<MapView, MapProps>(
initialZoom={INITIAL_ZOOM}
userLocationEnabled
edgeInsets={edgeInsets}
onPress={onPress}
onLongPress={onLongPress}
onCameraMove={handleCameraMove}
onCameraIdle={handleCameraIdle}
{...props}
Expand Down
18 changes: 18 additions & 0 deletions ios/LuggMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#import "core/MapProviderDelegate.h"
#import "events/CameraIdleEvent.h"
#import "events/CameraMoveEvent.h"
#import "events/LongPressEvent.h"
#import "events/PressEvent.h"
#import "events/ReadyEvent.h"

#import <react/renderer/components/RNMapsSpec/ComponentDescriptors.h>
Expand Down Expand Up @@ -203,6 +205,22 @@ - (void)mapProviderDidIdleCamera:(double)latitude
.emit<LuggMapViewEventEmitter>(_eventEmitter);
}

- (void)mapProviderDidPress:(double)latitude
longitude:(double)longitude
x:(double)x
y:(double)y {
PressEvent{latitude, longitude, x, y}
.emit<LuggMapViewEventEmitter>(_eventEmitter);
}

- (void)mapProviderDidLongPress:(double)latitude
longitude:(double)longitude
x:(double)x
y:(double)y {
LongPressEvent{latitude, longitude, x, y}
.emit<LuggMapViewEventEmitter>(_eventEmitter);
}

#pragma mark - Property Setters

- (void)applyProps {
Expand Down
Loading