Android使用GoogleMap v2(一)

使用之前的一些准备:

https://developers.google.com/maps/documentation/android/start#get_an_android_certificate_and_the_google_maps_api_key (官网的详细教程)

Creating a new Android application that uses the Google Maps Android API v2 requires several steps. Many of the steps outlined in this section will only have to be performed once, but some of the information will be a handy reference for future applications. The overall process of adding a map to an Android application is as follows:

  1. Install the Android SDK.
  2. Download and configure the Google Play services SDK, which includes the Google Maps Android API. If you use the Google Maps Mobile SDK for Businessyou must download and configure the Google Maps Mobile SDK for Business static library.
  3. Obtain an API key. To do this, you will need to register a project in the Google APIs Console, and get a signing certificate for your app.
  4. Add the required settings in your application's manifest.
  5. Add a map to your application.
  6. Publish your application.

  1. 安装Android SDK

  2. 下载并配置Google Play service SDK,包括Google Maps Android API.如果用于商业的话你必须下载配置 针对商业版的库

  3. 获取 APIkey,最先你得有一个Google账号吧,

  4. 在应用的manifest文件中添加一些权限,声明等

  5. 添加map到你的应用

  6.发布你的应用

一.先取出证书的指纹

(1)可以在Eclipse下的设置处查看, 

取得证书的SHA1,等一下有用处

(2)也可以使用工具来查看

在命令行下 

keytool -v -list -keystore C:Users<你的用户名称>.androiddebug.keystore 找到相对应文件的位置..默认密码是android,查看证书指纹

二.首先得申请一个Google Map Api key

最起码你得有一个Google账号,然后到这里https://code.google.com/apis/console/?noredirect#project:974575633588

  1. Navigate to your project in the Google APIs Console.
  2. In the left navigation bar, click API Access.
  3. In the resulting page, click Create New Android Key....
  4. In the resulting dialog, enter the SHA-1 fingerprint, then a semicolon, then your application's package name. For example:

    BB:0D:AC:74:D3:21:E1:43:67:71:9B:62:91:AF:A1:66:6E:44:5D:75;com.example.android.mapexample
  5. The Google APIs Console responds by displaying Key for Android apps (with certificates) followed by a forty-character API key, for example:

    AIzaSyBdVl-cTICSwYKrZ95SuvNw7dbMuDt1KG0

打开Google的控制台,如果你是第一次使用那么将会提示你先建立一个项目,然后点击API Access,找到Create New Android Key....创建一个android的key不是其他的..

然后输入前面获得的证书指纹加;你得包名    形式如:   BB:0D:AC:74:D3:21:E1:43:67:71:9B:62:91:AF:A1:66:6E:44:5D:75;com.example.android.mapexample

然后就好了,他会给你生成一个唯一标识的key,好了现在key就拿到手了.

还需在控制台开启一些特定的访问权限

三.配置项目信息

新建一个demo项目,在导入已存在的代码作为依赖库

<存放位置>sdkextrasgooglegoogle_play_serviceslibproject 导入,然后再属性处,Android节点下把那个作为库的钩选上即可

(1).在manifest中添加一些权限

1    <!-- 添加GoogleMap的最少权限 -->
2     <uses-permission android:name="android.permission.INTERNET" />
3     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
4     <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
5     <uses-permission android:name="android.permission.android.permission.WRITE_EXTERNAL_STORAGE" />
6 
7     <!-- 可能用到的权限 -->
8     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
9     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

(2).在根节点下添加下面的代码

1  <!-- 使用OpenGL ES version2声明 -->
2     <uses-feature
3         android:glEsVersion="0x00020000"
4         android:required="true" />

(3).在application节点下添加,在</application>之前

1 <!-- 服务组件声明 -->
2         <meta-data
3             android:name="com.google.android.gms.version"
4             android:value="@integer/google_play_services_version" />
5 
6         <!-- 声明google map api key -->
7         <meta-data
8             android:name="com.google.android.maps.v2.API_KEY"
9             android:value="前面得到key" />

(四).测试一下配置是否成功

 下面这个例子的平台是 针对api 12 以上的    (Android API 12 or later)

最简单的测试方法

用这段代码覆盖到原来的layout文件

1 <?xml version="1.0" encoding="utf-8"?>
2 <fragment xmlns:android="http://schemas.android.com/apk/res/android"
3           android:id="@+id/map"
4           android:layout_width="match_parent"
5           android:layout_height="match_parent"
6           android:name="com.google.android.gms.maps.MapFragment"/>

然后运行..查看是否正常..

可能出现的问题:

1.提示"您的设备不支持部分应用依赖的Google Play服务...",这是因为没有安装Google服务框架导致的

2.启动就出现报错,有可能是因为使用的平台API低于12,也就是低于android3.1

3.不能运行,可能是没有导入 Support V4 包,或者前面没有导入其依赖库,导入即可

也可以使用官方的Sample

同样,使用之前先导入依赖库,然后导入Sample项目,只需把manifest中的那个key替换成你的就行了,

可能出错的地方,因为前面在申请key的时候使用的包名和Sample的包名不同,重构一下包名即可

参考文章:http://www.blogjava.net/xmlspy/articles/393726.html

            https://developers.google.com/maps/documentation/android/start#obtain_a_google_maps_api_key

如有雷同,纯属意外! good good study,day day up! go,go,go!
原文地址:https://www.cnblogs.com/act262/p/3561402.html