You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To use the Google Maps SDK on any platform, API keys associated with an account with billing enabled are required. These can be obtained from the Google Cloud Console. This is required for all three platforms, Android, iOS, and Javascript. Additional information about obtaining these API keys can be found in the Google Maps documentation for each platform.
iOS
The Google Maps SDK supports the use of showing the users current location via enableCurrentLocation(bool). To use this, Apple requires privacy descriptions to be specified in Info.plist:
The Google Maps SDK currently does not support running on simulators using the new M1-based Macbooks. This is a known and acknowledged issue and requires a fix from Google. If you are developing on a M1 Macbook, building and running on physical devices is still supported and is the recommended approach.
Android
The Google Maps SDK for Android requires you to add your API key to the AndroidManifest.xml file in your project.
This plugin will use the following project variables (defined in your app's variables.gradle file):
$googleMapsPlayServicesVersion: version of com.google.android.gms:play-services-maps (default: 18.0.2)
$googleMapsUtilsVersion: version of com.google.maps.android:android-maps-utils (default: 2.3.0)
$googleMapsKtxVersion: version of com.google.maps.android:maps-ktx (default: 3.4.0)
$googleMapsUtilsKtxVersion: version of com.google.maps.android:maps-utils-ktx (default: 3.4.0)
$kotlinxCoroutinesVersion: version of org.jetbrains.kotlinx:kotlinx-coroutines-android and org.jetbrains.kotlinx:kotlinx-coroutines-core (default: 1.6.3)
$androidxCoreKTXVersion: version of androidx.core:core-ktx (default: 1.8.0)
$kotlin_version: version of org.jetbrains.kotlin:kotlin-stdlib-jdk7 (default: 1.7.0)
Usage
The Google Maps Capacitor plugin ships with a web component that must be used to render the map in your application as it enables us to embed the native view more effectively on iOS. The plugin will automatically register this web component for use in your application.
For Angular users, you will get an error warning that this web component is unknown to the Angular compiler. This is resolved by modifying the module that declares your component to allow for custom web components.
On Android, the map is rendered beneath the entire webview, and uses this component to manage its positioning during scrolling events. This means that as the developer, you must ensure that the webview is transparent all the way through the layers to the very bottom. In a typically Ionic application, that means setting transparency on elements such as IonContent and the root HTML tag to ensure that it can be seen. If you can't see your map on Android, this should be the first thing you check.
On iOS, we render the map directly into the webview and so the same transparency effects are not required. We are investigating alternate methods for Android still and hope to resolve this better in a future update.
The Google Map element itself comes unstyled, so you should style it to fit within the layout of your page structure. Because we're rendering a view into this slot, by itself the element has no width or height, so be sure to set those explicitly.
Next, we should create the map reference. This is done by importing the GoogleMap class from the Capacitor plugin and calling the create method, and passing in the required parameters.
import{GoogleMap}from'@capacitor/google-maps';constapiKey='YOUR_API_KEY_HERE';constmapRef=document.getElementById('map');constnewMap=awaitGoogleMap.create({id: 'my-map',// Unique identifier for this map instanceelement: mapRef,// reference to the capacitor-google-map elementapiKey: apiKey,// Your Google Maps API Keyconfig: {center: {// The initial position to be rendered by the maplat: 33.6,lng: -117.9,},zoom: 8,// The initial zoom level to be rendered by the map},});
At this point, your map should be created within your application. Using the returned reference to the map, you can easily interact with your map in a number of way, a few of which are shown here.
constnewMap=awaitGoogleMap.create({...});// Add a marker to the mapconstmarkerId=awaitnewMap.addMarker({coordinate: {lat: 33.6,lng: -117.9}});// Move the map programmaticallyawaitnewMap.setCamera({coordinate: {lat: 33.6,lng: -117.9}});// Enable marker clusteringawaitnewMap.enableClustering();// Handle marker clickawaitnewMap.setOnMarkerClickListener((event)=>{...});// Clean up map referenceawaitnewMap.destroy();
<capacitor-google-mapid="map"></capacitor-google-map><buttononclick="createMap()">Create Map</button><style>capacitor-google-map {
display: inline-block;
width:275px;
height:400px;
}
</style><script>import{GoogleMap}from'@capacitor/google-maps';constcreateMap=async()=>{constmapRef=document.getElementById('map');constnewMap=awaitGoogleMap.create({id: 'my-map',// Unique identifier for this map instanceelement: mapRef,// reference to the capacitor-google-map elementapiKey: 'YOUR_API_KEY_HERE',// Your Google Maps API Keyconfig: {center: {// The initial position to be rendered by the maplat: 33.6,lng: -117.9,},zoom: 8,// The initial zoom level to be rendered by the map},});};</script>