maps-api-v3_Android Google Maps API集成

maps-api-v3

In this tutorial we’ll show how to integrate android google maps API in our application and customise it according to our own needs. Android Google Maps is an important utility that features in numerous apps.

在本教程中,我们将展示如何在我们的应用程序中集成android google maps API并根据我们自己的需要对其进行自定义。 Android Google Maps是一个重要的实用程序,可在众多应用程序中使用。

Android Google Maps API (Android Google Maps API)

Android Google Maps v1 API is deprecated now so we’ll integrate Google Maps v2 API in our application.

现在不推荐使用Android Google Maps v1 API,因此我们将Google Maps v2 API集成到我们的应用程序中。

The API automatically handles access to Google Maps servers, data downloading, map display, and response to map gestures.

该API自动处理对Google Maps服务器的访问,数据下载,地图显示以及对地图手势的响应。

We can use the API calls to add markers, polygons, show user location and provide additional features for the user to interact with the map.

我们可以使用API​​调用来添加标记,多边形,显示用户位置并为用户提供与地图交互的其他功能。

Android Google Maps API密钥 (Android Google Maps API key)

To use android google maps api in our application we need to register our app in the google developer console and enable the Google Map API keys.

要在我们的应用程序中使用android google maps api,我们需要在google开发者控制台中注册我们的应用程序并启用Google Map API密钥。

Click the Create Project button to create a new project and give it the desired name.

单击创建项目按钮以创建一个新项目,并为其指定所需的名称。

Select Google Maps Android API and click the Enable API button that appears on the next page.

选择Google Maps Android API,然后单击下一页上显示的Enable API按钮。

After the API is enabled, click Go to Credentials, from the left panel. From the Add Credentials menu , select API Key.

启用API后,单击左侧面板中的Go to Credentials。 从“添加凭据”菜单中,选择“ API密钥”。

Select Android Key as shown below.

选择Android Key,如下所示。

Click the add package name and fingerprint button to add your app’s package name (We’ve used com.journaldev.integratingmaps) and the SHA1 fingerprint. After clicking the Create button, you will be shown an API key that we will use in our app.

单击添加软件包名称和指纹按钮以添加应用程序的软件包名称(我们已使用com.journaldev.integratingmaps)和SHA1指纹 。 单击创建按钮后,将显示一个API密钥,供我们在应用程序中使用。

Note : The SHA 1 fingerprint is fetched by running the following commands:

注意:通过运行以下命令来获取SHA 1指纹:

  • For Linux OS/ MAC :
    keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android

    对于Linux OS / MAC
  • For Windows :
    keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android

    对于Windows

The SHA 1 fingerprint that we’ve added by running the above command on the terminal is shown in the image below along with the project package name. Your SHA 1 fingerprint key could be a different one.

下图显示了我们通过在终端上运行以上命令添加的SHA 1指纹以及项目包名称。 您的SHA 1指纹密钥可能是另一种。

添加Android谷歌地图 (Adding android google map)

Add the following dependency to build.gradle file.

将以下依赖项添加到build.gradle文件。

compile 'com.google.android.gms:play-services:8.3.0'

Add the following tags under the application tag in AndroidManifest.xml.

在AndroidManifest.xml的application标记下添加以下标记。

<meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="YOUR_GOOGLE_MAP_API_KEY" />

<meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

Add your own API key string in the android:value attribute of maps.v2.API_KEY by following the steps mentioned above.

按照上述步骤,在maps.v2.API_KEY的android:value属性中添加您自己的API密钥字符串。

Android Google Maps项目结构 (Android Google Maps Project Structure)

Android Google Maps示例代码 (Android Google Maps Example Code)

Keeping this application simple we’ve just added a SupportMapFragment tag inside content_main.xml as shown below:

为了简化该应用程序,我们刚刚在content_main.xml内添加了一个SupportMapFragment标记,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.journaldev.integratingmaps.MainActivity"
    tools:showIn="@layout/activity_main">

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_gravity="center"
        android:layout_height="match_parent"
        />

</RelativeLayout>

Add the following permission to the AndroidManifest.xml file.

将以下权限添加到AndroidManifest.xml文件。

<uses-permission android:name="android.permission.INTERNET"/>

Build and run this application on the emulator. The following outputs would be shown:

在模拟器上生成并运行此应用程序。 将显示以下输出:

Oops! Did we miss out something? No. This error occurs when the latest google play services version isn’t installed on the emulator. There are many workarounds to update to the latest versions like downloading and installing a play store apk using the command : adb install play_store_file.apk.

糟糕! 我们错过了什么吗? 否。如果模拟器上未安装最新的Google Play服务版本,则会发生此错误。 有许多变通办法可以更新到最新版本,例如使用以下命令下载和安装Play Store APK: adb install play_store_file.apk

We recommend a simpler workaround. Just go to Settings->Apps->Google Play Services and use that version number in the build.gradle in place of :

我们建议一个更简单的解决方法。 只需转到“设置”->“应用”->“ Google Play服务”,并在build.gradle中使用该版本号代替:

compile 'com.google.android.gms:play-services:8.3.0'

The following output would be shown. A default world map.

将显示以下输出。 默认的世界地图。

This brings an end to this tutorial. We’ll discuss and implement various features of google maps in later tutorials. Being a basic project with minimal differences from a default new project we’ve omitted the source code. Following are the major changes to be done to the default android studio project:

本教程到此结束。 我们将在以后的教程中讨论和实现Google地图的各种功能。 作为与默认新项目差异最小的基础项目,我们省略了源代码。 以下是对默认android studio项目要进行的主要更改:

  • Create a new project and replace the content_main.xml with the snippet shown above.

    创建一个新项目,并用上面显示的代码段替换content_main.xml。
  • Add the meta-data tags in the AndroidManifest.xml as shown above with the respective Google Map v2 API key.

    如上所示,使用相应的Google Map v2 API密钥将元数据标签添加到AndroidManifest.xml中。
  • Your first Google Map integrated application is ready!

    您的第一个Google Map集成应用程序已准备就绪!

翻译自: https://www.journaldev.com/10365/android-google-maps-api

maps-api-v3

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值