Skip to content

Commit

Permalink
feat(google-maps): Add getMapType method (#1420)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored Feb 22, 2023
1 parent 1f2f618 commit c2fa96f
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 3 deletions.
14 changes: 14 additions & 0 deletions google-maps/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ export default MyMap;
* [`removeMarkers(...)`](#removemarkers)
* [`destroy()`](#destroy)
* [`setCamera(...)`](#setcamera)
* [`getMapType()`](#getmaptype)
* [`setMapType(...)`](#setmaptype)
* [`enableIndoorMaps(...)`](#enableindoormaps)
* [`enableTrafficLayer(...)`](#enabletrafficlayer)
Expand Down Expand Up @@ -422,6 +423,19 @@ setCamera(config: CameraConfig) => Promise<void>
--------------------


### getMapType()

```typescript
getMapType() => Promise<MapType>
```

Get current map type

**Returns:** <code>Promise&lt;<a href="#maptype">MapType</a>&gt;</code>

--------------------


### setMapType(...)

```typescript
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions google-maps/ios/Plugin/CapacitorGoogleMapsPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
36 changes: 36 additions & 0 deletions google-maps/ios/Plugin/CapacitorGoogleMapsPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions google-maps/ios/Plugin/Map.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions google-maps/src/implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export interface CapacitorGoogleMapsPlugin extends Plugin {
disableClustering(args: { id: string }): Promise<void>;
destroy(args: DestroyMapArgs): Promise<void>;
setCamera(args: CameraArgs): Promise<void>;
getMapType(args: { id: string }): Promise<{ type: string }>;
setMapType(args: MapTypeArgs): Promise<void>;
enableIndoorMaps(args: IndoorMapArgs): Promise<void>;
enableTrafficLayer(args: TrafficLayerArgs): Promise<void>;
Expand Down
12 changes: 10 additions & 2 deletions google-maps/src/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type {
CameraConfig,
Marker,
MapPadding,
MapType,
MapListenerCallback,
MapReadyCallbackData,
CameraIdleCallbackData,
Expand All @@ -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';

Expand All @@ -37,6 +36,10 @@ export interface GoogleMapInterface {
removeMarkers(ids: string[]): Promise<void>;
destroy(): Promise<void>;
setCamera(config: CameraConfig): Promise<void>;
/**
* Get current map type
*/
getMapType(): Promise<MapType>;
setMapType(mapType: MapType): Promise<void>;
enableIndoorMaps(enabled: boolean): Promise<void>;
enableTrafficLayer(enabled: boolean): Promise<void>;
Expand Down Expand Up @@ -316,6 +319,11 @@ export class GoogleMap {
});
}

async getMapType(): Promise<MapType> {
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.
*
Expand Down
13 changes: 12 additions & 1 deletion google-maps/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<void> {
this.maps[_args.id].map.setMapTypeId(_args.mapType);
}
Expand Down

0 comments on commit c2fa96f

Please sign in to comment.