其实百度给放出来的Demo已经很详细了,但是还是会有一些细节可能会被忽略导致程序无法运行。那么我再尽可能的补充一些细节吧。
1、显示地图前的准备
当前版本为:BaiduMap_AndroidSDK_v3.6.0_Lib.zip
3.6.0的版本:http://developer.baidu.com/map/index.php
jar包的介绍:
自V3.6.0起,Android SDK采用功能包拆分的形式,其中:
baidumapapi_base_vX_X_X.jar和libBaiduMapSDK_base_vX_X_X.so为基础包,使用地图、检索、云检索、工具、周边雷达中任何一功能都必须包含;
baidumapapi_map_vX_X_X.jar和libBaiduMapSDK_map_vX_X_X.so为地图功能包;
baidumapapi_search_vX_X_X.jar和libBaiduMapSDK_search_vX_X_X.so为检索功能包;
baidumapapi_cloud_vX_X_X.jar和libBaiduMapSDK_cloud_vX_X_X.so为云检索功能包;
baidumapapi_util_vX_X_X.jar和libBaiduMapSDK_util_vX_X_X.so为工具功能包;
baidumapapi_radar_vX_X_X.jar和libBaiduMapSDK_radar_vX_X_X.so为周边雷达工具包;
大家看需要导入不同的jar包,我直接全部导入进去了。
导入jar包后,记得也要把相应的so文件也导入进去,不然无法正常工作哦。
如图:
2、分析AndroidManifest.xml的配置
记得添加申请的密钥。...>
"com.baidu.lbsapi.API_KEY"
android:value="更换密钥" />
.......
.......
记得添加服务。...>
.....
.....
"com.baidu.location.f"
android:enabled="true"
android:process=":remote" >
添加权限<manifest ...>
....
....
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
uses-permission>
<uses-permission android:name="android.permission.INTERNET" >
uses-permission>
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
<uses-permission android:name="android.permission.WAKE_LOCK" >
uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
uses-permission>
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION">uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION">uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE">uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE">uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE">uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">uses-permission>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS">uses-permission>
<uses-permission android:name="android.permission.READ_LOGS">uses-permission>
manifest>
添加屏幕尺寸信息...>
....
....
"true"
android:largeScreens="true"
android:normalScreens="false"
android:resizeable="true"
android:smallScreens="true" />
3、构建基本的地图
接着我们构建一个基本的地图,我们只需要把百度地图的DEMO中的BaseMapDemo.java中的内容拷贝到我们的代码中就可以了。
例子:
package com.sangbo.bdmapdemo;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMapOptions;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.model.LatLng;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends Activity {
@SuppressWarnings("unused")
private MapView mMapView;
private BaiduMap mBaiduMap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
if (intent.hasExtra("x") && intent.hasExtra("y")) {
// 当用intent参数时,设置中心点为指定点
Bundle b = intent.getExtras();
LatLng p = new LatLng(b.getDouble("y"), b.getDouble("x"));
mMapView = new MapView(this,
new BaiduMapOptions().mapStatus(new MapStatus.Builder()
.target(p).build()));
} else {
mMapView = new MapView(this, new BaiduMapOptions());
}
setContentView(mMapView);
mBaiduMap = mMapView.getMap();
}
@Override
protected void onPause() {
super.onPause();
// activity 暂停时同时暂停地图控件
mMapView.onPause();
}
@Override
protected void onResume() {
super.onResume();
// activity 恢复时同时恢复地图控件
mMapView.onResume();
}
@Override
protected void onDestroy() {
super.onDestroy();
// activity 销毁时同时销毁地图控件
mMapView.onDestroy();
}
}
直接运行会报错,提示:
at com.baidu.mapapi.SDKInitializer.initialize(Unknown Source)
Caused by: java.lang.RuntimeException: context must be an Application Context
嗯,我们还没进行初始化操作。
在AndroidManifest.xml中你会发现:
"baidumapsdk.demo.DemoApplication"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:hardwareAccelerated="true" >
android:name="baidumapsdk.demo.DemoApplication"
这里初始化了一个Application
我们也写一个
package com.sangbo.bdmapdemo;
import com.baidu.mapapi.SDKInitializer;
import android.app.Application;
public class MyAppApplication extends Application{
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
// 在使用 SDK 各组间之前初始化 context 信息,传入 ApplicationContext
SDKInitializer.initialize(this);
}
}
然后在AndroidManifest.xml中添加
"com.sangbo.bdmapdemo.MyAppApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >