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

基于Google Map的简单android应用开发(图解)

一、准备工作

1.        申请Android Map API Key

必要条件:google账号以及系统的证明书。

首先找到我们的debug.keystore文件,如果您已经安装了eclipse,并且配置好了android的开发环境(这里不再重复环境的配置,前面的博客有详细指导),可以通过Window -> Preference -> Android ->Build,我们可以看到Default debug keystore便是debug.keystore的路径。

  \


 

接下来我们要取得MD5的值,打开命令行,进入debug.keystore所在的目录下,执行命令keytool -list -keystore debug.keystore,这里会让你输入keystore密码,默认是android。


  \


 

接着我们要申请Android Map的API Key,打开网址:http://code.google.com/intl/zh-CN/android/maps-api-signup.html,登陆你的google账号,输入上步得到的MD5,生成API Key。


  \


 

1.        创建基于Google APIs的AVD

Window -> AVD Manager->new,输入AVD的名字,在Target中选择Google APIs。

  \

 

这里需要注意的是,如果在Target选项中没有Google APIs的选项,需要到Android SDK Manager中安装Google APIs。


  \


 

一、创建简单基于GoogleAPIs的应用

1.        创建新的工程

前面跟创建普通android应用一样,File -> new ->other -> Android Project,我们给工程命名googleMapApp,这里要注意的是,选择Target的时候要选择Google APIs。


  \

 

1.        必要的修改

打开AndroidManifest.xml文件,由于要使用Google Map APIs必须定义下面这句:

<uses-libraryandroid:name="com.google.android.maps" />

由于我们还要用到网络,所以还要添加网络访问许可<uses-permissionandroid:name="android.permission.INTERNET"/>,如果不添加网络许可,应用程序就不会显示地图,只显示一下网格线。

其次要在布局文件main.xml中添加MapView属性,代码如下:

 


[html] <com.google.android.maps.MapView 
                    android:id="@+id/mapView" 
                 android:layout_width="fill_parent" 
                 android:layout_height="fill_parent" 
                 android:apiKey="0DXjJ7k6Ul6gx2s4aQEbs8Chg43eW-dVeowPqIQ" 
                 /> 
<com.google.android.maps.MapView
        android:id="@+id/mapView"
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent"
                 android:apiKey="0DXjJ7k6Ul6gx2s4aQEbs8Chg43eW-dVeowPqIQ"
                 />

 

 

其中的android:apiKey为登陆google账号输入MD5生成的API Key,这里注意不要和MD5混淆!

类GoogleMapAppActivity要继承MapActivity而不是Activity。具体代码如下:

 


[java] public class GoogleMapAppActivity extends MapActivity { 
    public MapView mapView; 
    public MapController mapController; 
    public GeoPoint geoPoint; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
         
        mapView = (MapView)findViewById(R.id.mapView); 
        mapView.setTraffic(true);//设置为交通模式          
        mapView.setClickable(true); 
        mapView.setBuiltInZoomControls(true);//设置可以缩放  
         
        mapController = mapView.getController(); 
        geoPoint = new GeoPoint((int)40.38014*1000000,(int)117.00021*1000000); //设置起点为北京附近  
        mapController.animateTo(geoPoint);//定位到北京  
        mapController.setZoom(12); 
    } 
 
    @Override 
    protected boolean isRouteDisplayed() { 
        return false; 
    } 
 

 \



摘自  pku_android
 

 


 

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