官术网_书友最值得收藏!

Adding permissions and defining the API key

The permissions and API key must be defined in the AndroidManifest.xml file, which provides essential information about applications to an operating system. The OpenGL ES version must be specified in manifest, which is then required to render the map and also different versions of Google Play services.

Adding permissions

Three permissions are required for our map application to work properly.

The permissions should be added inside the <manifest> element. The four permissions are as follows:

  • INTERNET
  • ACCESS_NETWORK_STATE
  • WRITE_EXTERNAL_STORAGE
  • READ_GSERVICES

Let's now take a look at what these permissions are for.

INTERNET

This permission is required for our application to gain access to the Internet. Since Google Maps mainly works on real-time Internet access, access to the Internet is essential.

ACCESS_NETWORK_STATE

This permission gives information about networks and whether we are connected to a network or not.

WRITE_EXTERNAL_STORAGE

This permission is required to write data to an external storage. In our application, it is required to cache map data to an external storage.

READ_GSERVICES

This gives permission to read Google services.

The permissions are added to AndroidManifest.xml as follows:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

There are some more permissions that are currently not required. We will learn about these in later chapters.

Specifying the Google Play services version

The Google Play services version must be specified in manifest for the functioning of maps. It must be within the <application> element.

Add the following code to AndroidManifest.xml:

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

Specifying the version 2 of OpenGL ES

Android Google maps uses OpenGL to render the map. Google maps will not work on devices that do not support version 2 of OpenGL. Hence, it is necessary to specify this in the manifest.

It must be added within the <manifest> element similar to permissions. Add the following code to AndroidManifest.xml:

<uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

The preceding code specifies that version 2 of OpenGL is required for the functioning of our application.

Defining the API key

The Google maps API key is required to provide authorization to the Google maps service. It must be specified within the <application> element.

Add the following code to AndroidManifest.xml:

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

The API_KEY value must be replaced with the API key we noted earlier in the Google Developer Console.

The complete AndroidManifest structure after adding permissions, specifying OpenGL, the Google Play services version, and defining the API key, is given as follows:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.raj.sampleapplication"
    android:versionCode="1"
    android:versionName="1.0" >


    <uses-feature
            android:glEsVersion="0x00020000"
          android:required="true"/>


    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission     android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />



   <application>
   
   
   
   <meta-data android:name="com.google.android.gms.version"
              android:value="@integer/google_play_services_version" />
   <meta-data
                 android:name="com.google.android.maps.v2.API_KEY"
                  android:value="AIzaSyBVMWTLk4uKcXSHBJTzrxsrPNSjfL18lk0"/>
   
   
   </application>
</manifest>
主站蜘蛛池模板: 达州市| 延寿县| 乌兰浩特市| 安岳县| 乌审旗| 安义县| 吉水县| 蓬安县| 张家界市| 锡林郭勒盟| 孝义市| 木兰县| 石景山区| 寿阳县| 双江| 左云县| 拉孜县| 恩平市| 阳原县| 凤山市| 白河县| 庆元县| 凭祥市| 依兰县| 开封市| 平顺县| 肥西县| 新郑市| 陆河县| 民乐县| 郯城县| 依安县| 会昌县| 龙州县| 临桂县| 望奎县| 平和县| 怀化市| 墨脱县| 仪陇县| 重庆市|