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
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class GoogleMapsViewImpl(
val locationHandler: LocationHandler,
val playServiceHandler: PlayServicesHandler,
val markerBuilder: MapMarkerBuilder,
val mapErrorHandler: MapErrorHandler,
) : FrameLayout(reactContext),
GoogleMap.OnCameraMoveStartedListener,
GoogleMap.OnCameraMoveListener,
Expand Down Expand Up @@ -135,7 +136,7 @@ class GoogleMapsViewImpl(
val result = playServiceHandler.playServicesAvailability()
val errorCode = result.toRNMapErrorCodeOrNull()
if (errorCode != null) {
onMapError?.invoke(errorCode)
mapErrorHandler.report(errorCode, "play services unavailable")
if (errorCode == RNMapErrorCode.PLAY_SERVICES_MISSING ||
errorCode == RNMapErrorCode.PLAY_SERVICES_INVALID
) {
Expand Down Expand Up @@ -396,7 +397,6 @@ class GoogleMapsViewImpl(
)
}

var onMapError: ((RNMapErrorCode) -> Unit)? = null
var onMapReady: ((Boolean) -> Unit)? = null
var onMapLoaded: ((RNRegion, RNCamera) -> Unit)? = null
var onLocationUpdate: ((RNLocation) -> Unit)? = null
Expand Down Expand Up @@ -505,7 +505,7 @@ class GoogleMapsViewImpl(
onUi {
googleMap?.snapshot { bitmap ->
bitmap
?.encode(context, size, format, compressFormat, quality, resultIsFile)
?.encode(context, size, format, compressFormat, quality, resultIsFile, mapErrorHandler)
?.let(promise::resolve) ?: promise.resolve(null)
}
}
Expand Down Expand Up @@ -754,7 +754,7 @@ class GoogleMapsViewImpl(
kmlLayersById[id] = layer
layer.addLayerToMap()
} catch (_: Exception) {
mapsLog("kml layer parse failed: id=$id")
mapErrorHandler.report(RNMapErrorCode.KML_LAYER_FAILED, "kml layer parse failed: id=$id")
}
}

Expand Down
24 changes: 24 additions & 0 deletions android/src/main/java/com/rngooglemapsplus/MapErrorHandler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.rngooglemapsplus

private const val MAPS_LOG_TAG = "react-native-google-maps-plus"

class MapErrorHandler {
@Volatile
var callback: ((RNMapErrorCode, String) -> Unit)? = null

fun report(
code: RNMapErrorCode,
msg: String,
t: Throwable? = null,
) {
if (t != null) {
android.util.Log.w(MAPS_LOG_TAG, msg, t)
} else {
android.util.Log.w(MAPS_LOG_TAG, msg)
}

onUi {
callback?.invoke(code, msg)
}
}
}
13 changes: 0 additions & 13 deletions android/src/main/java/com/rngooglemapsplus/MapHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,3 @@ inline fun <T> onUiSync(crossinline block: () -> T): T {
}
return runBlocking { result.await() }
}

private const val MAPS_LOG_TAG = "react-native-google-maps-plus"

fun mapsLog(msg: String) {
android.util.Log.w(MAPS_LOG_TAG, msg)
}

fun mapsLog(
msg: String,
t: Throwable,
) {
android.util.Log.w(MAPS_LOG_TAG, msg, t)
}
19 changes: 10 additions & 9 deletions android/src/main/java/com/rngooglemapsplus/MapMarkerBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import kotlin.coroutines.cancellation.CancellationException

class MapMarkerBuilder(
val context: ThemedReactContext,
private val mapErrorHandler: MapErrorHandler,
private val scope: CoroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Default),
) {
private val iconCache =
Expand Down Expand Up @@ -121,7 +122,7 @@ class MapMarkerBuilder(
}
}
}.onFailure {
mapsLog("external svg resolve failed")
mapErrorHandler.report(RNMapErrorCode.MARKER_ICON_BUILD_FAILED, "external svg resolve failed")
}.getOrNull()
}

Expand All @@ -145,7 +146,7 @@ class MapMarkerBuilder(
try {
return Typeface.createFromAsset(assetManager, path)
} catch (_: Throwable) {
mapsLog("font resolve failed: $path")
mapErrorHandler.report(RNMapErrorCode.INVALID_ARGUMENT, "font resolve failed: $path")
}
}

