有多简单呢?看,只是显示了一下地图而已:
想编写
Android谷歌地图应用,准备工作比编写其他应用要麻烦一些。因为:
Android谷歌地图API,不是开源免费的,是谷歌的私有
软件,虽然是免费的;这个API,需要时刻依赖向谷歌下载地图信息。
那么第一条还比较好办。我这里用的是
Android 2.1,用其他版本比如1.5的,需要做的类似。需要在项目中导入
google map api,默认情况下是没有的。默认情况是
Android某个版本比如
Android 2.1,现在需要改为对应版本的google apis,版本要和
Android版本一致。这个google apis是同版本的
Android超集,包含了google的私有应用api。比如:
这样就可以在项目中使用比如:
com.google.
Android.maps.MapActivity
这还不够,google需要一个签名指纹的机制,要先到google
注册,并把这个指纹包含在应用中,才可以下载到地图信息。也就是说每次下载地图信息要带着这个指纹信息。
指纹信息的注册和获取都是免费的。
指纹有两种:
用于开发的debug指纹,只能使用在自己的debug应用程序里;正式的指纹。
这里只需要第一种就可以了。
操作步骤是,首先开发环境要有JDK,应该都有的吧。进入JDK的bin目录,执行:
会得到类似:
把指纹部分,复制下来。
然后访问:
http://code.google.com/intl/zh-CN/android/add-ons/google-apis/maps-api-signup.html这里还有个前提,就是你要有google帐号,并且登录。
把刚才的md5指纹,复制到红框位置:
并且勾选同意协议。
提交后,会看到:
其实主要是得到红框的密钥。然后在程序或者布局文件中,凡是用到MapView的地方,加入或者设置
AndroidLapiKey属性,就可以了。
代码其实很简单:
public class LocationActivity extends MapActivity {
private MapView mapView;
private MapController mapController;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.map_view);
Log.i("welcome", "created map activity.");
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
使用的布局文件:
<LinearLayout xmlns:
Android="http://schemas.
Android.com/apk/res/
Android"
Android:orientation="vertical"
Android:layout_width="fill_parent"
Android:layout_height="fill_parent">
<com.google.
Android.maps.MapView
Android:id="@+id/map_view"
Android:layout_width="fill_parent"
Android:layout_height="fill_parent"
Android:enabled="true"
Android:clickable="true"
Android:apiKey="xxxxxxxxxxxxxx" />
</LinearLayout>
摘自 虚怀若谷