maps-api-v3_如何为Android配置Google Maps API密钥

本文详细介绍了如何为Android应用配置Google Maps API V3的密钥,确保您的应用程序能够顺利使用Google Maps服务。通过遵循步骤,开发者可以成功集成地图功能并实现HTTPS安全连接。
摘要由CSDN通过智能技术生成

maps-api-v3

介绍 (Introduction)

After successfully publishing a couple of apps on Google Play, I have experienced an unusual thing: the day after releasing one of my apps, I noticed that the newly released application was showing a gray screen in the place where Google Map was supposed to appear!

Google Play上成功发布了两个应用程序后,我遇到了一件不寻常的事情:发布我的一个应用程序的第二天,我注意到新发布的应用程序在应该显示Google Map的位置显示了灰屏!

Since it is a travel app, and I am using Google Map to display locations of interesting objects nearby, it is pretty important to have it working.

由于它是一款旅行应用程序,并且我正在使用Google Map显示附近有趣物体的位置,因此使其正常工作非常重要。

Anyway, I connected my phone via ADB and in Logcat I saw the following error:

无论如何,我通过ADB连接了手机,在Logcat中看到了以下错误:

E/Google Maps Android API: Authorization failure. Please see https://developers.google.com/maps/documentation/android-api/start for how to correctly set up the map.

My first thought was that I may have forgotten to check the API key for Google Maps for the release version. I immediately opened Android Studio and on my surprise both debug and release keys were there. Next, I opened Google API Console and voilà — only my debug key was listed there and release key was gone — which was causing my Google Map view to render gray background, as it was not properly defined.

我的第一个想法是,我可能忘记了为发行版检查Google Maps的API密钥。 我立即打开了Android Studio,令人惊讶的是调试发布键都在那里。 接下来,我打开了Google API控制台 ,打开了voilà(仅列出了我的调试键,而释放键不见了),这导致我的Google Map视图呈现灰色背景,因为定义不正确。

I have read a couple of answers online, and none of them were actually enough to resolve my issue.

我已经在线阅读了一些答案,但实际上没有一个答案足以解决我的问题。

So, here it is — the proper way of setting API keys for Google Maps.

因此,这是为Google Maps设置API密钥的正确方法。

API密钥 (API keys)

As you could conclude from the introduction, in order to use Google Maps in Android applications, we have to get those API keys. But in this post, I will take a step back and start by generating one Google Maps activity.

正如您可以从简介中得出的结论那样,为了在Android应用程序中使用Google Maps,我们必须获取这些API密钥。 但是在本文中,我将退后一步,首先生成一个Google Maps活动。

So, in your Android Studio project, click File -> New -> Activity -> Gallery -> Google Maps Activity. This action will add an activity in the AndroidManifest.xml file together with the following:

因此,在您的Android Studio项目中,点击文件->新建->活动->库-> Google Maps活动 。 该操作将在AndroidManifest.xml文件中添加以下活动:

Also, it will generate boilerplate files like layout resource file & activity class. Besides, it will create a placeholder for API keys under values/google_maps_api.xml. Note that it creates two different files as explained in the comment section above.

另外,它将生成样板文件,例如布局资源文件和活动类。 此外,它将在values / google_maps_api.xml下为API密钥创建一个占位符 请注意,它会创建两个不同的文件,如上面的注释部分所述。

\app\src\debug\res\values\google_maps_api.xml \app\src\release\res\values\google_maps_api.xml

Even though those two files are different, placeholder looks the same in both:

即使这两个文件不同,但两者的占位符看起来相同:

Keep in mind that in the Android project view, only debug google_maps_api.xml is shown.

请记住,在Android项目视图中,仅显示调试 google_maps_api.xml

Image for post

You can see both files by changing your project view from default Android to Project view.

您可以通过改变从默认的Android项目视图项目视图中看到这两个文件。

Image for post

So what are these keys for? Basically, you can use the debug API key while developing and testing your app, but when you are ready for release, you have to add the API key for the release version.

那么这些键是做什么用的呢? 基本上,您可以在开发和测试应用程序时使用调试 API密钥,但是在准备发布时,必须为发布版本添加API密钥。

Otherwise, you will end up facing the same problem as I talked about in the introduction.

否则,您最终将面临与导言中谈到的相同问题。

除错 (Debug)

In the \app\src\debug\res\values\google_maps_api.xml file, Android Studio will also provide you with a link that you can follow in order to generate your API key. Below that link are instructions for the manual key generation, for which you need a package name and SHA-1 certificate fingerprint.

\ app \ src \ debug \ res \ values \ google_maps_api.xml文件中,Android Studio还将为您提供一个链接,您可以点击该链接来生成API密钥。 该链接下面是手动密钥生成的说明,您需要该软件包的软件包名称SHA-1证书指纹。

