Skip to content

Commit

Permalink
feat(google-maps): Separate mapId for Google Maps Cloud IDs (#1943)
Browse files Browse the repository at this point in the history
Co-authored-by: Joey Pender <[email protected]>
  • Loading branch information
jcesarmobile and theproducer authored Dec 8, 2023
1 parent 3b520b8 commit bf7ec33
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 4 deletions.
3 changes: 3 additions & 0 deletions google-maps/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,9 @@ For iOS and Android only the config options declared on <a href="#googlemapconfi
| **`androidLiteMode`** | <code>boolean</code> | Enables image-based lite mode on Android. | <code>false</code> | |
| **`devicePixelRatio`** | <code>number</code> | Override pixel ratio for native map. | | |
| **`styles`** | <code>MapTypeStyle[] \| null</code> | Styles to apply to each of the default map types. Note that for satellite, hybrid and terrain modes, these styles will only apply to labels and geometry. | | 4.3.0 |
| **`mapId`** | <code>string</code> | A map id associated with a specific map style or feature. [Use Map IDs](https://developers.google.com/maps/documentation/get-map-id) Only for Web. | | 5.4.0 |
| **`androidMapId`** | <code>string</code> | A map id associated with a specific map style or feature. [Use Map IDs](https://developers.google.com/maps/documentation/get-map-id) Only for Android. | | 5.4.0 |
| **`iOSMapId`** | <code>string</code> | A map id associated with a specific map style or feature. [Use Map IDs](https://developers.google.com/maps/documentation/get-map-id) Only for iOS. | | 5.4.0 |


#### LatLng
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.getcapacitor.annotation.CapacitorPlugin
import com.getcapacitor.annotation.Permission
import com.getcapacitor.annotation.PermissionCallback
import com.google.android.gms.maps.MapsInitializer
import com.google.android.gms.maps.OnMapsSdkInitializedCallback
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.LatLngBounds
import kotlinx.coroutines.CoroutineScope
Expand All @@ -29,7 +30,7 @@ import org.json.JSONObject
),
],
)
class CapacitorGoogleMapsPlugin : Plugin() {
class CapacitorGoogleMapsPlugin : Plugin(), OnMapsSdkInitializedCallback {
private var maps: HashMap<String, CapacitorGoogleMap> = HashMap()
private var cachedTouchEvents: HashMap<String, MutableList<MotionEvent>> = HashMap()
private val tag: String = "CAP-GOOGLE-MAPS"
Expand All @@ -43,7 +44,8 @@ class CapacitorGoogleMapsPlugin : Plugin() {
override fun load() {
super.load()

MapsInitializer.initialize(this.context, MapsInitializer.Renderer.LATEST, null)
MapsInitializer.initialize(this.context, MapsInitializer.Renderer.LATEST, this)


this.bridge.webView.setOnTouchListener(
object : View.OnTouchListener {
Expand Down Expand Up @@ -90,6 +92,13 @@ class CapacitorGoogleMapsPlugin : Plugin() {
)
}

override fun onMapsSdkInitialized(renderer: MapsInitializer.Renderer) {
when (renderer) {
MapsInitializer.Renderer.LATEST -> Logger.debug("Capacitor Google Maps", "Latest Google Maps renderer enabled")
MapsInitializer.Renderer.LEGACY -> Logger.debug("Capacitor Google Maps", "Legacy Google Maps renderer enabled - Cloud based map styling and advanced drawing not available")
}
}

override fun handleOnStart() {
super.handleOnStart()
maps.forEach { it.value.onStart() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class GoogleMapConfig(fromJSONObject: JSONObject) {
var liteMode: Boolean = false
var devicePixelRatio: Float = 1.00f
var styles: String? = null
var mapId: String? = null

init {
if (!fromJSONObject.has("width")) {
Expand Down Expand Up @@ -81,8 +82,14 @@ class GoogleMapConfig(fromJSONObject: JSONObject) {
center = LatLng(lat, lng)

val cameraPosition = CameraPosition(center, zoom.toFloat(), 0.0F, 0.0F)
googleMapOptions = GoogleMapOptions().camera(cameraPosition).liteMode(liteMode)

styles = fromJSONObject.getString("styles")

mapId = fromJSONObject.getString("androidMapId")

googleMapOptions = GoogleMapOptions().camera(cameraPosition).liteMode(liteMode)
if (mapId != null) {
googleMapOptions?.mapId(mapId!!)
}
}
}
3 changes: 3 additions & 0 deletions google-maps/ios/Plugin/GoogleMapConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public struct GoogleMapConfig: Codable {
let center: LatLng
let zoom: Double
let styles: String?
var mapId: String?

init(fromJSObject: JSObject) throws {
guard let width = fromJSObject["width"] as? Double else {
Expand Down Expand Up @@ -50,5 +51,7 @@ public struct GoogleMapConfig: Codable {
} else {
self.styles = nil
}

self.mapId = fromJSObject["iOSMapId"] as? String
}
}
10 changes: 9 additions & 1 deletion google-maps/ios/Plugin/Map.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class GMViewController: UIViewController {
var GMapView: GMSMapView!
var cameraPosition: [String: Double]!
var minimumClusterSize: Int?
var mapId: String?

private var clusterManager: GMUClusterManager?

Expand All @@ -25,7 +26,13 @@ class GMViewController: UIViewController {

let camera = GMSCameraPosition.camera(withLatitude: cameraPosition["latitude"] ?? 0, longitude: cameraPosition["longitude"] ?? 0, zoom: Float(cameraPosition["zoom"] ?? 12))
let frame = CGRect(x: mapViewBounds["x"] ?? 0, y: mapViewBounds["y"] ?? 0, width: mapViewBounds["width"] ?? 0, height: mapViewBounds["height"] ?? 0)
self.GMapView = GMSMapView.map(withFrame: frame, camera: camera)
if let id = mapId {
let gmsId = GMSMapID(identifier: id)
self.GMapView = GMSMapView(frame: frame, mapID: gmsId, camera: camera)
} else {
self.GMapView = GMSMapView(frame: frame, camera: camera)
}

self.view = GMapView
}

Expand Down Expand Up @@ -85,6 +92,7 @@ public class Map {
self.config = config
self.delegate = delegate
self.mapViewController = GMViewController()
self.mapViewController.mapId = config.mapId

self.render()
}
Expand Down
30 changes: 30 additions & 0 deletions google-maps/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,36 @@ export interface GoogleMapConfig extends google.maps.MapOptions {
* @since 4.3.0
*/
styles?: google.maps.MapTypeStyle[] | null;
/**
* A map id associated with a specific map style or feature.
*
* [Use Map IDs](https://developers.google.com/maps/documentation/get-map-id)
*
* Only for Web.
*
* @since 5.4.0
*/
mapId?: string;
/**
* A map id associated with a specific map style or feature.
*
* [Use Map IDs](https://developers.google.com/maps/documentation/get-map-id)
*
* Only for Android.
*
* @since 5.4.0
*/
androidMapId?: string;
/**
* A map id associated with a specific map style or feature.
*
* [Use Map IDs](https://developers.google.com/maps/documentation/get-map-id)
*
* Only for iOS.
*
* @since 5.4.0
*/
iOSMapId?: string;
}

/**
Expand Down
1 change: 1 addition & 0 deletions google-maps/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ export class CapacitorGoogleMapsWeb
async create(_args: CreateMapArgs): Promise<void> {
console.log(`Create map: ${_args.id}`);
await this.importGoogleLib(_args.apiKey, _args.region, _args.language);

this.maps[_args.id] = {
map: new window.google.maps.Map(_args.element, { ..._args.config }),
element: _args.element,
Expand Down

0 comments on commit bf7ec33

Please sign in to comment.