1. 首先先要获取你的debug keystore位置:
打开eclipse--->windows---> preferences--->android--->build
查看默认的debug keystore位置,我的是c:\documents and settings\administrator\.android\debug.keystore
2.
c:\program files\java\jdk1.6.0_04\jre\bin>keytool -list -alias androiddebugkey -keystore "c:\documents and settings\administrator\.android\debug.keystore" -storepass android -keypass android
androiddebugkey, 2009-7-25, privatekeyentry,
认证指纹 (md5): da:d5:6e:c2:80:d1:0f:0d:f8:2a:58:6a:74:7c:ce:2d
3.
填入你的认证指纹(md5)即可获得apikey了,结果显示如下:
感谢您注册 android 地图 api 密钥!
您的密钥是:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
4. 使用得到的apikey:
在layout中加入mapview
<com.google.android.maps.mapview
android:id="@+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apikey="0jokq80od1jl9c6haja99ugxcris2cgjko_bc_g" />
或者
<view android:id="@+id/mv"
class="com.google.android.maps.mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:apikey="0jokq80od1jl9c6haja99ugxcris2cgjko_bc_g"
/>
5.android在sdk1.5的预装的add-on中提供了一个map扩展库com.google.android.maps,利用它你就可以给你的android应用程序增加上强大的地图功能了。这个库的位置在your-sdk_dir\add-ons\google_apis-3\libs.
一定要在manifest.xml中设置全相应的权限,比如:
<uses-permission android:name="android.permission.access_coarse_location" />
<uses-permission android:name="android.permission.internet" />
要在manifest.xml中加上要用的maps库:
package="com.example.package.name">
...
<application android:name="myapplication" >
<uses-library android:name="com.google.android.maps" />
...
</application>
</manifest>
需要说明的是这个库不是标准的android sdk的内容,你也可以自己从这里这里下载并放到你的sdk里面。
maps库分析
mapview是用来显示地图的view, 它也是派生自android.view.viewgroup。
地图可以以不同的形式来显示出来,如街景模式,卫星模式等,具体方法可以参考:
setsatellite(boolean), settraffic(boolean), and setstreetview(boolean)
mapview只能被mapactivity来创建,这是因为mapview需要通过后台的线程来连接网络或者文件系统,而这些线程要由mapactivity来管理。
需要特别说明的一点是,android 1.5中,map的zoom采用了built-in机制,可以通过setbuiltinzoomcontrols(boolean)来设置是否在地图上显示zoom控件。
mapactivity是一个抽象类,任何想要显示mapview的activity都需要派生自mapactivity。并且在其派生类的oncreate()中,都要创建一个mapview实例,可以通过mapview constructor (then add it to a layout view with viewgroup.addview(view)) 或者通过layout xml来创建。
实例分析
最后,按照惯例,还是用一个小程序来演示一下android中地图功能的开发。主要功能是实现了地图的缩放,添加了菜单,从而可以手动选择地图的显示模式等。
step 1: 新建一个android project, 注意这里要选择的build target为"google apis"
step 2: 修改menifest文件:
<?xml version="1.0" encoding="utf-8"?>
package="com.map.prac"
android:versioncode="1"
android:versionname="1.0">
<uses-permission android:name="android.permission.access_coarse_location" />
<uses-permission android:name="android.permission.internet" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".mapviewprac2"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.main" />
<category android:name="android.intent.category.launcher" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minsdkversion="3" />
step 3: 修改layout文件,main.xml
android:id="@+id/main"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.mapview
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apikey="???????????????????????????????????"
/>
</linearlayout>
这里需要将api key中的????????????改成你自己申请到的api key.
step 4: 修改代码:
package com.map.prac;
import com.google.android.maps.geopoint;
import com.google.android.maps.mapactivity;
import com.google.android.maps.mapcontroller;
import com.google.android.maps.mapview;
import android.app.alertdialog;
import android.app.dialog;
import android.content.dialoginterface;
import android.os.bundle;
import android.util.log;
import android.view.keyevent;
import android.view.menu;
import android.view.menuitem;
public class mapviewprac2 extends mapactivity {
private final string tag = "mapprac";
private mapview mapview = null;
private mapcontroller mc;
//menu items
final private int menumode = menu.first;
final private int menuexit = menu.first+1;
final private int menucommandlist = menu.first + 2;
private int chooseitem = 0;
/** 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.map);
mc = mapview.getcontroller();
mapview.settraffic(true); //
mapview.setsatellite(false);
mapview.setstreetview(true);
//geopoint gp = new geopoint((int)(39.269259 * 1000000), (int)(115.255762 * 1000000));//yixian
geopoint gp = new geopoint((int)(39.95 * 1000000), (int)(116.37 * 1000000));//beijing
//mc.animateto(gp);
//mc.setzoom(12);
mc.setcenter(gp);
//to display zoom control in mapview
mapview.setbuiltinzoomcontrols(true);
}
public boolean onkeydown(int keycode, keyevent event) {
// todo auto-generated method stub
log.i(tag,"enter onkeydown");
return super.onkeydown(keycode, event);
public boolean oncreateoptionsmenu(menu menu) {
menu.add(0, menumode, 0, "map mode");
menu.add(0, menucommandlist, 1, "command list");
menu.add(0, menuexit, 2, "exit");
return super.oncreateoptionsmenu(menu);
public boolean onmenuitemselected(int featureid, menuitem item) {
switch(item.getitemid())
{
case menumode:
dialog dmode = new alertdialog.builder(this)
//.seticon(r.drawable.alert_dialog_icon)
.settitle(r.string.alert_dialog_single_choice)
.setsinglechoiceitems(r.array.select_dialog_items2, chooseitem, new dialoginterface.onclicklistener() {
public void onclick(dialoginterface dialog, int whichbutton) {
log.i(tag, "choose button is "+ whichbutton);
chooseitem = whichbutton;
/* user clicked on a radio button do some stuff */
}
})
.setpositivebutton(r.string.alert_dialog_ok, new dialoginterface.onclicklistener() {
/* user clicked yes so do some stuff */
log.i(tag,"item = "+chooseitem);
switch(chooseitem)
{
case 0:
mapview.setsatellite(false);
break;
case 1:
mapview.setsatellite(true);
case 2:
mapview.settraffic(true);
case 3:
mapview.setstreetview(true);
default:
}
.setnegativebutton(r.string.alert_dialog_cancel, new dialoginterface.onclicklistener() {
/* user clicked no so do some stuff */
.create();
dmode.show();
break;
case menucommandlist:
//create the dialog
dialog d = new alertdialog.builder(this)
.settitle(r.string.select_dialog)
.setitems(r.array.select_dialog_items, new dialoginterface.onclicklistener() {
public void onclick(dialoginterface dialog, int which) {
/* user clicked so do some stuff */
string[] items = getresources().getstringarray(r.array.select_dialog_items);
/*new alertdialog.builder(this)
.setmessage("you selected: " + which + " , " + items[which])
.show();*/
log.i(tag,"you choose is: " + items[which]);
.create();
//show the dialog
d.show();
case menuexit:
finish();
default:
break;
}
return super.onmenuitemselected(featureid, item);
protected boolean isroutedisplayed() {
return false;
}