Skip to content

Commit 6ce8c9d

Browse files
authored
refactor: add more extensions
2 parents d9b2416 + db28521 commit 6ce8c9d

File tree

9 files changed

+129
-141
lines changed

9 files changed

+129
-141
lines changed

android/src/main/java/com/rngooglemapsplus/GoogleMapsViewImpl.kt

Lines changed: 17 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import com.facebook.react.bridge.LifecycleEventListener
1010
import com.facebook.react.bridge.UiThreadUtil
1111
import com.facebook.react.uimanager.PixelUtil.dpToPx
1212
import com.facebook.react.uimanager.ThemedReactContext
13-
import com.google.android.gms.common.ConnectionResult
1413
import com.google.android.gms.maps.CameraUpdateFactory
1514
import com.google.android.gms.maps.GoogleMap
1615
import com.google.android.gms.maps.GoogleMapOptions
@@ -38,8 +37,11 @@ import com.rngooglemapsplus.extensions.toLatLng
3837
import com.rngooglemapsplus.extensions.toLocationErrorCode
3938
import com.rngooglemapsplus.extensions.toRNIndoorBuilding
4039
import com.rngooglemapsplus.extensions.toRNIndoorLevel
40+
import com.rngooglemapsplus.extensions.toRNMapErrorCodeOrNull
41+
import com.rngooglemapsplus.extensions.toRnCamera
4142
import com.rngooglemapsplus.extensions.toRnLatLng
4243
import com.rngooglemapsplus.extensions.toRnLocation
44+
import com.rngooglemapsplus.extensions.toRnRegion
4345
import java.io.ByteArrayInputStream
4446
import java.io.ByteArrayOutputStream
4547
import java.io.File
@@ -94,31 +96,16 @@ class GoogleMapsViewImpl(
9496
if (initialized) return
9597
initialized = true
9698
val result = playServiceHandler.playServicesAvailability()
99+
val errorCode = result.toRNMapErrorCodeOrNull()
97100

98-
when (result) {
99-
ConnectionResult.SERVICE_MISSING -> {
100-
onMapError?.invoke(RNMapErrorCode.PLAY_SERVICES_MISSING)
101-
return
102-
}
101+
if (errorCode != null) {
102+
onMapError?.invoke(errorCode)
103103

104-
ConnectionResult.SERVICE_INVALID -> {
105-
onMapError?.invoke(RNMapErrorCode.PLAY_SERVICES_INVALID)
104+
if (errorCode == RNMapErrorCode.PLAY_SERVICES_MISSING ||
105+
errorCode == RNMapErrorCode.PLAY_SERVICES_INVALID
106+
) {
106107
return
107108
}
108-
109-
ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED ->
110-
onMapError?.invoke(RNMapErrorCode.PLAY_SERVICES_OUTDATED)
111-
112-
ConnectionResult.SERVICE_UPDATING ->
113-
onMapError?.invoke(RNMapErrorCode.PLAY_SERVICE_UPDATING)
114-
115-
ConnectionResult.SERVICE_DISABLED ->
116-
onMapError?.invoke(RNMapErrorCode.PLAY_SERVICES_DISABLED)
117-
118-
ConnectionResult.SUCCESS -> {}
119-
120-
else ->
121-
onMapError?.invoke(RNMapErrorCode.UNKNOWN)
122109
}
123110

124111
mapView =
@@ -160,21 +147,9 @@ class GoogleMapsViewImpl(
160147
}
161148
val isGesture = GoogleMap.OnCameraMoveStartedListener.REASON_GESTURE == reason
162149

163-
val latDelta = bounds.northeast.latitude - bounds.southwest.latitude
164-
val lngDelta = bounds.northeast.longitude - bounds.southwest.longitude
165-
166150
onCameraChangeStart?.invoke(
167-
RNRegion(
168-
center = bounds.center.toRnLatLng(),
169-
latitudeDelta = latDelta,
170-
longitudeDelta = lngDelta,
171-
),
172-
RNCamera(
173-
center = cameraPosition.target.toRnLatLng(),
174-
zoom = cameraPosition.zoom.toDouble(),
175-
bearing = cameraPosition.bearing.toDouble(),
176-
tilt = cameraPosition.tilt.toDouble(),
177-
),
151+
bounds.toRnRegion(),
152+
cameraPosition.toRnCamera(),
178153
isGesture,
179154
)
180155
}
@@ -192,21 +167,9 @@ class GoogleMapsViewImpl(
192167

193168
val isGesture = GoogleMap.OnCameraMoveStartedListener.REASON_GESTURE == cameraMoveReason
194169

195-
val latDelta = bounds.northeast.latitude - bounds.southwest.latitude
196-
val lngDelta = bounds.northeast.longitude - bounds.southwest.longitude
197-
198-
onCameraChange?.invoke(
199-
RNRegion(
200-
center = bounds.center.toRnLatLng(),
201-
latitudeDelta = latDelta,
202-
longitudeDelta = lngDelta,
203-
),
204-
RNCamera(
205-
center = cameraPosition.target.toRnLatLng(),
206-
zoom = cameraPosition.zoom.toDouble(),
207-
bearing = cameraPosition.bearing.toDouble(),
208-
tilt = cameraPosition.tilt.toDouble(),
209-
),
170+
onCameraChangeStart?.invoke(
171+
bounds.toRnRegion(),
172+
cameraPosition.toRnCamera(),
210173
isGesture,
211174
)
212175
}
@@ -220,21 +183,9 @@ class GoogleMapsViewImpl(
220183
}
221184
val isGesture = GoogleMap.OnCameraMoveStartedListener.REASON_GESTURE == cameraMoveReason
222185

223-
val latDelta = bounds.northeast.latitude - bounds.southwest.latitude
224-
val lngDelta = bounds.northeast.longitude - bounds.southwest.longitude
225-
226-
onCameraChangeComplete?.invoke(
227-
RNRegion(
228-
center = bounds.center.toRnLatLng(),
229-
latitudeDelta = latDelta,
230-
longitudeDelta = lngDelta,
231-
),
232-
RNCamera(
233-
center = cameraPosition.target.toRnLatLng(),
234-
zoom = cameraPosition.zoom.toDouble(),
235-
bearing = cameraPosition.bearing.toDouble(),
236-
tilt = cameraPosition.tilt.toDouble(),
237-
),
186+
onCameraChangeStart?.invoke(
187+
bounds.toRnRegion(),
188+
cameraPosition.toRnCamera(),
238189
isGesture,
239190
)
240191
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.rngooglemapsplus.extensions
2+
3+
import com.google.android.gms.maps.model.CameraPosition
4+
import com.rngooglemapsplus.RNCamera
5+
6+
fun CameraPosition.toRnCamera(): RNCamera =
7+
RNCamera(
8+
center = target.toRnLatLng(),
9+
zoom = zoom.toDouble(),
10+
bearing = bearing.toDouble(),
11+
tilt = tilt.toDouble(),
12+
)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.rngooglemapsplus.extensions
2+
3+
import com.google.android.gms.common.ConnectionResult
4+
import com.rngooglemapsplus.RNMapErrorCode
5+
6+
fun Int.toRNMapErrorCodeOrNull(): RNMapErrorCode? =
7+
when (this) {
8+
ConnectionResult.SERVICE_MISSING ->
9+
RNMapErrorCode.PLAY_SERVICES_MISSING
10+
11+
ConnectionResult.SERVICE_INVALID ->
12+
RNMapErrorCode.PLAY_SERVICES_INVALID
13+
14+
ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED ->
15+
RNMapErrorCode.PLAY_SERVICES_OUTDATED
16+
17+
ConnectionResult.SERVICE_UPDATING ->
18+
RNMapErrorCode.PLAY_SERVICE_UPDATING
19+
20+
ConnectionResult.SERVICE_DISABLED ->
21+
RNMapErrorCode.PLAY_SERVICES_DISABLED
22+
23+
ConnectionResult.SUCCESS ->
24+
null
25+
26+
else ->
27+
RNMapErrorCode.UNKNOWN
28+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.rngooglemapsplus.extensions
2+
3+
import com.google.android.gms.maps.model.LatLngBounds
4+
import com.rngooglemapsplus.RNRegion
5+
6+
fun LatLngBounds.toRnRegion(): RNRegion {
7+
val latDelta = northeast.latitude - southwest.latitude
8+
val lngDelta = northeast.longitude - southwest.longitude
9+
10+
return RNRegion(
11+
center = center.toRnLatLng(),
12+
latitudeDelta = latDelta,
13+
longitudeDelta = lngDelta,
14+
)
15+
}

ios/GoogleMapViewImpl.swift

Lines changed: 10 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -657,33 +657,14 @@ GMSIndoorDisplayDelegate {
657657

658658
func mapView(_ mapView: GMSMapView, willMove gesture: Bool) {
659659
onMain {
660+
self.cameraMoveReasonIsGesture = gesture
660661
let visibleRegion = mapView.projection.visibleRegion()
661662
let bounds = GMSCoordinateBounds(region: visibleRegion)
662663

663-
let center = CLLocationCoordinate2D(
664-
latitude: (bounds.northEast.latitude + bounds.southWest.latitude) / 2.0,
665-
longitude: (bounds.northEast.longitude + bounds.southWest.longitude)
666-
/ 2.0
667-
)
668-
669-
let latDelta = bounds.northEast.latitude - bounds.southWest.latitude
670-
let lngDelta = bounds.northEast.longitude - bounds.southWest.longitude
671-
672-
let cp = mapView.camera
673-
let region = RNRegion(
674-
center: center.toRNLatLng(),
675-
latitudeDelta: latDelta,
676-
longitudeDelta: lngDelta
677-
)
678-
let cam = RNCamera(
679-
center: cp.target.toRNLatLng(),
680-
zoom: Double(cp.zoom),
681-
bearing: cp.bearing,
682-
tilt: cp.viewingAngle
683-
)
684-
self.cameraMoveReasonIsGesture = gesture
664+
let region = bounds.toRNRegion()
665+
let camera = mapView.camera.toRNCamera()
685666

686-
self.onCameraChangeStart?(region, cam, gesture)
667+
self.onCameraChange?(region, camera, gesture)
687668
}
688669
}
689670

@@ -702,28 +683,10 @@ GMSIndoorDisplayDelegate {
702683
let visibleRegion = mapView.projection.visibleRegion()
703684
let bounds = GMSCoordinateBounds(region: visibleRegion)
704685

705-
let center = CLLocationCoordinate2D(
706-
latitude: (bounds.northEast.latitude + bounds.southWest.latitude) / 2.0,
707-
longitude: (bounds.northEast.longitude + bounds.southWest.longitude)
708-
/ 2.0
709-
)
686+
let region = bounds.toRNRegion()
687+
let camera = mapView.camera.toRNCamera()
710688

711-
let latDelta = bounds.northEast.latitude - bounds.southWest.latitude
712-
let lngDelta = bounds.northEast.longitude - bounds.southWest.longitude
713-
714-
let cp = mapView.camera
715-
let region = RNRegion(
716-
center: center.toRNLatLng(),
717-
latitudeDelta: latDelta,
718-
longitudeDelta: lngDelta
719-
)
720-
let cam = RNCamera(
721-
center: cp.target.toRNLatLng(),
722-
zoom: Double(cp.zoom),
723-
bearing: cp.bearing,
724-
tilt: cp.viewingAngle
725-
)
726-
self.onCameraChange?(region, cam, self.cameraMoveReasonIsGesture)
689+
self.onCameraChange?(region, camera, self.cameraMoveReasonIsGesture)
727690
}
728691
}
729692

@@ -732,28 +695,10 @@ GMSIndoorDisplayDelegate {
732695
let visibleRegion = mapView.projection.visibleRegion()
733696
let bounds = GMSCoordinateBounds(region: visibleRegion)
734697

735-
let center = CLLocationCoordinate2D(
736-
latitude: (bounds.northEast.latitude + bounds.southWest.latitude) / 2.0,
737-
longitude: (bounds.northEast.longitude + bounds.southWest.longitude)
738-
/ 2.0
739-
)
698+
let region = bounds.toRNRegion()
699+
let camera = mapView.camera.toRNCamera()
740700

741-
let latDelta = bounds.northEast.latitude - bounds.southWest.latitude
742-
let lngDelta = bounds.northEast.longitude - bounds.southWest.longitude
743-
744-
let cp = mapView.camera
745-
let region = RNRegion(
746-
center: center.toRNLatLng(),
747-
latitudeDelta: latDelta,
748-
longitudeDelta: lngDelta
749-
)
750-
let cam = RNCamera(
751-
center: cp.target.toRNLatLng(),
752-
zoom: Double(cp.zoom),
753-
bearing: cp.bearing,
754-
tilt: cp.viewingAngle
755-
)
756-
self.onCameraChangeComplete?(region, cam, self.cameraMoveReasonIsGesture)
701+
self.onCameraChange?(region, camera, self.cameraMoveReasonIsGesture)
757702
}
758703
}
759704

ios/LocationHandler.swift

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,10 @@ final class LocationHandler: NSObject, CLLocationManagerDelegate {
114114
didFailWithError error: Error
115115
) {
116116
let code: RNLocationErrorCode
117-
118117
if let clError = error as? CLError {
119-
switch clError.code {
120-
case .denied:
121-
code = RNLocationErrorCode.permissionDenied
122-
case .locationUnknown, .network:
123-
code = RNLocationErrorCode.positionUnavailable
124-
default:
125-
code = RNLocationErrorCode.internalError
126-
}
118+
code = clError.code.toRNLocationErrorCode
127119
} else {
128-
code = RNLocationErrorCode.internalError
120+
code = .internalError
129121
}
130122
onError?(code)
131123
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import CoreLocation
2+
3+
extension CLError.Code {
4+
var toRNLocationErrorCode: RNLocationErrorCode {
5+
switch self {
6+
case .denied:
7+
return .permissionDenied
8+
case .locationUnknown, .network:
9+
return .positionUnavailable
10+
default:
11+
return .internalError
12+
}
13+
}
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import GoogleMaps
2+
3+
extension GMSCameraPosition {
4+
func toRNCamera() -> RNCamera {
5+
return RNCamera(
6+
center: target.toRNLatLng(),
7+
zoom: Double(zoom),
8+
bearing: bearing,
9+
tilt: viewingAngle
10+
)
11+
}
12+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import GoogleMaps
2+
3+
extension GMSCoordinateBounds {
4+
func toRNRegion() -> RNRegion {
5+
let center = CLLocationCoordinate2D(
6+
latitude: (northEast.latitude + southWest.latitude) / 2.0,
7+
longitude: (northEast.longitude + southWest.longitude) / 2.0
8+
)
9+
10+
let latDelta = northEast.latitude - southWest.latitude
11+
let lngDelta = northEast.longitude - southWest.longitude
12+
13+
return RNRegion(
14+
center: center.toRNLatLng(),
15+
latitudeDelta: latDelta,
16+
longitudeDelta: lngDelta
17+
)
18+
}
19+
}

0 commit comments

Comments
 (0)