Skip to content

Commit a0fd3eb

Browse files
committed
feat(google-maps): Refactored options and added feature to update the map options
1 parent ac36a4f commit a0fd3eb

File tree

13 files changed

+646
-694
lines changed

13 files changed

+646
-694
lines changed

plugin/android/src/main/java/com/capacitorjs/plugins/googlemaps/CapacitorGoogleMap.kt

Lines changed: 174 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,6 @@ class CapacitorGoogleMap(
102102

103103
bridge.webView.bringToFront()
104104
bridge.webView.setBackgroundColor(Color.TRANSPARENT)
105-
if (config.styles != null) {
106-
googleMap?.setMapStyle(MapStyleOptions(config.styles!!))
107-
}
108105
}
109106
}
110107
}
@@ -153,6 +150,166 @@ class CapacitorGoogleMap(
153150
}
154151
}
155152

153+
fun applyConfig(configObject: JSObject, callback: (error: GoogleMapsError?) -> Unit) {
154+
try {
155+
googleMap ?: throw GoogleMapNotAvailable()
156+
157+
CoroutineScope(Dispatchers.Main).launch {
158+
if (configObject.has("gestureHandling")) {
159+
googleMap?.uiSettings?.setAllGesturesEnabled(configObject.getString("gestureHandling") != "none")
160+
}
161+
162+
if (configObject.has("isCompassEnabled")) {
163+
googleMap?.uiSettings?.isCompassEnabled = configObject.getBool("isCompassEnabled") == true
164+
}
165+
166+
if (configObject.has("isIndoorMapsEnabled")) {
167+
googleMap?.isIndoorEnabled = configObject.getBool("isIndoorMapsEnabled") == true
168+
}
169+
170+
if (configObject.has("isMyLocationButtonEnabled")) {
171+
googleMap?.uiSettings?.isMyLocationButtonEnabled = configObject.getBool("isMyLocationButtonEnabled") == true
172+
}
173+
174+
if (configObject.has("isMyLocationEnabled")) {
175+
@SuppressLint("MissingPermission")
176+
googleMap?.isMyLocationEnabled = configObject.getBool("isMyLocationEnabled") == true
177+
}
178+
179+
if (configObject.has("isRotateGesturesEnabled")) {
180+
googleMap?.uiSettings?.isRotateGesturesEnabled = configObject.getBool("isRotateGesturesEnabled") == true
181+
}
182+
183+
if (configObject.has("isTiltGesturesEnabled")) {
184+
googleMap?.uiSettings?.isTiltGesturesEnabled = configObject.getBool("isTiltGesturesEnabled") == true
185+
}
186+
187+
if (configObject.has("isToolbarEnabled")) {
188+
googleMap?.uiSettings?.isMapToolbarEnabled = configObject.getBool("isToolbarEnabled") == true
189+
}
190+
191+
if (configObject.has("isTrafficLayerEnabled")) {
192+
googleMap?.isTrafficEnabled = configObject.getBool("isTrafficLayerEnabled") == true
193+
}
194+
195+
if (configObject.has("isZoomGesturesEnabled")) {
196+
googleMap?.uiSettings?.isZoomGesturesEnabled = configObject.getBool("isZoomGesturesEnabled") == true
197+
}
198+
199+
if (configObject.has("mapTypeId")) {
200+
setMapType(configObject.getString("mapTypeId"))
201+
}
202+
203+
if (configObject.has("maxZoom")) {
204+
setMaxZoom(configObject.getDouble("maxZoom").toFloat())
205+
}
206+
207+
if (configObject.has("minZoom")) {
208+
setMinZoom(configObject.getDouble("minZoom").toFloat())
209+
}
210+
211+
if (configObject.has("padding")) {
212+
setPadding(configObject.getJSObject("padding"))
213+
}
214+
215+
if (configObject.has("restriction")) {
216+
setRestriction(configObject.getJSObject("restriction"))
217+
}
218+
219+
if (configObject.has("styles")) {
220+
googleMap?.setMapStyle(configObject.getString("styles")
221+
?.let { MapStyleOptions(it) })
222+
}
223+
224+
callback(null)
225+
}
226+
} catch (e: GoogleMapsError) {
227+
callback(e)
228+
}
229+
}
230+
231+
private fun setMapType(mapTypeId: String?) {
232+
val mapTypeInt: Int =
233+
when (mapTypeId?.lowercase()) {
234+
"normal" -> MAP_TYPE_NORMAL
235+
"hybrid" -> MAP_TYPE_HYBRID
236+
"satellite" -> MAP_TYPE_SATELLITE
237+
"terrain" -> MAP_TYPE_TERRAIN
238+
"none" -> MAP_TYPE_NONE
239+
else -> {
240+
Log.w(
241+
"CapacitorGoogleMaps",
242+
"unknown mapView type '$mapTypeId' Defaulting to normal."
243+
)
244+
MAP_TYPE_NORMAL
245+
}
246+
}
247+
248+
googleMap?.mapType = mapTypeInt
249+
}
250+
251+
private fun setMaxZoom(maxZoom: Float) {
252+
var minZoom = googleMap?.minZoomLevel
253+
googleMap?.resetMinMaxZoomPreference()
254+
if (minZoom != null) {
255+
googleMap?.setMinZoomPreference(minZoom)
256+
}
257+
if (maxZoom != 0F) {
258+
googleMap?.setMinZoomPreference(maxZoom)
259+
}
260+
}
261+
262+
private fun setMinZoom(minZoom: Float) {
263+
var maxZoom = googleMap?.maxZoomLevel
264+
googleMap?.resetMinMaxZoomPreference()
265+
if (maxZoom != null) {
266+
googleMap?.setMaxZoomPreference(maxZoom)
267+
}
268+
if (minZoom != 0F) {
269+
googleMap?.setMinZoomPreference(minZoom)
270+
}
271+
}
272+
273+
private fun setPadding(paddingObj: JSObject?) {
274+
if (paddingObj == null) {
275+
googleMap?.setPadding(0, 0, 0, 0)
276+
} else {
277+
val padding = GoogleMapPadding(paddingObj)
278+
val left = getScaledPixels(delegate.bridge, padding.left)
279+
val top = getScaledPixels(delegate.bridge, padding.top)
280+
val right = getScaledPixels(delegate.bridge, padding.right)
281+
val bottom = getScaledPixels(delegate.bridge, padding.bottom)
282+
googleMap?.setPadding(left, top, right, bottom)
283+
}
284+
}
285+
286+
private fun setRestriction(restrictionObj: JSObject?) {
287+
var latLngBounds = restrictionObj?.getJSObject("latLngBounds")
288+
var bounds: LatLngBounds? = null
289+
290+
if (latLngBounds != null) {
291+
bounds = createLatLngBoundsFromGMSJS(latLngBounds)
292+
}
293+
294+
googleMap?.resetMinMaxZoomPreference()
295+
googleMap?.setLatLngBoundsForCameraTarget(null)
296+
297+
if (bounds != null) {
298+
googleMap?.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 0),
299+
object : CancelableCallback {
300+
override fun onFinish() {
301+
val zoom = googleMap?.cameraPosition?.zoom
302+
if (zoom != null) {
303+
googleMap?.setMinZoomPreference(zoom)
304+
}
305+
googleMap?.setLatLngBoundsForCameraTarget(bounds)
306+
}
307+
override fun onCancel() {}
308+
}
309+
)
310+
}
311+
}
312+
156313
fun destroy() {
157314
runBlocking {
158315
val job =
@@ -562,104 +719,6 @@ class CapacitorGoogleMap(
562719
}
563720
}
564721

565-
fun getMapType(callback: (type: String, error: GoogleMapsError?) -> Unit) {
566-
try {
567-
googleMap ?: throw GoogleMapNotAvailable()
568-
CoroutineScope(Dispatchers.Main).launch {
569-
val mapType: String = when (googleMap?.mapType) {
570-
MAP_TYPE_NORMAL -> "Normal"
571-
MAP_TYPE_HYBRID -> "Hybrid"
572-
MAP_TYPE_SATELLITE -> "Satellite"
573-
MAP_TYPE_TERRAIN -> "Terrain"
574-
MAP_TYPE_NONE -> "None"
575-
else -> {
576-
"Normal"
577-
}
578-
}
579-
callback(mapType, null);
580-
}
581-
} catch (e: GoogleMapsError) {
582-
callback("", e)
583-
}
584-
}
585-
586-
fun setMapType(mapType: String, callback: (error: GoogleMapsError?) -> Unit) {
587-
try {
588-
googleMap ?: throw GoogleMapNotAvailable()
589-
CoroutineScope(Dispatchers.Main).launch {
590-
val mapTypeInt: Int =
591-
when (mapType) {
592-
"Normal" -> MAP_TYPE_NORMAL
593-
"Hybrid" -> MAP_TYPE_HYBRID
594-
"Satellite" -> MAP_TYPE_SATELLITE
595-
"Terrain" -> MAP_TYPE_TERRAIN
596-
"None" -> MAP_TYPE_NONE
597-
else -> {
598-
Log.w(
599-
"CapacitorGoogleMaps",
600-
"unknown mapView type '$mapType' Defaulting to normal."
601-
)
602-
MAP_TYPE_NORMAL
603-
}
604-
}
605-
606-
googleMap?.mapType = mapTypeInt
607-
callback(null)
608-
}
609-
} catch (e: GoogleMapsError) {
610-
callback(e)
611-
}
612-
}
613-
614-
fun enableIndoorMaps(enabled: Boolean, callback: (error: GoogleMapsError?) -> Unit) {
615-
try {
616-
googleMap ?: throw GoogleMapNotAvailable()
617-
CoroutineScope(Dispatchers.Main).launch {
618-
googleMap?.isIndoorEnabled = enabled
619-
callback(null)
620-
}
621-
} catch (e: GoogleMapsError) {
622-
callback(e)
623-
}
624-
}
625-
626-
fun enableTrafficLayer(enabled: Boolean, callback: (error: GoogleMapsError?) -> Unit) {
627-
try {
628-
googleMap ?: throw GoogleMapNotAvailable()
629-
CoroutineScope(Dispatchers.Main).launch {
630-
googleMap?.isTrafficEnabled = enabled
631-
callback(null)
632-
}
633-
} catch (e: GoogleMapsError) {
634-
callback(e)
635-
}
636-
}
637-
638-
@SuppressLint("MissingPermission")
639-
fun enableCurrentLocation(enabled: Boolean, callback: (error: GoogleMapsError?) -> Unit) {
640-
try {
641-
googleMap ?: throw GoogleMapNotAvailable()
642-
CoroutineScope(Dispatchers.Main).launch {
643-
googleMap?.isMyLocationEnabled = enabled
644-
callback(null)
645-
}
646-
} catch (e: GoogleMapsError) {
647-
callback(e)
648-
}
649-
}
650-
651-
fun setPadding(padding: GoogleMapPadding, callback: (error: GoogleMapsError?) -> Unit) {
652-
try {
653-
googleMap ?: throw GoogleMapNotAvailable()
654-
CoroutineScope(Dispatchers.Main).launch {
655-
googleMap?.setPadding(padding.left, padding.top, padding.right, padding.bottom)
656-
callback(null)
657-
}
658-
} catch (e: GoogleMapsError) {
659-
callback(e)
660-
}
661-
}
662-
663722
fun getMapBounds(): Rect {
664723
return Rect(
665724
getScaledPixels(delegate.bridge, config.x),
@@ -678,6 +737,20 @@ class CapacitorGoogleMap(
678737
googleMap?.animateCamera(cameraUpdate)
679738
}
680739

740+
private fun createLatLngBoundsFromGMSJS(boundsObject: JSObject): LatLngBounds {
741+
val southwestLatLng = LatLng(
742+
boundsObject.getDouble("south"),
743+
boundsObject.getDouble("west")
744+
)
745+
746+
val northeastLatLng = LatLng(
747+
boundsObject.getDouble("north"),
748+
boundsObject.getDouble("east")
749+
)
750+
751+
return LatLngBounds(southwestLatLng, northeastLatLng)
752+
}
753+
681754
private fun getScaledPixels(bridge: Bridge, pixels: Int): Int {
682755
// Get the screen's density scale
683756
val scale = bridge.activity.resources.displayMetrics.density

0 commit comments

Comments
 (0)