如何使用android百度地图离线地图

时间:2024-03-09 22:37:36

1.首先把离线地图放在android工程下的assets里面。

注意:建议离线地图下载通过百度地图APIDEMO去下载,因为到官网上下载的离线地图文件格式不一样,APIDEMO的格式是.dat,而官网上的格式是.dat_svc有可能加载不了地图.

2.把离线地图从assets下拷贝到sd卡下

public static void copy(Context context) {
try {
String filepath = Environment.getExternalStorageDirectory() + "/"
+ "BaiduMapSDK" + "/" + "vmp" + "/" + "h" + "/"
+ "xxxxx.dat";
String path = Environment.getExternalStorageDirectory() + "/"
+ "BaiduMapSDK" + "/" + "vmp" + "/" + "h" + "/";
File file = new File(path);
if (!file.exists()) {
file.mkdir();

}
if (!(new File(filepath)).exists()) {

new File(filepath).createNewFile();

InputStream is = context.getAssets().open(
"xxxx.dat");
FileOutputStream fos = new FileOutputStream(filepath);
byte[] buffer = new byte[8192];
int count = 0;
while ((count = is.read(buffer)) > 0) {
fos.write(buffer, 0, count);
}
fos.close();
is.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}

3.初始化离线地图

private MKOfflineMap mOffline = null;
MapController mMapController;

mMapController = mMapView.getController();

mOffline = new MKOfflineMap();
mOffline.init(mMapController, new MKOfflineMapListener() {

@Override
public void onGetOfflineMapState(int type, int state) {
// TODO Auto-generated method stub

switch (type) {
case MKOfflineMap.TYPE_DOWNLOAD_UPDATE: {
}
break;
case MKOfflineMap.TYPE_NEW_OFFLINE:

break;

case MKOfflineMap.TYPE_VER_UPDATE:
break;
}

}
});