From db28521969ab54c033f1fafb335e1204253ac535 Mon Sep 17 00:00:00 2001 From: pinpong Date: Wed, 15 Oct 2025 10:58:01 +0700 Subject: [PATCH] refactor: add more extensions --- .../rngooglemapsplus/GoogleMapsViewImpl.kt | 83 ++++--------------- .../extensions/CameraPositionExtension.kt | 12 +++ .../extensions/IntExtension.kt | 28 +++++++ .../extensions/LatLngBounds.kt | 15 ++++ ios/GoogleMapViewImpl.swift | 75 +++-------------- ios/LocationHandler.swift | 12 +-- ios/extensions/CLError+Extension.swift | 14 ++++ .../GMSCameraPosition+Extension.swift | 12 +++ .../GMSCoordinateBounds+Extension.swift | 19 +++++ 9 files changed, 129 insertions(+), 141 deletions(-) create mode 100644 android/src/main/java/com/rngooglemapsplus/extensions/CameraPositionExtension.kt create mode 100644 android/src/main/java/com/rngooglemapsplus/extensions/IntExtension.kt create mode 100644 android/src/main/java/com/rngooglemapsplus/extensions/LatLngBounds.kt create mode 100644 ios/extensions/CLError+Extension.swift create mode 100644 ios/extensions/GMSCameraPosition+Extension.swift create mode 100644 ios/extensions/GMSCoordinateBounds+Extension.swift diff --git a/android/src/main/java/com/rngooglemapsplus/GoogleMapsViewImpl.kt b/android/src/main/java/com/rngooglemapsplus/GoogleMapsViewImpl.kt index 4f6006a..21790d5 100644 --- a/android/src/main/java/com/rngooglemapsplus/GoogleMapsViewImpl.kt +++ b/android/src/main/java/com/rngooglemapsplus/GoogleMapsViewImpl.kt @@ -10,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 @@ -38,8 +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 @@ -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 = @@ -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, ) } @@ -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, ) } @@ -220,21 +183,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 - - 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, ) } diff --git a/android/src/main/java/com/rngooglemapsplus/extensions/CameraPositionExtension.kt b/android/src/main/java/com/rngooglemapsplus/extensions/CameraPositionExtension.kt new file mode 100644 index 0000000..529b80d --- /dev/null +++ b/android/src/main/java/com/rngooglemapsplus/extensions/CameraPositionExtension.kt @@ -0,0 +1,12 @@ +package com.rngooglemapsplus.extensions + +import com.google.android.gms.maps.model.CameraPosition +import com.rngooglemapsplus.RNCamera + +fun CameraPosition.toRnCamera(): RNCamera = + RNCamera( + center = target.toRnLatLng(), + zoom = zoom.toDouble(), + bearing = bearing.toDouble(), + tilt = tilt.toDouble(), + ) diff --git a/android/src/main/java/com/rngooglemapsplus/extensions/IntExtension.kt b/android/src/main/java/com/rngooglemapsplus/extensions/IntExtension.kt new file mode 100644 index 0000000..edff5d8 --- /dev/null +++ b/android/src/main/java/com/rngooglemapsplus/extensions/IntExtension.kt @@ -0,0 +1,28 @@ +package com.rngooglemapsplus.extensions + +import com.google.android.gms.common.ConnectionResult +import com.rngooglemapsplus.RNMapErrorCode + +fun Int.toRNMapErrorCodeOrNull(): RNMapErrorCode? = + when (this) { + ConnectionResult.SERVICE_MISSING -> + RNMapErrorCode.PLAY_SERVICES_MISSING + + ConnectionResult.SERVICE_INVALID -> + RNMapErrorCode.PLAY_SERVICES_INVALID + + ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED -> + RNMapErrorCode.PLAY_SERVICES_OUTDATED + + ConnectionResult.SERVICE_UPDATING -> + RNMapErrorCode.PLAY_SERVICE_UPDATING + + ConnectionResult.SERVICE_DISABLED -> + RNMapErrorCode.PLAY_SERVICES_DISABLED + + ConnectionResult.SUCCESS -> + null + + else -> + RNMapErrorCode.UNKNOWN + } diff --git a/android/src/main/java/com/rngooglemapsplus/extensions/LatLngBounds.kt b/android/src/main/java/com/rngooglemapsplus/extensions/LatLngBounds.kt new file mode 100644 index 0000000..a178c79 --- /dev/null +++ b/android/src/main/java/com/rngooglemapsplus/extensions/LatLngBounds.kt @@ -0,0 +1,15 @@ +package com.rngooglemapsplus.extensions + +import com.google.android.gms.maps.model.LatLngBounds +import com.rngooglemapsplus.RNRegion + +fun LatLngBounds.toRnRegion(): RNRegion { + val latDelta = northeast.latitude - southwest.latitude + val lngDelta = northeast.longitude - southwest.longitude + + return RNRegion( + center = center.toRnLatLng(), + latitudeDelta = latDelta, + longitudeDelta = lngDelta, + ) +} diff --git a/ios/GoogleMapViewImpl.swift b/ios/GoogleMapViewImpl.swift index 67f3df4..1bb8d07 100644 --- a/ios/GoogleMapViewImpl.swift +++ b/ios/GoogleMapViewImpl.swift @@ -657,33 +657,14 @@ GMSIndoorDisplayDelegate { func mapView(_ mapView: GMSMapView, willMove gesture: Bool) { onMain { + self.cameraMoveReasonIsGesture = gesture let visibleRegion = mapView.projection.visibleRegion() let bounds = GMSCoordinateBounds(region: visibleRegion) - let center = CLLocationCoordinate2D( - latitude: (bounds.northEast.latitude + bounds.southWest.latitude) / 2.0, - longitude: (bounds.northEast.longitude + bounds.southWest.longitude) - / 2.0 - ) - - let latDelta = bounds.northEast.latitude - bounds.southWest.latitude - let lngDelta = bounds.northEast.longitude - bounds.southWest.longitude - - let cp = mapView.camera - let region = RNRegion( - center: center.toRNLatLng(), - latitudeDelta: latDelta, - longitudeDelta: lngDelta - ) - let cam = RNCamera( - center: cp.target.toRNLatLng(), - zoom: Double(cp.zoom), - bearing: cp.bearing, - tilt: cp.viewingAngle - ) - self.cameraMoveReasonIsGesture = gesture + let region = bounds.toRNRegion() + let camera = mapView.camera.toRNCamera() - self.onCameraChangeStart?(region, cam, gesture) + self.onCameraChange?(region, camera, gesture) } } @@ -702,28 +683,10 @@ GMSIndoorDisplayDelegate { let visibleRegion = mapView.projection.visibleRegion() let bounds = GMSCoordinateBounds(region: visibleRegion) - let center = CLLocationCoordinate2D( - latitude: (bounds.northEast.latitude + bounds.southWest.latitude) / 2.0, - longitude: (bounds.northEast.longitude + bounds.southWest.longitude) - / 2.0 - ) + let region = bounds.toRNRegion() + let camera = mapView.camera.toRNCamera() - let latDelta = bounds.northEast.latitude - bounds.southWest.latitude - let lngDelta = bounds.northEast.longitude - bounds.southWest.longitude - - let cp = mapView.camera - let region = RNRegion( - center: center.toRNLatLng(), - latitudeDelta: latDelta, - longitudeDelta: lngDelta - ) - let cam = RNCamera( - center: cp.target.toRNLatLng(), - zoom: Double(cp.zoom), - bearing: cp.bearing, - tilt: cp.viewingAngle - ) - self.onCameraChange?(region, cam, self.cameraMoveReasonIsGesture) + self.onCameraChange?(region, camera, self.cameraMoveReasonIsGesture) } } @@ -732,28 +695,10 @@ GMSIndoorDisplayDelegate { let visibleRegion = mapView.projection.visibleRegion() let bounds = GMSCoordinateBounds(region: visibleRegion) - let center = CLLocationCoordinate2D( - latitude: (bounds.northEast.latitude + bounds.southWest.latitude) / 2.0, - longitude: (bounds.northEast.longitude + bounds.southWest.longitude) - / 2.0 - ) + let region = bounds.toRNRegion() + let camera = mapView.camera.toRNCamera() - let latDelta = bounds.northEast.latitude - bounds.southWest.latitude - let lngDelta = bounds.northEast.longitude - bounds.southWest.longitude - - let cp = mapView.camera - let region = RNRegion( - center: center.toRNLatLng(), - latitudeDelta: latDelta, - longitudeDelta: lngDelta - ) - let cam = RNCamera( - center: cp.target.toRNLatLng(), - zoom: Double(cp.zoom), - bearing: cp.bearing, - tilt: cp.viewingAngle - ) - self.onCameraChangeComplete?(region, cam, self.cameraMoveReasonIsGesture) + self.onCameraChange?(region, camera, self.cameraMoveReasonIsGesture) } } diff --git a/ios/LocationHandler.swift b/ios/LocationHandler.swift index 3c38f10..a08501d 100644 --- a/ios/LocationHandler.swift +++ b/ios/LocationHandler.swift @@ -114,18 +114,10 @@ final class LocationHandler: NSObject, CLLocationManagerDelegate { didFailWithError error: Error ) { let code: RNLocationErrorCode - if let clError = error as? CLError { - switch clError.code { - case .denied: - code = RNLocationErrorCode.permissionDenied - case .locationUnknown, .network: - code = RNLocationErrorCode.positionUnavailable - default: - code = RNLocationErrorCode.internalError - } + code = clError.code.toRNLocationErrorCode } else { - code = RNLocationErrorCode.internalError + code = .internalError } onError?(code) } diff --git a/ios/extensions/CLError+Extension.swift b/ios/extensions/CLError+Extension.swift new file mode 100644 index 0000000..5cda693 --- /dev/null +++ b/ios/extensions/CLError+Extension.swift @@ -0,0 +1,14 @@ +import CoreLocation + +extension CLError.Code { + var toRNLocationErrorCode: RNLocationErrorCode { + switch self { + case .denied: + return .permissionDenied + case .locationUnknown, .network: + return .positionUnavailable + default: + return .internalError + } + } +} diff --git a/ios/extensions/GMSCameraPosition+Extension.swift b/ios/extensions/GMSCameraPosition+Extension.swift new file mode 100644 index 0000000..016e5af --- /dev/null +++ b/ios/extensions/GMSCameraPosition+Extension.swift @@ -0,0 +1,12 @@ +import GoogleMaps + +extension GMSCameraPosition { + func toRNCamera() -> RNCamera { + return RNCamera( + center: target.toRNLatLng(), + zoom: Double(zoom), + bearing: bearing, + tilt: viewingAngle + ) + } +} diff --git a/ios/extensions/GMSCoordinateBounds+Extension.swift b/ios/extensions/GMSCoordinateBounds+Extension.swift new file mode 100644 index 0000000..8676b88 --- /dev/null +++ b/ios/extensions/GMSCoordinateBounds+Extension.swift @@ -0,0 +1,19 @@ +import GoogleMaps + +extension GMSCoordinateBounds { + func toRNRegion() -> RNRegion { + let center = CLLocationCoordinate2D( + latitude: (northEast.latitude + southWest.latitude) / 2.0, + longitude: (northEast.longitude + southWest.longitude) / 2.0 + ) + + let latDelta = northEast.latitude - southWest.latitude + let lngDelta = northEast.longitude - southWest.longitude + + return RNRegion( + center: center.toRNLatLng(), + latitudeDelta: latDelta, + longitudeDelta: lngDelta + ) + } +}