Expand Down Expand Up @@ -292,14 +293,14 @@ class MapMarkerBuilder(
onReady(desc)
}
} catch (_: OutOfMemoryError) {
mapsLog("markerId=${m.id} buildIconAsync out of memory")
mapErrorHandler.report(RNMapErrorCode.MARKER_ICON_BUILD_FAILED, "markerId=${m.id} buildIconAsync out of memory")
clearIconCache()
withContext(Dispatchers.Main) {
ensureActive()
onReady(createFallbackDescriptor())
}
} catch (_: Throwable) {
mapsLog("markerId=${m.id} buildIconAsync failed")
mapErrorHandler.report(RNMapErrorCode.MARKER_ICON_BUILD_FAILED, "markerId=${m.id} buildIconAsync failed")
withContext(Dispatchers.Main) {
ensureActive()
onReady(createFallbackDescriptor())
Expand Down Expand Up @@ -343,7 +344,7 @@ class MapMarkerBuilder(
.toInt()

if (wPx <= 0 || hPx <= 0) {
mapsLog("markerId=${markerTag.id} invalid svg size")
mapErrorHandler.report(RNMapErrorCode.INVALID_ARGUMENT, "markerId=${markerTag.id} invalid svg size")
return ImageView(context)
}

Expand All @@ -365,7 +366,7 @@ class MapMarkerBuilder(
val drawable = PictureDrawable(svg.renderToPicture())
svgView.setImageDrawable(drawable)
} catch (_: Exception) {
mapsLog("markerId=${markerTag.id} infoWindow: svg render failed")
mapErrorHandler.report(RNMapErrorCode.MARKER_ICON_BUILD_FAILED, "markerId=${markerTag.id} infoWindow: svg render failed")
return ImageView(context)
}

Expand Down Expand Up @@ -403,7 +404,7 @@ class MapMarkerBuilder(
.toInt()

if (wPx <= 0 || hPx <= 0) {
mapsLog("markerId=$markerId invalid svg size")
mapErrorHandler.report(RNMapErrorCode.INVALID_ARGUMENT, "markerId=$markerId invalid svg size")
return RenderBitmapResult(createFallbackBitmap(), true)
}

Expand All @@ -416,10 +417,10 @@ class MapMarkerBuilder(
documentHeight = hPx.toFloat()
}
} catch (_: SVGParseException) {
mapsLog("markerId=$markerId icon: svg parse failed")
mapErrorHandler.report(RNMapErrorCode.INVALID_ARGUMENT, "markerId=$markerId icon: svg parse failed")
return RenderBitmapResult(createFallbackBitmap(), true)
} catch (_: IllegalArgumentException) {
mapsLog("markerId=$markerId icon: svg invalid")
mapErrorHandler.report(RNMapErrorCode.INVALID_ARGUMENT, "markerId=$markerId icon: svg invalid")
return RenderBitmapResult(createFallbackBitmap(), true)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import com.google.android.gms.maps.model.TileOverlayOptions
import com.google.android.gms.maps.model.UrlTileProvider
import java.net.URL

class MapUrlTileOverlayBuilder {
class MapUrlTileOverlayBuilder(
private val mapErrorHandler: MapErrorHandler,
) {
fun build(t: RNUrlTileOverlay): TileOverlayOptions {
val provider =
object : UrlTileProvider(
Expand All @@ -25,7 +27,7 @@ class MapUrlTileOverlayBuilder {
return try {
URL(url)
} catch (e: Exception) {
mapsLog("tile url invalid: $url", e)
mapErrorHandler.report(RNMapErrorCode.TILE_OVERLAY_FAILED, "tile url invalid: $url", e)
null
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,22 @@ import com.rngooglemapsplus.extensions.toSize
class RNGoogleMapsPlusView(
val context: ThemedReactContext,
) : HybridRNGoogleMapsPlusViewSpec() {
private val mapErrorHandler = MapErrorHandler()

private var currentCustomMapStyle: String? = null
private var permissionHandler = PermissionHandler(context)
private var locationHandler = LocationHandler(context)
private var playServiceHandler = PlayServicesHandler(context)

private val markerBuilder = MapMarkerBuilder(context)
private val markerBuilder = MapMarkerBuilder(context, mapErrorHandler)
private val polylineBuilder = MapPolylineBuilder()
private val polygonBuilder = MapPolygonBuilder()
private val circleBuilder = MapCircleBuilder()
private val heatmapBuilder = MapHeatmapBuilder()
private val urlTileOverlayBuilder = MapUrlTileOverlayBuilder()
private val urlTileOverlayBuilder = MapUrlTileOverlayBuilder(mapErrorHandler)

override val view =
GoogleMapsViewImpl(context, locationHandler, playServiceHandler, markerBuilder)
GoogleMapsViewImpl(context, locationHandler, playServiceHandler, markerBuilder, mapErrorHandler)

override var initialProps: RNInitialProps? = null
set(value) {
Expand Down Expand Up @@ -303,9 +305,10 @@ class RNGoogleMapsPlusView(
view.locationConfig = value
}

override var onMapError: ((RNMapErrorCode) -> Unit)? = null
override var onMapError: ((RNMapErrorCode, String) -> Unit)? = null
set(cb) {
view.onMapError = cb
field = cb
mapErrorHandler.callback = cb
}

override var onMapReady: ((Boolean) -> Unit)? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import android.graphics.Bitmap
import android.util.Base64
import android.util.Size
import androidx.core.graphics.scale
import com.rngooglemapsplus.mapsLog
import com.rngooglemapsplus.MapErrorHandler
import com.rngooglemapsplus.RNMapErrorCode
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.FileOutputStream
Expand All @@ -17,6 +18,7 @@ fun Bitmap.encode(
compressFormat: Bitmap.CompressFormat,
quality: Double,
asFile: Boolean,
mapErrorHandler: MapErrorHandler,
): String? =
try {
targetSize?.let { scale(it.width, it.height) }
Expand All @@ -32,6 +34,6 @@ fun Bitmap.encode(
"data:image/$format;base64," + Base64.encodeToString(bytes, Base64.NO_WRAP)
}
} catch (e: Exception) {
mapsLog("snapshot export failed", e)
mapErrorHandler.report(RNMapErrorCode.SNAPSHOT_EXPORT_FAILED, "snapshot export failed", e)
null
}
6 changes: 5 additions & 1 deletion example/src/hooks/useMapCallbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ export function useMapCallbacks(

const onMapError = useNitroCallback(
props.onMapError,
useCallback((e: RNMapErrorCode) => console.log('Map error:', e), [])
useCallback(
(e: RNMapErrorCode, message: string) =>
console.log('Map error:', RNMapErrorCode[e], message),
[]
)
);

const onMapReady = useNitroCallback(
Expand Down
6 changes: 5 additions & 1 deletion ios/GoogleMapViewImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import UIKit
final class GoogleMapsViewImpl: UIView, GMSMapViewDelegate,
GMSIndoorDisplayDelegate {

private let mapErrorHandler: MapErrorHandler
private let locationHandler: LocationHandler
private let markerBuilder: MapMarkerBuilder
private var mapView: GMSMapView?
Expand Down Expand Up @@ -35,9 +36,11 @@ GMSIndoorDisplayDelegate {

init(
frame: CGRect = .zero,
mapErrorHandler: MapErrorHandler,
locationHandler: LocationHandler,
markerBuilder: MapMarkerBuilder
) {
self.mapErrorHandler = mapErrorHandler
self.locationHandler = locationHandler
self.markerBuilder = markerBuilder
super.init(frame: frame)
Expand Down Expand Up @@ -468,7 +471,8 @@ GMSIndoorDisplayDelegate {
format: format,
imageFormat: imageFormat,
quality: quality,
resultIsFile: resultIsFile
resultIsFile: resultIsFile,
mapErrorHandler: self.mapErrorHandler
) {
promise.resolve(withResult: result)
} else {
Expand Down
21 changes: 21 additions & 0 deletions ios/MapErrorHandler.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Foundation

private let mapsLogTag = "react-native-google-maps-plus"

final class MapErrorHandler {

var callback: ((RNMapErrorCode, String) -> Void)?

func report(_ code: RNMapErrorCode, _ msg: String, _ error: Error? = nil) {
if let error {
NSLog("[%@] %@ | %@", mapsLogTag, msg, String(describing: error))
} else {
NSLog("[%@] %@", mapsLogTag, msg)
}

onMain { [weak self] in
guard let self else { return }
self.callback?(code, msg)
}
}
}
14 changes: 0 additions & 14 deletions ios/MapHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,3 @@ func onMain(
}
}
}

@inline(__always)
func mapsLog(_ message: String) {
NSLog("[react-native-google-maps-plus] %@", message)
}

@inline(__always)
func mapsLog(_ message: String, _ error: Error) {
NSLog(
"[react-native-google-maps-plus] %@ | %@",
message,
String(describing: error)
)
}
Loading
Loading