feat: Using local.properties to provide API key (#398)

* feat: Using local.properties to provide API key instead of secure.properties.

* Remove redundant declaration in main AndroidManifest.xml

* Migrate ApiDemos - Kotlin to use local.properties.

* Update README for tutorials.

* Genericize.

* Remove references to secure.properties.

* s/master/main
This commit is contained in:
Chris Arriola 2020-11-25 13:38:00 -08:00 committed by GitHub
parent bf82dc8989
commit 2ab19b7e0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
35 changed files with 218 additions and 413 deletions

1
.gitignore vendored
View File

@ -4,4 +4,3 @@ build/
*.iml
local.properties
.DS_Store
secure.properties

View File

@ -7,20 +7,19 @@ in the Java programming language.
They demonstrate most of the features available in the API.
This app was written with a minSdk of 16 and the androidx appcompat library, but it can be easily
adapted to use native functionality instead (for example replacing `SupportMapFragment` with
`MapFragment`).
The Maps SDK for Android samples can be found under the `gms` gradle product flavor, while the Maps SDK V3 BETA samples can be found under the `v3` gradle product flavor. The active product flavor can be modified through Android Studios “Build Variants” toolbar options.
The Maps SDK for Android samples can be found under the `gms` gradle product
flavor, while the Maps SDK V3 BETA samples can be found under the `v3` gradle
product flavor. The active product flavor can be modified through
Android Studios “Build Variants” toolbar options.
Pre-requisites
--------------
- Android SDK v29
- Android SDK v30
- Latest Android Build Tools
- Android Support Repository
- Google Repository
- Google Play services
- Google Play Services
Getting Started
---------------
@ -35,12 +34,7 @@ If prompted for a gradle configuration accept the default settings.
Alternatively use the `gradlew build` command to build the project directly.
This demo app requires that you add your own Google Maps API key:
1. [Get a Maps API key](https://developers.google.com/maps/documentation/android-sdk/get-api-key)
1. Create a file in the root directory called `secure.properties` (this file should *NOT* be under version control to protect your API key)
1. Add a single line to `secure.properties` that looks like `MAPS_API_KEY=YOUR_API_KEY`, where `YOUR_API_KEY` is the API key you obtained in the first step
1. Build and run
This demo app requires that you add your own Google Maps API key. See [Get an API Key](../../docs/GET_AN_API_KEY.md) for more instructions.
Support
-------
@ -61,4 +55,4 @@ CONTRIBUTING.md.
License
-------
Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/master/LICENSE) at the root of this repo.
Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/main/LICENSE) at the root of this repo.

View File

