Android使用GoogleMap v2(一)

摘要:
但其中一些信息将在今后的应用中被引用。将地图添加到Android应用程序的整个过程如下:然后转到此处https://nordirect#project:

使用之前的一些准备:

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下的设置处查看,Android使用GoogleMap v2(一)第1张 

取得证书的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就拿到手了.

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

Android使用GoogleMap v2(一)第2张

三.配置项目信息

新建一个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

免责声明:文章转载自《Android使用GoogleMap v2(一)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇MOS管体二极管的作用任务切换下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

关于nodejs中遇到mysql默认8小时连接断开机制的终极简单解决方案

由于mysql默认8小时连接无访问,就会断开.为此查了一下资料,有同种比较简单的解决方案: 1. 增加 MySQL 的 wait_timeout 属性的值。  修改 /etc/mysql/my.cnf文件,在 [mysqld] 节中设置: # Set a connection to wait 8hours in idle status.  wait_tim...

Hibernate环境搭建

1、导入jar包 hibernate.jar  antlr-2.7.6.jar  commons-collections-3.1.jar  jta-1.1.jar  dom4j-1.6.1.jar javassist-3.4.GA.jar  slf4j-api-1.5.2.jar  slf4j-log4j12-1.5.2.jar  假设使用C3P0数据源...

launcher- 第三方应用图标替换

有时候我们感觉第三方应用的icon不美观,或者跟我们主题风格不一致,这时候我们希望换成我们想要的icon,那我们可以这么做(以更换QQ应用icon为例): 1.首先我们当然要根据自己的需要做一张替换icon了(图片我们不妨命名为qq) 2.接下来我们需要得到第三方应用的信息,可以通过GetDftlayoutXml.apk 工具获得 具体步骤如下 1)网上...

ES7学习笔记(十二)高亮 和 搜索建议

ES当中大部分的内容都已经学习完了,今天呢算是对前面内容的查漏补缺,把ES中非常实用的功能整理一下,在以后的项目开发中,这些功能肯定是对你的项目加分的,我们来看看吧。 高亮 高亮在搜索功能中是十分重要的,我们希望搜索的内容在搜索结果中重点突出,让用户聚焦在搜索的内容上。我们看看在ES当中是怎么实现高亮的,我们还用之前的索引ik_index,前面的章节,我们...

Qt 事件总结(鼠标、键盘) QMouseEvent、QKeyEvent

Qt 程序需要在main()函数创建一个QCoreApplication对象,然后调用它的exec()函数。这个函数就是开始 Qt 的事件循环。在执行 exec()函数之后,程序将进入事件循环来监听应用程序的事件。当事件发生时,Qt 将创建一个事件对象。Qt 中所有事件类都继承于 QEvent。在事件对象创建完毕后,Qt 将这个事件对象传递给QObject...

谈java之GUI与安卓

首先说说swing的特点: 1.拥有一个丰富,快捷的用户界面元素集合。 2.与运行平台的依赖性很小,因此bug很少。 3.对于不同的运行平台,可以给用户一致的感觉。但是所制作的用户界面与本地运行平台有很大的差别。 java的图像用户界面并不是他的强项,不管是最初的awt,还是后来的swing都是同样的,不过对于目前我个人安卓的学习来说,尽管是用了xml去写...