Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(google-maps): Separate mapId for Google Maps Cloud IDs #1750

Merged
merged 11 commits into from
Oct 23, 2023
1 change: 1 addition & 0 deletions google-maps/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,7 @@ 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) | | |


#### 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 -> Log.d("Capacitor Google Maps", "Latest Google Maps renderer enabled")
MapsInitializer.Renderer.LEGACY -> Log.d("Capacitor Google Maps", "Legacy Google Maps renderer enabled - Cloud based map styling and advanced drawing not available")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Logger.debug instead of android Log class so it can be globally disabled

}
}

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("mapId")

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["mapId"] 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
6 changes: 6 additions & 0 deletions google-maps/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ 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)
*/
mapId?: 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
Loading