您的位置:首页 > 移动开发 > Android开发

Google Maps Android API V2使用及问题解决

2013-09-28 16:51 591 查看
摘要:Google Maps Android API 在2012年12月更新,其V2版摒弃了MapActivity,而采用MapFragment,从而可以更好的在Android中使用地图组件。本文详细介绍了如何使用这一新特性。

说明

因为Google Maps的API版本更新,之前的一些教程都是关于旧版本V1的,虽然Google说继续提供服务,但是不再提供API Key的申请。

而新的V2版本貌似改动还挺大。也没搜到国内有什么系统介绍的博客文章之类的(书肯定是来不及那么新了)。

断断续续折腾了大概半个月,因为对Android也不是特别熟悉,所以碰到这样那样的问题。

终于在昨天看见模拟器上跑的地图了。太感人了。

下面就主要说说要成功做成这一件事的流程吧。

因为肯定有时效性,所以打上一个时间戳:版本更新事件发生在2012年12月,而这篇博文目前的时间是2013年1月1日。

背景

一些相关的链接:

Google Maps Android API V1的介绍:
https://developers.google.com/maps/documentation/android/v1/mapkey?hl=zh-CN
Google Maps Android API v2的初步介绍:
https://developers.google.com/maps/documentation/android/

Introduction

https://developers.google.com/maps/documentation/android/intro

Getting Started

(本部分参考https://developers.google.com/maps/documentation/android/start)

1.首先安装Google Play services SDK

Google Maps Android API是作为这个SDK的一部分发行的。

这个安装是通过Android SDK Manager进行,配置好之后的Eclipse上面应该有Android SDK Manager的图标,一般的SDK版本安装和更新都在这里进行。

安装和更新Extras下的Google Play services即可。

2.获取API key

获取Maps API key需要两样东西:应用的signing certificate和它的package name。

获取这个key之后,把它加在应用程序的AndroidManifest.xml文件里即可。

为应用获取一个key还是需要好几个步骤的,下面详细说明:

获取数字证书(digital certificate)信息

数字证书有Debug和Release两种,下面主要说Debug的。

要获取一个叫做SHA-1 fingerprint的东西,作为数字证书的一个简短代表。

这个指纹(fingerprint)是通过一个哈希算法得到的字符串,为了得到你的证书的SHA-1 fingerprint,首先要找到你的debug keystore 文件,文件名叫debug.keystore。

默认情况下它和虚拟机AVD存放在一起,win7下的路径是:C:\Users\your_user_name\.android\,也可以通过Eclipse中的Windows > Prefs > Android > Build来查看这个路径。

然后,在cmd命令行里运行下列命令:

keytool -list -v -keystore "C:\Users\your_user_name\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android

就显示一大堆东西,其中就有证书指纹:



SHA1那一行就包含了证书的SHA-1 fingerprint,是二十段用冒号割开的数字段,每段是两个十六进制的数。

在Google APIs Console上创建API Project

在Google APIs Console上创建项目,并且注册Maps API。

首先,去这个网址:https://code.google.com/apis/console/

用Gmail的账户登录,如果是第一次的话,需要创建项目,默认情况会创建一个叫做API Project的项目。

点击左边的Services,会在中间看到很多的APIs和Services,找到Google Maps Android API v2,然后把它设置成on,需要接受一些服务条款。

获得API Key

在左边的导航条中选择API Access。

在出来的页面中选择Create New Android Key...就可以生成key了:



然后在对话框中填入:SHA-1 指纹, 分号隔开,然后是应用的 package name.然后就会生成一个Key。

比如:



3.把API Key加入应用程序

首先,建立虚拟设备AVD和应用程序。

关于AVD,官方文档并没详细介绍,我后面会有说明。

建立好应用程序,注意包名应该和申请key时候的包名一致。

之后修改AndroidManifest.xml文件:

3.1.在元素中加入子标签

<meta-data

android:name="com.google.android.maps.v2.API_KEY"

android:value="your_api_key"/>

3.2.加入一些许可信息

<permission
android:name="com.example.mapdemo.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE"/>

其中com.example.mapdemo换成自己的包名。

4. AndroidManifest.xml中的其他具体设置

许可设置

作为 的子元素,需要加入下列一些:

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

OpenGL ES V2特性支持

同样也是作为 的子元素。

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

5.加上地图

首先布局文件:

xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@ id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment"/>

然后在MainActivity.java:

MainActivity.java

package com.example.mapdemo;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}

遇到的问题和解决的方法

程序编译错误,显示找不到一些类

如图:



解决这个问题,首先需要把Google Play services的类库加载进来:

在Eclipse里面选择:File > Import > Android > Existing Android Code Into Workspace然后点击Next.

之后Browse..., 找到路径下的/extras/google/google_play_services /libproject/google-play-services_lib, 然后选择Finish。

第二步是添加对这个库的引用:

在自己的项目上右键,选Properties,左边选Android,然后在下面的Library里面Add刚才的google-play-services_lib。



之后程序就应该能运行了。

接着你可能会碰到下面的问题:

程序运行成功,但是显示This app won"t run unless you update Google Play services.

如图:



有传言说V2不能在AVD上运行,可能Google还会对此问题进行更新。

经过搜索,这个问题已经在Stackoverflow上被讨论过了,链接

所以看来在AVD上运行的问题已经被解决了。

解决的方法就是在AVD上安装两个包:vending.apk和gms.apk,(给一个网盘链接)

并且AVD就选择普通的API 16就行,不需要是Google APIs。我选的是Android4.1 API16.

安装时把那两个包放在当前目录,用命令行安装:



之后运行程序,就出地图了:



现在,这个程序可以在手机中运行了(不能运行在模拟器中).如果手机中没有安装Goole Play Services和Google Map,程序会崩溃,在Android Logcat视图中可以看到提示相关东西没有安装.这种方式非常不友好,我们应该添加一些代码,提示用户安装安装缺少的东西.

在MainActivity.java中的最下面添加如下代码:

//=======  For Google Maps Check============

public boolean isGoogleMapsInstalled() {
try {
ApplicationInfo info = getPackageManager().getApplicationInfo("com.google.android.apps.maps", 0);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}

public OnClickListener getGoogleMapsListener() {
return new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("market://details?id=com.google.android.apps.maps"));
startActivity(intent);

//Finish the activity so they can't circumvent the check
finish();
}
};
}


然后在onCreate方法中添加一些代码,如下:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ListAdapter adapter = new CustomArrayAdapter(this, demos);

setListAdapter(adapter);

//=======  For Google Maps Check============
if (!this.isGoogleMapsInstalled()) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Install Google Map ?");
builder.setCancelable(false);
builder.setPositiveButton("Install", getGoogleMapsListener());
AlertDialog dialog = builder.create();
dialog.show();
}
}


现在再运行,就会提示用户安装缺少的内容.

本文转自:http://mobile.cnw.com.cn/exp/android/htm2013/20130417_267773.shtml
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: