diff --git a/google-maps/README.md b/google-maps/README.md index c1df0504e..ee50d58aa 100644 --- a/google-maps/README.md +++ b/google-maps/README.md @@ -278,6 +278,7 @@ export default MyMap; * [`removeMarkers(...)`](#removemarkers) * [`destroy()`](#destroy) * [`setCamera(...)`](#setcamera) +* [`getMapType()`](#getmaptype) * [`setMapType(...)`](#setmaptype) * [`enableIndoorMaps(...)`](#enableindoormaps) * [`enableTrafficLayer(...)`](#enabletrafficlayer) @@ -422,6 +423,19 @@ setCamera(config: CameraConfig) => Promise -------------------- +### getMapType() + +```typescript +getMapType() => Promise +``` + +Get current map type + +**Returns:** Promise<MapType> + +-------------------- + + ### setMapType(...) ```typescript diff --git a/google-maps/android/src/main/java/com/capacitorjs/plugins/googlemaps/CapacitorGoogleMap.kt b/google-maps/android/src/main/java/com/capacitorjs/plugins/googlemaps/CapacitorGoogleMap.kt index 47ab61aab..f70f7659b 100644 --- a/google-maps/android/src/main/java/com/capacitorjs/plugins/googlemaps/CapacitorGoogleMap.kt +++ b/google-maps/android/src/main/java/com/capacitorjs/plugins/googlemaps/CapacitorGoogleMap.kt @@ -416,6 +416,27 @@ class CapacitorGoogleMap( } } + fun getMapType(callback: (type: String, error: GoogleMapsError?) -> Unit) { + try { + googleMap ?: throw GoogleMapNotAvailable() + CoroutineScope(Dispatchers.Main).launch { + val mapType: String = when (googleMap?.mapType) { + MAP_TYPE_NORMAL -> "Normal" + MAP_TYPE_HYBRID -> "Hybrid" + MAP_TYPE_SATELLITE -> "Satellite" + MAP_TYPE_TERRAIN -> "Terrain" + MAP_TYPE_NONE -> "None" + else -> { + "Normal" + } + } + callback(mapType, null); + } + } catch (e: GoogleMapsError) { + callback("", e) + } + } + fun setMapType(mapType: String, callback: (error: GoogleMapsError?) -> Unit) { try { googleMap ?: throw GoogleMapNotAvailable() diff --git a/google-maps/android/src/main/java/com/capacitorjs/plugins/googlemaps/CapacitorGoogleMapsPlugin.kt b/google-maps/android/src/main/java/com/capacitorjs/plugins/googlemaps/CapacitorGoogleMapsPlugin.kt index 1222c464e..eb54a5c1c 100644 --- a/google-maps/android/src/main/java/com/capacitorjs/plugins/googlemaps/CapacitorGoogleMapsPlugin.kt +++ b/google-maps/android/src/main/java/com/capacitorjs/plugins/googlemaps/CapacitorGoogleMapsPlugin.kt @@ -375,6 +375,31 @@ class CapacitorGoogleMapsPlugin : Plugin() { } } + @PluginMethod + fun getMapType(call: PluginCall) { + try { + val id = call.getString("id") + id ?: throw InvalidMapIdError() + + val map = maps[id] + map ?: throw MapNotFoundError() + + map.getMapType() { type, err -> + + if (err != null) { + throw err + } + val data = JSObject() + data.put("type", type) + call.resolve(data) + } + } catch (e: GoogleMapsError) { + handleError(call, e) + } catch (e: Exception) { + handleError(call, e) + } + } + @PluginMethod fun setMapType(call: PluginCall) { try { diff --git a/google-maps/ios/Plugin/CapacitorGoogleMapsPlugin.m b/google-maps/ios/Plugin/CapacitorGoogleMapsPlugin.m index 4ee4eb467..fb11a45f6 100644 --- a/google-maps/ios/Plugin/CapacitorGoogleMapsPlugin.m +++ b/google-maps/ios/Plugin/CapacitorGoogleMapsPlugin.m @@ -13,6 +13,7 @@ CAP_PLUGIN_METHOD(disableClustering, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(destroy, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(setCamera, CAPPluginReturnPromise); + CAP_PLUGIN_METHOD(getMapType, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(setMapType, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(enableIndoorMaps, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(enableTrafficLayer, CAPPluginReturnPromise); diff --git a/google-maps/ios/Plugin/CapacitorGoogleMapsPlugin.swift b/google-maps/ios/Plugin/CapacitorGoogleMapsPlugin.swift index d51c5bb44..cb537cae1 100644 --- a/google-maps/ios/Plugin/CapacitorGoogleMapsPlugin.swift +++ b/google-maps/ios/Plugin/CapacitorGoogleMapsPlugin.swift @@ -21,6 +21,22 @@ extension GMSMapViewType { return .normal } } + static func toString(mapType: GMSMapViewType) -> String { + switch mapType { + case .normal: + return "Normal" + case .hybrid: + return "Hybrid" + case .satellite: + return "Satellite" + case .terrain: + return "Terrain" + case .none: + return "None" + default: + return "Normal" + } + } } extension CGRect { @@ -275,6 +291,26 @@ public class CapacitorGoogleMapsPlugin: CAPPlugin, GMSMapViewDelegate { } } + @objc func getMapType(_ call: CAPPluginCall) { + do { + guard let id = call.getString("id") else { + throw GoogleMapErrors.invalidMapId + } + + guard let map = self.maps[id] else { + throw GoogleMapErrors.mapNotFound + } + + let mapType = GMSMapViewType.toString(mapType: map.getMapType()) + + call.resolve([ + "type": mapType + ]) + } catch { + handleError(call, error: error) + } + } + @objc func setMapType(_ call: CAPPluginCall) { do { guard let id = call.getString("id") else { diff --git a/google-maps/ios/Plugin/Map.swift b/google-maps/ios/Plugin/Map.swift index 717365b9e..dd94872c6 100644 --- a/google-maps/ios/Plugin/Map.swift +++ b/google-maps/ios/Plugin/Map.swift @@ -303,6 +303,10 @@ public class Map { } + func getMapType() -> GMSMapViewType { + return self.mapViewController.GMapView.mapType + } + func setMapType(mapType: GMSMapViewType) throws { DispatchQueue.main.sync { self.mapViewController.GMapView.mapType = mapType diff --git a/google-maps/src/implementation.ts b/google-maps/src/implementation.ts index 186e52669..469535acf 100644 --- a/google-maps/src/implementation.ts +++ b/google-maps/src/implementation.ts @@ -126,6 +126,7 @@ export interface CapacitorGoogleMapsPlugin extends Plugin { disableClustering(args: { id: string }): Promise; destroy(args: DestroyMapArgs): Promise; setCamera(args: CameraArgs): Promise; + getMapType(args: { id: string }): Promise<{ type: string }>; setMapType(args: MapTypeArgs): Promise; enableIndoorMaps(args: IndoorMapArgs): Promise; enableTrafficLayer(args: TrafficLayerArgs): Promise; diff --git a/google-maps/src/map.ts b/google-maps/src/map.ts index 366445adb..e22571da8 100644 --- a/google-maps/src/map.ts +++ b/google-maps/src/map.ts @@ -5,7 +5,6 @@ import type { CameraConfig, Marker, MapPadding, - MapType, MapListenerCallback, MapReadyCallbackData, CameraIdleCallbackData, @@ -15,7 +14,7 @@ import type { MarkerClickCallbackData, MyLocationButtonClickCallbackData, } from './definitions'; -import { LatLngBounds } from './definitions'; +import { LatLngBounds, MapType } from './definitions'; import type { CreateMapArgs } from './implementation'; import { CapacitorGoogleMaps } from './implementation'; @@ -37,6 +36,10 @@ export interface GoogleMapInterface { removeMarkers(ids: string[]): Promise; destroy(): Promise; setCamera(config: CameraConfig): Promise; + /** + * Get current map type + */ + getMapType(): Promise; setMapType(mapType: MapType): Promise; enableIndoorMaps(enabled: boolean): Promise; enableTrafficLayer(enabled: boolean): Promise; @@ -316,6 +319,11 @@ export class GoogleMap { }); } + async getMapType(): Promise { + const { type } = await CapacitorGoogleMaps.getMapType({ id: this.id }); + return MapType[type as keyof typeof MapType]; + } + /** * Sets the type of map tiles that should be displayed. * diff --git a/google-maps/src/web.ts b/google-maps/src/web.ts index faa12d7c7..fde51daf5 100644 --- a/google-maps/src/web.ts +++ b/google-maps/src/web.ts @@ -9,7 +9,7 @@ import { } from '@googlemaps/markerclusterer'; import type { Marker } from './definitions'; -import { LatLngBounds } from './definitions'; +import { MapType, LatLngBounds } from './definitions'; import type { AccElementsArgs, AddMarkerArgs, @@ -123,6 +123,17 @@ export class CapacitorGoogleMapsWeb }); } + async getMapType(_args: { id: string }): Promise<{ type: string }> { + let type = this.maps[_args.id].map.getMapTypeId(); + if (type !== undefined) { + if (type === 'roadmap') { + type = MapType.Normal; + } + return { type }; + } + throw new Error('Map type is undefined'); + } + async setMapType(_args: MapTypeArgs): Promise { this.maps[_args.id].map.setMapTypeId(_args.mapType); }