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
185 changes: 41 additions & 144 deletions android/src/main/java/com/rngooglemapsplus/GoogleMapsViewImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.rngooglemapsplus

import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.location.Location
import android.util.Base64
import android.util.Size
import android.widget.FrameLayout
Expand All @@ -11,7 +10,6 @@ import com.facebook.react.bridge.LifecycleEventListener
import com.facebook.react.bridge.UiThreadUtil
import com.facebook.react.uimanager.PixelUtil.dpToPx
import com.facebook.react.uimanager.ThemedReactContext
import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.GoogleMapOptions
Expand Down Expand Up @@ -39,7 +37,11 @@ import com.rngooglemapsplus.extensions.toLatLng
import com.rngooglemapsplus.extensions.toLocationErrorCode
import com.rngooglemapsplus.extensions.toRNIndoorBuilding
import com.rngooglemapsplus.extensions.toRNIndoorLevel
import com.rngooglemapsplus.extensions.toRNMapErrorCodeOrNull
import com.rngooglemapsplus.extensions.toRnCamera
import com.rngooglemapsplus.extensions.toRnLatLng
import com.rngooglemapsplus.extensions.toRnLocation
import com.rngooglemapsplus.extensions.toRnRegion
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.File
Expand All @@ -65,6 +67,7 @@ class GoogleMapsViewImpl(
LifecycleEventListener {
private var initialized = false
private var mapReady = false
private var destroyed = false
private var googleMap: GoogleMap? = null
private var mapView: MapView? = null

Expand All @@ -83,7 +86,6 @@ class GoogleMapsViewImpl(
private val kmlLayersById = mutableMapOf<String, KmlLayer>()

private var cameraMoveReason = -1
private var lastSubmittedLocation: Location? = null
private var lastSubmittedCameraPosition: CameraPosition? = null

init {
Expand All @@ -94,31 +96,16 @@ class GoogleMapsViewImpl(
if (initialized) return
initialized = true
val result = playServiceHandler.playServicesAvailability()
val errorCode = result.toRNMapErrorCodeOrNull()

when (result) {
ConnectionResult.SERVICE_MISSING -> {
onMapError?.invoke(RNMapErrorCode.PLAY_SERVICES_MISSING)
return
}
if (errorCode != null) {
onMapError?.invoke(errorCode)

ConnectionResult.SERVICE_INVALID -> {
onMapError?.invoke(RNMapErrorCode.PLAY_SERVICES_INVALID)
if (errorCode == RNMapErrorCode.PLAY_SERVICES_MISSING ||
errorCode == RNMapErrorCode.PLAY_SERVICES_INVALID
) {
return
}

ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED ->
onMapError?.invoke(RNMapErrorCode.PLAY_SERVICES_OUTDATED)

ConnectionResult.SERVICE_UPDATING ->
onMapError?.invoke(RNMapErrorCode.PLAY_SERVICE_UPDATING)

ConnectionResult.SERVICE_DISABLED ->
onMapError?.invoke(RNMapErrorCode.PLAY_SERVICES_DISABLED)

ConnectionResult.SUCCESS -> {}

else ->
onMapError?.invoke(RNMapErrorCode.UNKNOWN)
}

mapView =
Expand All @@ -143,8 +130,8 @@ class GoogleMapsViewImpl(
googleMap?.setOnMapClickListener(this@GoogleMapsViewImpl)
googleMap?.setOnMarkerDragListener(this@GoogleMapsViewImpl)
}
applyProps()
initLocationCallbacks()
applyPending()
mapReady = true
onMapReady?.invoke(true)
}
Expand All @@ -160,21 +147,9 @@ class GoogleMapsViewImpl(
}
val isGesture = GoogleMap.OnCameraMoveStartedListener.REASON_GESTURE == reason

val latDelta = bounds.northeast.latitude - bounds.southwest.latitude
val lngDelta = bounds.northeast.longitude - bounds.southwest.longitude

onCameraChangeStart?.invoke(
RNRegion(
center = bounds.center.toRnLatLng(),
latitudeDelta = latDelta,
longitudeDelta = lngDelta,
),
RNCamera(
center = cameraPosition.target.toRnLatLng(),
zoom = cameraPosition.zoom.toDouble(),
bearing = cameraPosition.bearing.toDouble(),
tilt = cameraPosition.tilt.toDouble(),
),
bounds.toRnRegion(),
cameraPosition.toRnCamera(),
isGesture,
)
}
Expand All @@ -192,21 +167,9 @@ class GoogleMapsViewImpl(

val isGesture = GoogleMap.OnCameraMoveStartedListener.REASON_GESTURE == cameraMoveReason

val latDelta = bounds.northeast.latitude - bounds.southwest.latitude
val lngDelta = bounds.northeast.longitude - bounds.southwest.longitude

onCameraChange?.invoke(
RNRegion(
center = bounds.center.toRnLatLng(),
latitudeDelta = latDelta,
longitudeDelta = lngDelta,
),
RNCamera(
center = cameraPosition.target.toRnLatLng(),
zoom = cameraPosition.zoom.toDouble(),
bearing = cameraPosition.bearing.toDouble(),
tilt = cameraPosition.tilt.toDouble(),
),
onCameraChangeStart?.invoke(
bounds.toRnRegion(),
cameraPosition.toRnCamera(),
isGesture,
)
}
Expand All @@ -220,39 +183,16 @@ class GoogleMapsViewImpl(
}
val isGesture = GoogleMap.OnCameraMoveStartedListener.REASON_GESTURE == cameraMoveReason

val latDelta = bounds.northeast.latitude - bounds.southwest.latitude
val lngDelta = bounds.northeast.longitude - bounds.southwest.longitude

onCameraChangeComplete?.invoke(
RNRegion(
center = bounds.center.toRnLatLng(),
latitudeDelta = latDelta,
longitudeDelta = lngDelta,
),
RNCamera(
center = cameraPosition.target.toRnLatLng(),
zoom = cameraPosition.zoom.toDouble(),
bearing = cameraPosition.bearing.toDouble(),
tilt = cameraPosition.tilt.toDouble(),
),
onCameraChangeStart?.invoke(
bounds.toRnRegion(),
cameraPosition.toRnCamera(),
isGesture,
)
}

fun initLocationCallbacks() {
locationHandler.onUpdate = { location ->
// / only the coordinated are relevant right now
if (lastSubmittedLocation?.latitude != location.latitude || lastSubmittedLocation?.longitude != location.longitude ||
lastSubmittedLocation?.bearing != location.bearing
) {
onLocationUpdate?.invoke(
RNLocation(
RNLatLng(location.latitude, location.longitude),
location.bearing.toDouble(),
),
)
}
lastSubmittedLocation = location
onLocationUpdate?.invoke(location.toRnLocation())
}

locationHandler.onError = { error ->
Expand All @@ -261,65 +201,18 @@ class GoogleMapsViewImpl(
locationHandler.start()
}

fun applyPending() {
onUi {
mapPadding?.let {
googleMap?.setPadding(
it.left.dpToPx().toInt(),
it.top.dpToPx().toInt(),
it.right.dpToPx().toInt(),
it.bottom.dpToPx().toInt(),
)
}

uiSettings?.let { v ->
googleMap?.uiSettings?.apply {
v.allGesturesEnabled?.let { setAllGesturesEnabled(it) }
v.compassEnabled?.let { isCompassEnabled = it }
v.indoorLevelPickerEnabled?.let { isIndoorLevelPickerEnabled = it }
v.mapToolbarEnabled?.let { isMapToolbarEnabled = it }
v.myLocationButtonEnabled?.let {
googleMap?.setLocationSource(locationHandler)
isMyLocationButtonEnabled = it
}
v.rotateEnabled?.let { isRotateGesturesEnabled = it }
v.scrollEnabled?.let { isScrollGesturesEnabled = it }
v.scrollDuringRotateOrZoomEnabled?.let {
isScrollGesturesEnabledDuringRotateOrZoom = it
}
v.tiltEnabled?.let { isTiltGesturesEnabled = it }
v.zoomControlsEnabled?.let { isZoomControlsEnabled = it }
v.zoomGesturesEnabled?.let { isZoomGesturesEnabled = it }
}
}

buildingEnabled?.let {
googleMap?.isBuildingsEnabled = it
}
trafficEnabled?.let {
googleMap?.isTrafficEnabled = it
}
indoorEnabled?.let {
googleMap?.isIndoorEnabled = it
}
googleMap?.setMapStyle(customMapStyle)
mapType?.let {
googleMap?.mapType = it
}
userInterfaceStyle?.let {
googleMap?.mapColorScheme = it
}
mapZoomConfig?.let {
googleMap?.setMinZoomPreference(it.min?.toFloat() ?: 2.0f)
googleMap?.setMaxZoomPreference(it.max?.toFloat() ?: 21.0f)
}
}

locationConfig?.let {
locationHandler.priority = it.android?.priority?.toGooglePriority()
locationHandler.interval = it.android?.interval?.toLong()
locationHandler.minUpdateInterval = it.android?.minUpdateInterval?.toLong()
}
fun applyProps() {
mapPadding = mapPadding
uiSettings = uiSettings
myLocationEnabled = myLocationEnabled
buildingEnabled = buildingEnabled
trafficEnabled = trafficEnabled
indoorEnabled = indoorEnabled
customMapStyle = customMapStyle
mapType = mapType
userInterfaceStyle = userInterfaceStyle
mapZoomConfig = mapZoomConfig
locationConfig = locationConfig

if (pendingMarkers.isNotEmpty()) {
pendingMarkers.forEach { (id, opts) ->
Expand Down Expand Up @@ -480,9 +373,11 @@ class GoogleMapsViewImpl(
var locationConfig: RNLocationConfig? = null
set(value) {
field = value
locationHandler.priority = value?.android?.priority?.toGooglePriority()
locationHandler.interval = value?.android?.interval?.toLong()
locationHandler.minUpdateInterval = value?.android?.minUpdateInterval?.toLong()
locationHandler.updateConfig(
value?.android?.priority?.toGooglePriority(),
value?.android?.interval?.toLong(),
value?.android?.minUpdateInterval?.toLong(),
)
}

var onMapError: ((RNMapErrorCode) -> Unit)? = null
Expand Down Expand Up @@ -919,7 +814,7 @@ class GoogleMapsViewImpl(
onUi {
heatmapsById.values.forEach { it.remove() }
}
circlesById.clear()
heatmapsById.clear()
pendingHeatmaps.clear()
}

Expand Down Expand Up @@ -968,6 +863,8 @@ class GoogleMapsViewImpl(
}

fun destroyInternal() {
if (destroyed) return
destroyed = true
onUi {
locationHandler.stop()
markerBuilder.cancelAllJobs()
Expand Down
Loading
Loading