(Optional) Use Griver capabilities
This topic introduces how to import Google Maps to use Griver's map capabilities.
Overview
After the integration of IAPConnect
, Griver's core capabilities are included in your project. To use the Griver's map capabilities, you need to integrate Google Maps into your project. If Google Maps is not imported into your project, your mini program would encounter error 1
(JSAPI not existed
) when calling map-related JSAPIs.
For the complete list of map-related JSAPIs, refer to my.createMapContext.
Integrate Griver Map
Follow the procedures below to integrate Griver Map into your project:
Step 1. Get an API Key from Google
- To get an API key, follow Google's instructions. The API key is required to use the Google Maps service.
- Once you get the API key, save it to the
local.properties
file:
MAPS_API_KEY=YOUR_API_KEY
- Update the
build.gradle
file to inject the mapsApiKey variable into theAndroidManifest.xml
file:
android {
defaultConfig {
// ...
// Set the properties within 'local.properties' into a 'Properties' class so that values
// within `local.properties` (e.g. Maps API key) are accessible in this file.
Properties properties = new Properties()
if (rootProject.file("local.properties").exists()) {
properties.load(rootProject.file("local.properties").newDataInputStream())
}
manifestPlaceholders = [ mapsApiKey : properties.getProperty("MAPS_API_KEY", "") ]
}
}
In your AndroidManifest.xml
file:
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="${mapsApiKey}" />
Step 2. Add the Maps SDK dependencies
Add the Maps dependencies into your app-level build.gradle
file:
implementation 'com.google.android.gms:play-services-maps:16.1.0'
implementation 'com.google.android.gms:play-services-location:16.0.0'
implementation 'com.google.maps.android:android-maps-utils:0.5'
Step 3. (Optional) Prevent mini program from using API key
If a mini program does not provide gAPIKey
, the super app's API key will be used by default. To prevent certain mini programs from using its API key, the super app can implement GriverMapCustomAPIKeyExtension
, as shown in the sample code below:
public class DemoGriverMapCustomAPIKeyExtension implements GriverMapCustomAPIKeyExtension {
@Override
public boolean canUseGoogleAPIKey(String appId) {
return false;
}
}
And register the Extension
after the SDK is initialized:
Griver.registerExtension(new DemoGriverMapCustomAPIKeyExtension());
More information
To learn how to use map-related JSAPIs in your mini program, check the following topics:
- <map> component
- my.createMapContext JSAPI
- MapContext overview