Skip to content

Commit a824323

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

File tree

14 files changed

+867
-957
lines changed

14 files changed

+867
-957
lines changed

README.md

Lines changed: 100 additions & 44 deletions
Large diffs are not rendered by default.

plugin/README.md

Lines changed: 100 additions & 44 deletions
Large diffs are not rendered by default.

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

Lines changed: 186 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -102,42 +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-
}
108-
109-
if (config.maxZoom != null) {
110-
googleMap?.setMaxZoomPreference(config.maxZoom!!.toFloat())
111-
}
112-
113-
if (config.minZoom != null) {
114-
googleMap?.setMinZoomPreference((config.minZoom!!.toFloat()))
115-
}
116-
117-
if (config.mapTypeId != null) {
118-
when (config.mapTypeId!!) {
119-
"hybrid" -> googleMap?.mapType = GoogleMap.MAP_TYPE_HYBRID
120-
"roadmap" -> googleMap?.mapType = GoogleMap.MAP_TYPE_NORMAL
121-
"satellite" -> googleMap?.mapType = GoogleMap.MAP_TYPE_SATELLITE
122-
"terrain" -> googleMap?.mapType = GoogleMap.MAP_TYPE_TERRAIN
123-
}
124-
}
125-
126-
if (config.restriction != null) {
127-
googleMap?.setLatLngBoundsForCameraTarget(config.restriction!!.latLngBounds)
128-
}
129-
130-
if (config.heading != null) {
131-
googleMap?.cameraPosition?.let { cameraPosition ->
132-
googleMap?.animateCamera(
133-
CameraUpdateFactory.newCameraPosition(
134-
CameraPosition.Builder(cameraPosition)
135-
.bearing(config.heading!!.toFloat())
136-
.build()
137-
)
138-
)
139-
}
140-
}
141105
}
142106
}
143107
}
@@ -186,6 +150,178 @@ class CapacitorGoogleMap(
186150
}
187151
}
188152

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

656-
fun getMapType(callback: (type: String, error: GoogleMapsError?) -> Unit) {
657-
try {
658-
googleMap ?: throw GoogleMapNotAvailable()
659-
CoroutineScope(Dispatchers.Main).launch {
660-
val mapType: String = when (googleMap?.mapType) {
661-
MAP_TYPE_NORMAL -> "Normal"
662-
MAP_TYPE_HYBRID -> "Hybrid"
663-
MAP_TYPE_SATELLITE -> "Satellite"
664-
MAP_TYPE_TERRAIN -> "Terrain"
665-
MAP_TYPE_NONE -> "None"
666-
else -> {
667-
"Normal"
668-
}
669-
}
670-
callback(mapType, null);
671-
}
672-
} catch (e: GoogleMapsError) {
673-
callback("", e)
674-
}
675-
}
676-
677-
fun setMapType(mapType: String, callback: (error: GoogleMapsError?) -> Unit) {
678-
try {
679-
googleMap ?: throw GoogleMapNotAvailable()
680-
CoroutineScope(Dispatchers.Main).launch {
681-
val mapTypeInt: Int =
682-
when (mapType) {
683-
"Normal" -> MAP_TYPE_NORMAL
684-
"Hybrid" -> MAP_TYPE_HYBRID
685-
"Satellite" -> MAP_TYPE_SATELLITE
686-
"Terrain" -> MAP_TYPE_TERRAIN
687-
"None" -> MAP_TYPE_NONE
688-
else -> {
689-
Log.w(
690-
"CapacitorGoogleMaps",
691-
"unknown mapView type '$mapType' Defaulting to normal."
692-
)
693-
MAP_TYPE_NORMAL
694-
}
695-
}
696-
697-
googleMap?.mapType = mapTypeInt
698-
callback(null)
699-
}
700-
} catch (e: GoogleMapsError) {
701-
callback(e)
702-
}
703-
}
704-
705-
fun enableIndoorMaps(enabled: Boolean, callback: (error: GoogleMapsError?) -> Unit) {
706-
try {
707-
googleMap ?: throw GoogleMapNotAvailable()
708-
CoroutineScope(Dispatchers.Main).launch {
709-
googleMap?.isIndoorEnabled = enabled
710-
callback(null)
711-
}
712-
} catch (e: GoogleMapsError) {
713-
callback(e)
714-
}
715-
}
716-
717-
fun enableTrafficLayer(enabled: Boolean, callback: (error: GoogleMapsError?) -> Unit) {
718-
try {
719-
googleMap ?: throw GoogleMapNotAvailable()
720-
CoroutineScope(Dispatchers.Main).launch {
721-
googleMap?.isTrafficEnabled = enabled
722-
callback(null)
723-
}
724-
} catch (e: GoogleMapsError) {
725-
callback(e)
726-
}
727-
}
728-
729-
@SuppressLint("MissingPermission")
730-
fun enableCurrentLocation(enabled: Boolean, callback: (error: GoogleMapsError?) -> Unit) {
731-
try {
732-
googleMap ?: throw GoogleMapNotAvailable()
733-
CoroutineScope(Dispatchers.Main).launch {
734-
googleMap?.isMyLocationEnabled = enabled
735-
callback(null)
736-
}
737-
} catch (e: GoogleMapsError) {
738-
callback(e)
739-
}
740-
}
741-
742-
fun setPadding(padding: GoogleMapPadding, callback: (error: GoogleMapsError?) -> Unit) {
743-
try {
744-
googleMap ?: throw GoogleMapNotAvailable()
745-
CoroutineScope(Dispatchers.Main).launch {
746-
googleMap?.setPadding(padding.left, padding.top, padding.right, padding.bottom)
747-
callback(null)
748-
}
749-
} catch (e: GoogleMapsError) {
750-
callback(e)
751-
}
752-
}
753-
754792
fun getMapBounds(): Rect {
755793
return Rect(
756794
getScaledPixels(delegate.bridge, config.x),
@@ -769,6 +807,20 @@ class CapacitorGoogleMap(
769807
googleMap?.animateCamera(cameraUpdate)
770808
}
771809

810+
private fun createLatLngBoundsFromGMSJS(boundsObject: JSObject): LatLngBounds {
811+
val southwestLatLng = LatLng(
812+
boundsObject.getDouble("south"),
813+
boundsObject.getDouble("west")
814+
)
815+
816+
val northeastLatLng = LatLng(
817+
boundsObject.getDouble("north"),
818+
boundsObject.getDouble("east")
819+
)
820+
821+
return LatLngBounds(southwestLatLng, northeastLatLng)
822+
}
823+
772824
private fun getScaledPixels(bridge: Bridge, pixels: Int): Int {
773825
// Get the screen's density scale
774826
val scale = bridge.activity.resources.displayMetrics.density

0 commit comments

Comments
 (0)