当前位置:操作系统 > 安卓/Android >>

(android基础)之编写最简单的android谷歌地图应用

 有多简单呢?看,只是显示了一下地图而已:

想编写android谷歌地图应用,准备工作比编写其他应用要麻烦一些。因为:
  1. android谷歌地图API,不是开源免费的,是谷歌的私有软件,虽然是免费的;
  2. 这个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注册,并把这个指纹包含在应用中,才可以下载到地图信息。也就是说每次下载地图信息要带着这个指纹信息。
指纹信息的注册和获取都是免费的。
指纹有两种:
  1. 用于开发的debug指纹,只能使用在自己的debug应用程序里;
  2. 正式的指纹。
这里只需要第一种就可以了。
操作步骤是,首先开发环境要有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>
 

摘自 虚怀若谷
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,