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
83 changes: 17 additions & 66 deletions android/src/main/java/com/rngooglemapsplus/GoogleMapsViewImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 Down Expand Up @@ -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,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,
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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(),
)
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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,
)
}
75 changes: 10 additions & 65 deletions ios/GoogleMapViewImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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)
}
}

Expand All @@ -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)
}
}

Expand Down
12 changes: 2 additions & 10 deletions ios/LocationHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
14 changes: 14 additions & 0 deletions ios/extensions/CLError+Extension.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
}
12 changes: 12 additions & 0 deletions ios/extensions/GMSCameraPosition+Extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import GoogleMaps

extension GMSCameraPosition {
func toRNCamera() -> RNCamera {
return RNCamera(
center: target.toRNLatLng(),
zoom: Double(zoom),
bearing: bearing,
tilt: viewingAngle
)
}
}
19 changes: 19 additions & 0 deletions ios/extensions/GMSCoordinateBounds+Extension.swift
Original file line number Diff line number Diff line change
@@ -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
)
}
}
Loading