@ -3,6 +3,15 @@ import org.apache.tools.ant.filters.ConcatFilter
apply plugin: 'com.android.application'
apply plugin: 'project-report'
// 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())
}
def mapsApiKey = properties.getProperty("MAPS_API_KEY", "")
android {
compileSdkVersion 30
@ -14,15 +23,16 @@ android {
versionName "1.0"
multiDexEnabled true
// Read the API key from ./secure.properties into R.string.maps_api_key
def secureProps = new Properties()
if (file("../../../secure.properties").exists()) {
file("../../../secure.properties")?.withInputStream { secureProps.load(it) }
}
resValue "string", "maps_api_key", (secureProps.getProperty("MAPS_API_KEY") ?: "")
// Inject the Maps API key into the manifest
manifestPlaceholders = [ mapsApiKey : mapsApiKey ]
// Reading the Maps API key into a BuildConfig value. This is only
// necessary so that the sample app can display a toast message if the
// API key was not set, otherwise, this can be skipped.
buildConfigField("String", "MAPS_API_KEY", "\"$mapsApiKey\"" )
// To add your Maps API key to this project:
// 1. Create a file ./secure.properties
// 1. Open the root project's local.properties file
// 2. Add this line, where YOUR_API_KEY is your API key:
// MAPS_API_KEY=YOUR_API_KEY
}

View File

@ -17,21 +17,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mapdemo">
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- EXTERNAL_STORAGE permissions are optional for Android 6.0 onwards. -->
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="22" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="22" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
@ -39,16 +24,6 @@
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!--
To add your Maps API key to this project:
1. Create a file ApiDemos/java/app/secure.properties
2. Add this line, where YOUR_API_KEY is your API key:
MAPS_API_KEY=YOUR_API_KEY
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/maps_api_key" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

View File

@ -85,8 +85,8 @@ public final class MainActivity extends AppCompatActivity
list.setOnItemClickListener(this);
list.setEmptyView(findViewById(R.id.empty));
if (getString(R.string.maps_api_key).isEmpty()) {
Toast.makeText(this, "Add your own API key in ApiDemos/java/secure.properties as MAPS_API_KEY=YOUR_API_KEY", Toast.LENGTH_LONG).show();
if (BuildConfig.MAPS_API_KEY.isEmpty()) {
Toast.makeText(this, "Add your own API key in local.properties as MAPS_API_KEY=YOUR_API_KEY", Toast.LENGTH_LONG).show();
}
}

View File

@ -17,6 +17,13 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mapdemo">
<!--
Android 11 package visibility changes require that apps specify which
set of other packages on the device that they can access. Since this
sample uses Google Maps, specifying the Google Maps package name is
required so that the buttons on the Map toolbar launch the Google Maps
app.
-->
<queries>
<package android:name="com.google.android.apps.maps" />
</queries>
@ -44,129 +51,14 @@
android:theme="@style/AppTheme">
<!--
To add your Maps API key to this project:
1. Create a file ApiDemos/java/app/secure.properties
To add your Maps API key to this project:
1. Open the root project's local.properties file
2. Add this line, where YOUR_API_KEY is your API key:
MAPS_API_KEY=YOUR_API_KEY
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/maps_api_key" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".BasicMapDemoActivity"
android:label="@string/basic_map_demo_label" />
<activity
android:name=".CameraDemoActivity"
android:label="@string/camera_demo_label" />
<activity
android:name=".CameraClampingDemoActivity"
android:label="@string/camera_clamping_demo_label" />
<activity
android:name=".CircleDemoActivity"
android:label="@string/circle_demo_label" />
<activity
android:name=".EventsDemoActivity"
android:label="@string/events_demo_label" />
<activity
android:name=".GroundOverlayDemoActivity"
android:label="@string/ground_overlay_demo_label" />
<activity
android:name=".IndoorDemoActivity"
android:label="@string/indoor_demo_label" />
<activity
android:name=".LayersDemoActivity"
android:label="@string/layers_demo_label" />
<activity
android:name=".LiteDemoActivity"
android:label="@string/lite_demo_label" />
<activity
android:name=".LiteListDemoActivity"
android:label="@string/lite_list_demo_label" />
<activity
android:name=".LocationSourceDemoActivity"
android:label="@string/location_source_demo_label" />
<activity
android:name=".MapInPagerDemoActivity"
android:label="@string/map_in_pager_demo_label" />
<activity
android:name=".MarkerDemoActivity"
android:label="@string/marker_demo_label" />
<activity
android:name=".MarkerCloseInfoWindowOnRetapDemoActivity"
android:label="@string/marker_close_info_window_on_retap_demo_label" />
<activity
android:name=".polyline.PolylineDemoActivity"
android:label="@string/polyline_demo_label" />
<activity
android:name=".MultiMapDemoActivity"
android:label="@string/multi_map_demo_label" />
<activity
android:name=".MyLocationDemoActivity"
android:label="@string/my_location_demo_label" />
<activity
android:name=".OptionsDemoActivity"
android:label="@string/options_demo_label" />
<activity
android:name=".PolygonDemoActivity"
android:label="@string/polygon_demo_label" />
<activity
android:name=".ProgrammaticDemoActivity"
android:label="@string/programmatic_demo_label" />
<activity
android:name=".RawMapViewDemoActivity"
android:label="@string/raw_map_view_demo_label" />
<activity
android:name=".RetainMapDemoActivity"
android:label="@string/retain_map_demo_label" />
<activity
android:name=".SaveStateDemoActivity"
android:label="@string/save_state_demo_label" />
<activity
android:name=".SnapshotDemoActivity"
android:label="@string/snapshot_demo_label" />
<activity
android:name=".SplitStreetViewPanoramaAndMapDemoActivity"
android:label="@string/split_street_view_panorama_and_map_demo_label" />
<activity
android:name=".StreetViewPanoramaBasicDemoActivity"
android:label="@string/street_view_panorama_basic_demo_label" />
<activity
android:name=".StreetViewPanoramaEventsDemoActivity"
android:label="@string/street_view_panorama_events_demo_label" />
<activity
android:name=".StreetViewPanoramaNavigationDemoActivity"
android:label="@string/street_view_panorama_navigation_demo_label" />
<activity
android:name=".StreetViewPanoramaOptionsDemoActivity"
android:label="@string/street_view_panorama_options_demo_label" />
<activity
android:name=".StreetViewPanoramaViewDemoActivity"
android:label="@string/street_view_panorama_view_demo_label" />
<activity
android:name=".StyledMapDemoActivity"
android:label="@string/styled_map_demo_label" />
<activity
android:name=".TagsDemoActivity"
android:label="@string/tags_demo_label" />
<activity
android:name=".TileCoordinateDemoActivity"
android:label="@string/tile_coordinate_demo_label" />
<activity
android:name=".TileOverlayDemoActivity"
android:label="@string/tile_overlay_demo_label" />
<activity
android:name=".UiSettingsDemoActivity"
android:label="@string/ui_settings_demo_label" />
<activity
android:name=".VisibleRegionDemoActivity"
android:label="@string/visible_region_demo_label" />
android:value="${mapsApiKey}" />
</application>

View File

@ -17,21 +17,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mapdemo">
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- EXTERNAL_STORAGE permissions are optional for Android 6.0 onwards. -->
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="22" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="22" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
@ -39,16 +24,6 @@
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!--
To add your Maps API key to this project:
1. Create a file ApiDemos/java/app/secure.properties
2. Add this line, where YOUR_API_KEY is your API key:
MAPS_API_KEY=YOUR_API_KEY
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/maps_api_key" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

View File

@ -85,8 +85,8 @@ public final class MainActivity extends AppCompatActivity
list.setOnItemClickListener(this);
list.setEmptyView(findViewById(R.id.empty));
if (getString(R.string.maps_api_key).isEmpty()) {
Toast.makeText(this, "Add your own API key in ApiDemos/java/app/secure.properties as MAPS_API_KEY=YOUR_API_KEY", Toast.LENGTH_LONG).show();
if (BuildConfig.MAPS_API_KEY.isEmpty()) {
Toast.makeText(this, "Add your own API key in local.properties as MAPS_API_KEY=YOUR_API_KEY", Toast.LENGTH_LONG).show();
}
}

View File

@ -18,3 +18,4 @@
# org.gradle.parallel=true
android.enableJetifier=true
android.useAndroidX=true
GOOGLE_MAPS_API_KEY="AIzaSyBri0nHwJxuIs8KgdsGXdJLFvg1vviUMfo"

View File

@ -1,21 +1,25 @@
Google Maps Android API Demos
===================================
These are demos for the [Google Maps Android API v2](https://developers.google.com/maps/documentation/android-api/) in Kotlin.
These are demos for the [Maps SDK for Android](https://developers.google.com/maps/documentation/android-api/)
and [Maps SDK for Android V3 BETA](https://developers.google.com/maps/documentation/android-sdk/v3-client-migration) libraries
in Kotlin.
They demonstrate most of the features available in the API.
This app was written for a minSdk of 15 and the v4 support library, but it can be easily adapted to
use native functionality instead.
(For example replacing ``SupportMapFragment`` with ``MapFragment``).
The Maps SDK for Android samples can be found under the `gms` gradle product flavor, while the Maps SDK V3 BETA samples can be found under the `v3` gradle product flavor. The active product flavor can be modified through Android Studios “Build Variants” toolbar options.
The Maps SDK for Android samples can be found under the `gms` gradle product
flavor, while the Maps SDK V3 BETA samples can be found under the `v3` gradle
product flavor. The active product flavor can be modified through
Android Studios “Build Variants” toolbar options.
Pre-requisites
--------------
- Android SDK v27
- Android SDK v30
- Latest Android Build Tools
- Android Support Repository
- Google Repository
- Google Play Services
Getting Started
---------------
@ -25,18 +29,13 @@ This sample uses the Gradle build system.
First download the samples by cloning this repository or downloading an archived
snapshot. (See the options at the top of the page.)
In Android Studio, use "Open an existing Android Studio project". Next select the `ApiDemos/kotlin/` directory that you downloaded
from this repository.
In Android Studio, use "Open an existing Android Studio project". Next select
the `ApiDemos/kotlin/` directory that you downloaded from this repository.
If prompted for a gradle configuration accept the default settings.
Alternatively use the `gradlew build` command to build the project directly.
This demo app requires that you add your own Google Maps API key:
1. [Get a Maps API key](https://developers.google.com/maps/documentation/android-sdk/get-api-key)
1. Create a file in the root directory called `secure.properties` (this file should *NOT* be under version control to protect your API key)
1. Add a single line to `secure.properties` that looks like `MAPS_API_KEY=YOUR_API_KEY`, where `YOUR_API_KEY` is the API key you obtained in the first step
1. Build and run
This demo app requires that you add your own Google Maps API key. See [Get an API Key](../../docs/GET_AN_API_KEY.md) for more instructions.
Support
-------
@ -57,4 +56,4 @@ CONTRIBUTING.md.
License
-------
Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/master/LICENSE) at the root of this repo.
Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/main/LICENSE) at the root of this repo.

View File

@ -4,6 +4,15 @@ apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
// 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())
}
def mapsApiKey = properties.getProperty("MAPS_API_KEY", "")
android {
compileSdkVersion 30
defaultConfig {
@ -15,15 +24,16 @@ android {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
// Read the API key from ./secure.properties into R.string.maps_api_key
def secureProps = new Properties()
if (file("../../../secure.properties").exists()) {
file("../../../secure.properties")?.withInputStream { secureProps.load(it) }
}
resValue "string", "maps_api_key", (secureProps.getProperty("MAPS_API_KEY") ?: "")
// Inject the Maps API key into the manifest
manifestPlaceholders = [ mapsApiKey : mapsApiKey ]
// Reading the Maps API key into a BuildConfig value. This is only
// necessary so that the sample app can display a toast message if the
// API key was not set, otherwise, this can be skipped.
buildConfigField("String", "MAPS_API_KEY", "\"$mapsApiKey\"" )
// To add your Maps API key to this project:
// 1. Create a file ./secure.properties
// 1. Open the root project's local.properties file
// 2. Add this line, where YOUR_API_KEY is your API key:
// MAPS_API_KEY=YOUR_API_KEY
}
@ -52,7 +62,6 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support.constraint:constraint-layout:2.0.4'
implementation "androidx.cardview:cardview:1.0.0"
implementation "androidx.recyclerview:recyclerview:1.1.0"
implementation 'androidx.multidex:multidex:2.0.1'

View File

@ -16,11 +16,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kotlindemos">
<!-- This app requires location permissions for the layers demo.
The user's current location is displayed using the 'My Location' layer. -->
<!-- Access to location is needed for the UI Settings Demo -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
@ -28,15 +23,7 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!--
To add your Maps API key to this project:
1. Create a file ApiDemos/kotlin/secure.properties
2. Add this line, where YOUR_API_KEY is your API key:
MAPS_API_KEY=YOUR_API_KEY
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/maps_api_key" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

View File

@ -49,10 +49,9 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
emptyView = emptyMessage
}
if (getString(R.string.maps_api_key).isEmpty()) {
Toast.makeText(this, "Add your own API key in ApiDemos/kotlin/app/secure.properties as MAPS_API_KEY=YOUR_API_KEY", Toast.LENGTH_LONG).show()
if (BuildConfig.MAPS_API_KEY.isEmpty()) {
Toast.makeText(this, "Add your own API key in local.properties as MAPS_API_KEY=YOUR_API_KEY", Toast.LENGTH_LONG).show()
}
}
/**

View File

@ -16,13 +16,22 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kotlindemos">
<!--
Android 11 package visibility changes require that apps specify which
set of other packages on the device that they can access. Since this
sample uses Google Maps, specifying the Google Maps package name is
required so that the buttons on the Map toolbar launch the Google Maps
app.
-->
<queries>
<package android:name="com.google.android.apps.maps" />
</queries>
<!-- This app requires location permissions for the layers demo.
The user's current location is displayed using the 'My Location' layer. -->
<!-- Access to location is needed for the UI Settings Demo -->
<!--
This app requires location permissions for the layers demo.
The user's current location is displayed using the 'My Location' layer.
Access to location is needed for the UI Settings Demo
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
@ -33,47 +42,14 @@
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!--
To add your Maps API key to this project:
1. Create a file ApiDemos/kotlin/secure.properties
To add your Maps API key to this project:
1. Open the root project'sl local.properties file
2. Add this line, where YOUR_API_KEY is your API key:
MAPS_API_KEY=YOUR_API_KEY
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/maps_api_key" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".BasicMapDemoActivity" />
<activity android:name=".CameraDemoActivity"/>
<activity android:name=".CircleDemoActivity" />
<activity android:name=".CloseInfoWindowDemoActivity" />
<activity android:name=".EventsDemoActivity" />
<activity android:name=".GroundOverlayDemoActivity" />
<activity android:name=".IndoorDemoActivity" />
<activity android:name=".LayersDemoActivity"/>
<activity android:name=".LiteDemoActivity" />
<activity android:name=".LiteListDemoActivity" />
<activity android:name=".MarkerDemoActivity"/>
<activity android:name=".MyLocationDemoActivity" />
<activity android:name=".PolygonDemoActivity"/>
<activity android:name=".RawMapViewDemoActivity"/>
<activity android:name=".SplitStreetViewPanoramaAndMapDemoActivity" />
<activity android:name=".StreetViewPanoramaBasicDemoActivity" />
<activity android:name=".StreetViewPanoramaEventsDemoActivity" />
<activity android:name=".StreetViewPanoramaNavigationDemoActivity" />
<activity android:name=".StreetViewPanoramaOptionsDemoActivity" />
<activity android:name=".StreetViewPanoramaViewDemoActivity" />
<activity android:name=".TagsDemoActivity"/>
<activity android:name=".TileCoordinateDemoActivity"/>
<activity android:name=".TileOverlayDemoActivity"/>
<activity android:name=".UiSettingsDemoActivity" />
<activity android:name=".VisibleRegionDemoActivity"/>
<activity android:name=".polyline.PolylineDemoActivity" />
android:value="${mapsApiKey}" />
</application>
</manifest>

View File

@ -16,11 +16,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kotlindemos">
<!-- This app requires location permissions for the layers demo.
The user's current location is displayed using the 'My Location' layer. -->
<!-- Access to location is needed for the UI Settings Demo -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
@ -28,15 +23,7 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!--
To add your Maps API key to this project:
1. Create a file ApiDemos/kotlin/secure.properties
2. Add this line, where YOUR_API_KEY is your API key:
MAPS_API_KEY=YOUR_API_KEY
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/maps_api_key" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

View File

@ -56,8 +56,8 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
emptyView = emptyMessage
}
if (getString(R.string.maps_api_key).isEmpty()) {
Toast.makeText(this, "Add your own API key in ApiDemos/kotlin/app/secure.properties as MAPS_API_KEY=YOUR_API_KEY", Toast.LENGTH_LONG).show()
if (BuildConfig.MAPS_API_KEY.isEmpty()) {
Toast.makeText(this, "Add your own API key in local.properties as MAPS_API_KEY=YOUR_API_KEY", Toast.LENGTH_LONG).show()
}
}

View File

@ -13,7 +13,7 @@ This repo contains the following samples:
1. [WearOS](WearOS):
Displays a map on a Wear OS device. This sample demonstrates the basic
setup required for a gradle-based Android Studio project.
1. [Tutorials](https://github.com/googlemaps/android-samples/tree/master/tutorials): Samples
1. [Tutorials](https://github.com/googlemaps/android-samples/tree/main/tutorials): Samples
associated with tutorials in the developer's guide. See each sample for a link to the associated guide.
1. [Snippets](snippets): Snippets for code found in https://developers.google.com/maps/documentation/android-sdk
@ -39,12 +39,7 @@ If prompted for a gradle configuration accept the default settings.
Alternatively use the `gradlew build` command to build the project directly.
The demo apps require that you add your own Google Maps API key:
1. [Get a Maps API key](https://developers.google.com/maps/documentation/android-sdk/get-api-key)
1. Create a file in the root directory called `secure.properties` (this file should *NOT* be under version control to protect your API key)
1. Add a single line to `secure.properties` that looks like `MAPS_API_KEY=YOUR_API_KEY`, where `YOUR_API_KEY` is the API key you obtained in the first step
1. Build and run
The demo apps require that you add your own Google Maps API key. See [Get an API Key](docs/GET_AN_API_KEY.md) docs for more instructions.
Support
-------

8
docs/GET_AN_API_KEY.md Normal file
View File

@ -0,0 +1,8 @@
# Getting an API Key
The demos found in this repository require an API key. To create and use an API key:
1. [Get a Maps API key](https://developers.google.com/maps/documentation/android-sdk/get-api-key)
1. Open the file in the root directory of the sample called `local.properties` (by default, this file is **NOT** under version control)
1. Add a single line to `local.properties` that looks like `MAPS_API_KEY=YOUR_API_KEY`, where `YOUR_API_KEY` is the API key you obtained in the first step
1. Build and run

View File

@ -1,3 +0,0 @@
# This is a template for the `secure.properties` file
# To use this, replace "YOUR_API_KEY" with your GMP API key and rename this file to `secure.properties`
MAPS_API_KEY="YOUR_API_KEY"

View File

@ -7,9 +7,11 @@ This sample goes hand in hand with a tutorial for the Google Maps Android API:
Prerequisites
--------------
- Android SDK v24
- Android SDK v30
- Latest Android Build Tools
- Android Support Repository
- Google Repository
- Google Play Services
Getting started
---------------
@ -24,13 +26,8 @@ This sample uses the Gradle build system.
1. If prompted for a gradle configuration, accept the default settings.
Alternatively use the `gradlew build` command to build the project directly.
This demo app requires that you add your own Google Maps API key:
1. [Get a Maps API key](https://developers.google.com/maps/documentation/android-sdk/get-api-key)
1. Create a file in the root directory called `secure.properties` (this file should *NOT* be under version control to protect your API key)
1. Add a single line to `secure.properties` that looks like `MAPS_API_KEY=YOUR_API_KEY`, where `YOUR_API_KEY` is the API key you obtained in the first step
1. Build and run
This demo app requires that you add your own Google Maps API key. See [Get an API Key](../../../docs/GET_AN_API_KEY.md) for more instructions.
Support
-------
@ -47,4 +44,4 @@ https://github.com/googlemaps/android-samples/issues
License
-------
Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/master/LICENSE) at the root of this repo.
Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/main/LICENSE) at the root of this repo.

View File

@ -1,25 +1,30 @@
apply plugin: 'com.android.application'
// 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())
}
def mapsApiKey = properties.getProperty("MAPS_API_KEY", "")
android {
compileSdkVersion 29
compileSdkVersion 30
buildToolsVersion '28.0.3'
defaultConfig {
applicationId "com.example.currentplacedetailsonmap"
minSdkVersion 17
targetSdkVersion 29
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
// Read the API key from ./secure.properties into R.string.maps_api_key
def secureProps = new Properties()
if (file("../../../../secure.properties").exists()) {
file("../../../../secure.properties")?.withInputStream { secureProps.load(it) }
}
resValue "string", "maps_api_key", (secureProps.getProperty("MAPS_API_KEY") ?: "")
// Read the API key from local.properties into R.string.maps_api_key
resValue "string", "maps_api_key", mapsApiKey
// To add your Maps API key to this project:
// 1. Create a file ./secure.properties
// 1. Open the root project's local.properties file
// 2. Add this line, where YOUR_API_KEY is your API key:
// MAPS_API_KEY=YOUR_API_KEY

View File

@ -7,9 +7,11 @@ This sample goes hand in hand with a tutorial for the Google Maps Android API:
Prerequisites
--------------
- Android SDK v24
- Android SDK v30
- Latest Android Build Tools
- Android Support Repository
- Google Repository
- Google Play Services
Getting started
---------------
@ -23,12 +25,7 @@ This sample uses the Gradle build system.
1. If prompted for a gradle configuration, accept the default settings.
Alternatively use the `gradlew build` command to build the project directly.
This demo app requires that you add your own Google Maps API key:
1. [Get a Maps API key](https://developers.google.com/maps/documentation/android-sdk/get-api-key)
1. Create a file in the root directory called `secure.properties` (this file should *NOT* be under version control to protect your API key)
1. Add a single line to `secure.properties` that looks like `MAPS_API_KEY=YOUR_API_KEY`, where `YOUR_API_KEY` is the API key you obtained in the first step
1. Build and run
This demo app requires that you add your own Google Maps API key. See [Get an API Key](../../../docs/GET_AN_API_KEY.md) for more instructions.
Support
-------
@ -46,4 +43,4 @@ https://github.com/googlemaps/android-samples/issues
License
-------
Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/master/LICENSE) at the root of this repo.
Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/main/LICENSE) at the root of this repo.

View File

@ -1,5 +1,14 @@
apply plugin: 'com.android.application'
// 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())
}
def mapsApiKey = properties.getProperty("MAPS_API_KEY", "")
android {
compileSdkVersion 29
buildToolsVersion "28.0.3"
@ -11,17 +20,14 @@ android {
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
// Read the API key from ./secure.properties into R.string.maps_api_key
def secureProps = new Properties()
if (file("../../../../secure.properties").exists()) {
file("../../../../secure.properties")?.withInputStream { secureProps.load(it) }
}
resValue "string", "maps_api_key", (secureProps.getProperty("MAPS_API_KEY") ?: "")
// Read the API key from local.properties into R.string.maps_api_key
resValue "string", "maps_api_key", mapsApiKey
// To add your Maps API key to this project:
// 1. Create a file ./secure.properties
// 1. Open the root project's local.properties file
// 2. Add this line, where YOUR_API_KEY is your API key:
// MAPS_API_KEY=YOUR_API_KEY
}
buildTypes {
release {

View File

@ -38,8 +38,8 @@ public class MapsMarkerActivity extends AppCompatActivity
super.onCreate(savedInstanceState);
// Retrieve the content view that renders the map.
setContentView(R.layout.activity_maps);
// Get the SupportMapFragment and request notification
// when the map is ready to be used.
// Get the SupportMapFragment and request notification when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

View File

@ -23,13 +23,8 @@ This sample uses the Gradle build system.
1. Select the `Polygons` directory that you downloaded with this repository.
1. If prompted for a gradle configuration, accept the default settings.
Alternatively use the `gradlew build` command to build the project directly.
This demo app requires that you add your own Google Maps API key:
1. [Get a Maps API key](https://developers.google.com/maps/documentation/android-sdk/get-api-key)
1. Create a file in the root directory called `secure.properties` (this file should *NOT* be under version control to protect your API key)
1. Add a single line to `secure.properties` that looks like `MAPS_API_KEY=YOUR_API_KEY`, where `YOUR_API_KEY` is the API key you obtained in the first step
1. Build and run
This demo app requires that you add your own Google Maps API key. See [Get an API Key](../../../docs/GET_AN_API_KEY.md) for more instructions.
Support
-------
@ -47,4 +42,4 @@ https://github.com/googlemaps/android-samples/issues
License
-------
Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/master/LICENSE) at the root of this repo.
Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/main/LICENSE) at the root of this repo.

View File

@ -1,5 +1,14 @@
apply plugin: 'com.android.application'
// 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())
}
def mapsApiKey = properties.getProperty("MAPS_API_KEY", "")
android {
compileSdkVersion 29
buildToolsVersion '28.0.3'
@ -11,15 +20,11 @@ android {
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
// Read the API key from ./secure.properties into R.string.maps_api_key
def secureProps = new Properties()
if (file("../../../../secure.properties").exists()) {
file("../../../../secure.properties")?.withInputStream { secureProps.load(it) }
}
resValue "string", "maps_api_key", (secureProps.getProperty("MAPS_API_KEY") ?: "")
// Read the API key from local.properties into R.string.maps_api_key
resValue "string", "maps_api_key", mapsApiKey
// To add your Maps API key to this project:
// 1. Create a file ./secure.properties
// 1. Open the root project's local.properties file
// 2. Add this line, where YOUR_API_KEY is your API key:
// MAPS_API_KEY=YOUR_API_KEY

View File

@ -24,12 +24,7 @@ This sample uses the Gradle build system.
1. If prompted for a gradle configuration, accept the default settings.
Alternatively use the "gradlew build" command to build the project directly.
This demo app requires that you add your own Google Maps API key:
1. [Get a Maps API key](https://developers.google.com/maps/documentation/android-sdk/get-api-key)
1. Create a file in the root directory called `secure.properties` (this file should *NOT* be under version control to protect your API key)
1. Add a single line to `secure.properties` that looks like `MAPS_API_KEY=YOUR_API_KEY`, where `YOUR_API_KEY` is the API key you obtained in the first step
1. Build and run
This demo app requires that you add your own Google Maps API key. See [Get an API Key](../../../docs/GET_AN_API_KEY.md) for more instructions.
Support
-------
@ -47,4 +42,4 @@ https://github.com/googlemaps/android-samples/issues
License
-------
Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/master/LICENSE) at the root of this repo.
Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/main/LICENSE) at the root of this repo.

View File

@ -1,5 +1,14 @@
apply plugin: 'com.android.application'
// 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())
}
def mapsApiKey = properties.getProperty("MAPS_API_KEY", "")
android {
compileSdkVersion 29
buildToolsVersion '28.0.3'
@ -10,16 +19,12 @@ android {
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
// Read the API key from ./secure.properties into R.string.maps_api_key
def secureProps = new Properties()
if (file("../../../../secure.properties").exists()) {
file("../../../../secure.properties")?.withInputStream { secureProps.load(it) }
}
resValue "string", "maps_api_key", (secureProps.getProperty("MAPS_API_KEY") ?: "")
// Read the API key from local.properties into R.string.maps_api_key
resValue "string", "maps_api_key", mapsApiKey
// To add your Maps API key to this project:
// 1. Create a file ./secure.properties
// 1. Open the root project's local.properties file
// 2. Add this line, where YOUR_API_KEY is your API key:
// MAPS_API_KEY=YOUR_API_KEY
}

View File

@ -24,13 +24,7 @@ This sample uses the Gradle build system.
1. If prompted for a gradle configuration, accept the default settings.
Alternatively use the `gradlew build` command to build the project directly.
This demo app requires that you add your own Google Maps API key:
1. [Get a Maps API key](https://developers.google.com/maps/documentation/android-sdk/get-api-key)
1. Create a file in the root directory called `secure.properties` (this file should *NOT* be under version control to protect your API key)
1. Add a single line to `secure.properties` that looks like `MAPS_API_KEY=YOUR_API_KEY`, where `YOUR_API_KEY` is the API key you obtained in the first step
1. Build and run
This demo app requires that you add your own Google Maps API key. See [Get an API Key](../../../docs/GET_AN_API_KEY.md) for more instructions.
Support
-------
@ -48,4 +42,4 @@ https://github.com/googlemaps/android-samples/issues
License
-------
Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/master/LICENSE) at the root of this repo.
Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/main/LICENSE) at the root of this repo.

View File

@ -2,6 +2,15 @@ apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
// 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())
}
def mapsApiKey = properties.getProperty("MAPS_API_KEY", "")
android {
compileSdkVersion 29
buildToolsVersion '28.0.3'
@ -13,15 +22,11 @@ android {
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
// Read the API key from ./secure.properties into R.string.maps_api_key
def secureProps = new Properties()
if (file("../../../../secure.properties").exists()) {
file("../../../../secure.properties")?.withInputStream { secureProps.load(it) }
}
resValue "string", "maps_api_key", (secureProps.getProperty("MAPS_API_KEY") ?: "")
// Read the API key from local.properties into R.string.maps_api_key
resValue "string", "maps_api_key", mapsApiKey
// To add your Maps API key to this project:
// 1. Create a file ./secure.properties
// 1. Open the root project's local.properties file
// 2. Add this line, where YOUR_API_KEY is your API key:
// MAPS_API_KEY=YOUR_API_KEY

View File

@ -7,9 +7,11 @@ This sample goes hand in hand with a tutorial for the Google Maps Android API:
Prerequisites
--------------
- Android SDK v24
- Android SDK v30
- Latest Android Build Tools
- Android Support Repository
- Google Repository
- Google Play Services
Getting started
---------------
@ -23,13 +25,7 @@ This sample uses the Gradle build system.
1. If prompted for a gradle configuration, accept the default settings.
Alternatively use the `gradlew build` command to build the project directly.
This demo app requires that you add your own Google Maps API key:
1. [Get a Maps API key](https://developers.google.com/maps/documentation/android-sdk/get-api-key)
1. Create a file in the root directory called `secure.properties` (this file should *NOT* be under version control to protect your API key)
1. Add a single line to `secure.properties` that looks like `MAPS_API_KEY=YOUR_API_KEY`, where `YOUR_API_KEY` is the API key you obtained in the first step
1. Build and run
This demo app requires that you add your own Google Maps API key. See [Get an API Key](../../../docs/GET_AN_API_KEY.md) for more instructions.
Support
-------
@ -47,4 +43,4 @@ https://github.com/googlemaps/android-samples/issues
License
-------
Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/master/LICENSE) at the root of this repo.
Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/main/LICENSE) at the root of this repo.

View File

@ -2,6 +2,15 @@ apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
// 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())
}
def mapsApiKey = properties.getProperty("MAPS_API_KEY", "")
android {
compileSdkVersion 29
buildToolsVersion "28.0.3"
@ -13,15 +22,11 @@ android {
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
// Read the API key from ./secure.properties into R.string.maps_api_key
def secureProps = new Properties()
if (file("../../../../secure.properties").exists()) {
file("../../../../secure.properties")?.withInputStream { secureProps.load(it) }
}
resValue "string", "maps_api_key", (secureProps.getProperty("MAPS_API_KEY") ?: "")
// Read the API key from local.properties into R.string.maps_api_key
resValue "string", "maps_api_key", mapsApiKey
// To add your Maps API key to this project:
// 1. Create a file ./secure.properties
// 1. Open the root project's local.properties file
// 2. Add this line, where YOUR_API_KEY is your API key:
// MAPS_API_KEY=YOUR_API_KEY
}

View File

@ -36,10 +36,7 @@ class MapsMarkerActivity : AppCompatActivity(), OnMapReadyCallback {
super.onCreate(savedInstanceState)
// Retrieve the content view that renders the map.
setContentView(R.layout.activity_maps)
if (getString(R.string.maps_api_key).isEmpty()) {
Toast.makeText(this, "Add your own API key in MapWithMarker/app/secure.properties as MAPS_API_KEY=YOUR_API_KEY", Toast.LENGTH_LONG).show()
}
// Get the SupportMapFragment and request notification when the map is ready to be used.
val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as? SupportMapFragment
mapFragment?.getMapAsync(this)

View File

@ -24,12 +24,7 @@ This sample uses the Gradle build system.
1. If prompted for a gradle configuration, accept the default settings.
Alternatively use the `gradlew build` command to build the project directly.
This demo app requires that you add your own Google Maps API key:
1. [Get a Maps API key](https://developers.google.com/maps/documentation/android-sdk/get-api-key)
1. Create a file in the root directory called `secure.properties` (this file should *NOT* be under version control to protect your API key)
1. Add a single line to `secure.properties` that looks like `MAPS_API_KEY=YOUR_API_KEY`, where `YOUR_API_KEY` is the API key you obtained in the first step
1. Build and run
This demo app requires that you add your own Google Maps API key. See [Get an API Key](../../../docs/GET_AN_API_KEY.md) for more instructions.
Support
-------
@ -47,4 +42,4 @@ https://github.com/googlemaps/android-samples/issues
License
-------
Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/master/LICENSE) at the root of this repo.
Please refer to the [LICENSE](https://github.com/googlemaps/android-samples/blob/main/LICENSE) at the root of this repo.

View File

@ -2,6 +2,15 @@ apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
// 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())
}
def mapsApiKey = properties.getProperty("MAPS_API_KEY", "")
android {
compileSdkVersion 29
buildToolsVersion '28.0.3'
@ -13,15 +22,11 @@ android {
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
// Read the API key from ./secure.properties into R.string.maps_api_key
def secureProps = new Properties()
if (file("../../../../secure.properties").exists()) {
file("../../../../secure.properties")?.withInputStream { secureProps.load(it) }
}
resValue "string", "maps_api_key", (secureProps.getProperty("MAPS_API_KEY") ?: "")
// Read the API key from local.properties into R.string.maps_api_key
resValue "string", "maps_api_key", mapsApiKey
// To add your Maps API key to this project:
// 1. Create a file ./secure.properties
// 1. Open the root project's local.properties file
// 2. Add this line, where YOUR_API_KEY is your API key:
// MAPS_API_KEY=YOUR_API_KEY
}