If you have chosen the first option and clicked on the generated link, it will create a project for you on Google API Console, enable the Maps SDK for Android API and generate a key which will be restricted to your android app package name.

如果您选择了第一个选项并单击生成的链接,它将在Google API控制台上为您创建一个项目,启用Maps SDK for Android API并生成一个密钥,该密钥将限于您的Android应用程序包名称。

Image for post

Whichever option you chose — generating manually or with the given link, you will end-up having generated a debug API key which you can now copy-paste under above-given placeholder ‘’YOUR_KEY_HERE’’.

无论选择哪个选项-手动生成或使用给定链接生成,最终都将生成调试 API密钥,您现在可以在上述占位符“ YOUR_KEY_HERE”下复制粘贴该密钥。

After that, you can start developing and working with Google Maps in your Android app.

之后,您可以开始在Android应用中开发和使用Google Maps。

Note that it is not required for the debug key to be restricted, but it is a good practice to do so.

请注意,并不需要限制调试键,但这是一个好习惯。

释放 (Release)

Remember that before releasing your app on Google Play, you have to generate one more key for the release version.

请记住,在Google Play上发布您的应用之前,您必须为发布版本再生成一个密钥。

To do so, go to Google API Console -> APIs & Services -> Credentials and click on Create Credentials -> API key. This option will generate a key that is not restricted to anything (yet). Copy-paste that key under the placeholder in \app\src\release\res\values\google_maps_api.xml.

为此,请转到Google API控制台-> API和服务->凭据 ,然后点击创建凭据-> API密钥 。 此选项将生成不限于任何内容的密钥(尚未)。 将密钥复制粘贴到\ app \ src \ 版本 \ res \ values \ google_maps_api.xml中的占位符下

Now, you will have two different keys for debugging and release versions. You can go ahead and publish your app.

现在,您将具有两个用于调试发布版本的不同键。 您可以继续发布您的应用程序。

After that part is done, go to Google Play Console and enter your app. Click on Release Management -> App signing. On that page, you will see information about different certificates.

完成该部分后,转到Google Play控制台并输入您的应用。 单击发布管理->应用程序签名 。 在该页面上,您将看到有关不同证书的信息。

If we start from below, under the Upload certificate section you will see the SHA-1 certificate fingerprint where it clearly says:

如果从下面开始,则在“上载证书”部分下,您会看到SHA-1证书指纹 ,上面清楚地写着:

Use the certificate below to register your app signing key with your API providers for app testing purposes.

使用下面的证书向API提供商注册您的应用签名密钥,以进行应用测试。

Image for post

Also, this SHA-1 certificate fingerprint can be matched with the one used to generate your signed APK. That can be seen with the command:

此外,此SHA-1证书指纹可以与用于生成签名的APK的指纹匹配。 可以通过以下命令看到:

$ keytool -list -v -keystore your_keystore.jks -alias your_key_alias
Image for post

Now, let’s look at the above App signing certificate. There is another SHA-1 certificate fingerprint that is different from the previous one. There it says:

现在,让我们看一下上面的App签名证书 。 还有另一个SHA-1证书指纹 ,与以前的指纹不同。 那里说:

Use the certificate below to register your app signing key with your API providers.

使用下面的证书向API提供商注册您的应用签名密钥。

Image for post

Let’s copy that key and go to Google API Console. Open the API key which is used for the release version. Under Key restrictions, go to Application restrictions and select Android apps. Enter the application package name and paste the key copied from Google Play Console.

让我们复制该密钥,然后转到Google API控制台 。 打开用于发行版本的API密钥。 在“ 密钥限制”下 ,转到“ 应用程序限制”,然后选择“ Android应用程序” 。 输入应用程序包名称,然后粘贴从Google Play控制台复制的密钥。

Image for post

You can also restrict it to the desired API, under API restrictions. In this case, you can choose Maps SDK for Android and click Save.

您还可以在API限制下将其限制为所需的API 在这种情况下,您可以选择Android版Maps SDK,然后点击保存

Image for post

It will take some time for the changes to take effect.

更改生效需要一些时间。

After that, you will have two different API keys, one for debug and one for release versions.

之后,您将拥有两个不同的API密钥,一个用于调试 ,一个用于发行版本。

Image for post

结论 (Conclusion)

To conclude, even if it looks like it has many steps, it is actually an easy and straightforward process once you get familiar with Google API Console & Google Play Console

总而言之,即使看起来有很多步骤,但一旦您熟悉Google API控制台和Google Play控制台,这实际上就是一个简单而直接的过程

翻译自: https://medium.com/@drvucic94/how-to-configure-google-maps-api-keys-for-android-1496dfba6747

maps-api-v3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值