密钥申请成功后,就开始开发google map了。
任务:
1.建立google map 的项目
2.正确配置xml文件
3.类的编写
4.实现:显示地图、移动(pan)地图、缩放(zoom)地图。
一、建立项目
跟普通的项目一样,主要是选择“Build Target”的时候要选择 对应的google apis。相应的AVD也要选择apis。
二、配置xml
1.配置AndroidManifest.xml
在
<application>内添加:
<uses-libraryandroid:name="com.google.android.maps"/>
(--因为地图库不是安卓标准库的一部分,所以必须在此声明)
在<manifest>内(作子元素,不能在<application>内)添加:
<uses-permissionandroid:name="android.permission.INTERNET"/>
(--添加联网权限)
想要去掉标签栏,添加黑体那行:
<activityandroid:name=".HelloGoogleMaps"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">
android:theme="@android:style/Theme.NoTitleBar">
2.配置res/layout/main.xml
添加:
<com.google.android.maps.MapView
xmlns:android = "http://schemas.android.com/apk/res/android"
android:id = "@+id/mapview"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:clickable = "true"
android:apiKey = " Your Maps API Key goes here "
/>
xmlns:android = "http://schemas.android.com/apk/res/android"
android:id = "@+id/mapview"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:clickable = "true"
android:apiKey = " Your Maps API Key goes here "
/>
(--clickable="true"表示可以操作地图,android:apiKey 则要填写你申请的API Key)
三、类的编写
1.我们建立的activity是要继承MapActivity,而不是Activity。
2.添加两个必要的override(不需要手动输入,利用eclipse导入即可):
@Override
protected boolean isRouteDisplayed(){
return false;
}
protected boolean isRouteDisplayed(){
return false;
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
四、实现操作
1.显示地图
前面的步骤已经可以显示地图了。
2.移动地图
在main.xml中,clickable="true"就可以移动地图。
3.缩放地图
在onCreate方法里面添加:
MapView mapView= (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setBuiltInZoomControls(true);
完成!!