当前位置: 代码迷 >> Android >> 百度——mapAPI——Android—应用集成离线map
  详细解决方案

百度——mapAPI——Android—应用集成离线map

热度:154   发布时间:2016-04-28 07:57:44.0
百度——地图API——Android—应用集成离线地图

有离线地图,真的挺方便的。具体可以看看我的应用。

http://as.baidu.com/a/item?docid=4120392&pre=web_am_se

最近有网友不知道如何将离线的百度地图,打包在应用中发布。所以整理了这篇博客与大家分享。

 

1.使用离线地图。经过测试,首次必须在线验证百度的APIKey。

2.离线地图文件,放到res/raw目录下。参见下图。

3.编写代码拷贝函数。

package com.kkstudio.db;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import com.kkstudio.parkcar.R;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.os.Environment;import android.util.Log;public class DBReleasFromAPK {	/** Called when the activity is first created. */		private static String APK_PATHNAME = "parkcar";		// SD卡下的目录	private final String DATABASE_PATH = Environment.getExternalStorageDirectory().getPath() + "/" + APK_PATHNAME;		private final String MAP_FILENAME = "beijing_131.dat";	private final String MAP_FILEPATH = Environment.getExternalStorageDirectory().getPath() + "/BaiduMapSdk/vmp/h/";		// 数据库名	private final String DATABASE_FILENAME = "data.db";	private final String IMAGE_FILENAME = "defaultimg.jpg";	private final String DEFAULT_IMAGE_FILENAME = "default.jpg";		private final String DEFAULT_IMAGE_06FILENAME = "06.jpg";		// 这个context是必需的,没有context,怎么都不能实现数据库的拷贝操作;	private Context context;	// 构造函数必需传入Context,数据库的操作都带有这个参数的传入	public DBReleasFromAPK(Context ctx) {		this.context = ctx;//		APK_PATHNAME = this.context.getString(R.string.app_name);	}	public SQLiteDatabase OpenDataBase() {		try {			String imageFilename = DATABASE_PATH + "/" + IMAGE_FILENAME;			String defaultimageFilename = DATABASE_PATH + "/" + DEFAULT_IMAGE_FILENAME;			String default06imageFilename = DATABASE_PATH + "/" + DEFAULT_IMAGE_06FILENAME;						String mapFilename =  MAP_FILEPATH + MAP_FILENAME;						String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;			File dir = new File(DATABASE_PATH);			// 判断SD卡下是否存在存放数据库的目录,如果不存在,新建目录			if (!dir.exists()) {				dir.mkdir();				Log.i("ReleaseDataBaseActivity", "dir made:" + DATABASE_PATH);			} else {				Log.i("ReleaseDataBaseActivity", "dir exist:" + DATABASE_PATH);			}			try {				// 如果数据库已经在SD卡的目录下存在,那么不需要重新创建,否则创建文件,并拷贝/res/raw下面的数据库文件				if (!(new File(databaseFilename)).exists()) {					Log.i("ReleaseDataBaseActivity", "file not exist:"							+ databaseFilename);					// /res/raw数据库作为输出流					InputStream is = this.context.getResources()							.openRawResource(R.raw.data);					// 测试用					int size = is.available();					Log.i("ReleaseDataBaseActivity", "DATABASE_SIZE:" + 1);					Log.i("ReleaseDataBaseActivity", "count:" + 0);					// 用于存放数据库信息的数据流					FileOutputStream fos = new FileOutputStream(							databaseFilename);					byte[] buffer = new byte[8192];					int count = 0;					Log.i("ReleaseDataBaseActivity", "count:" + count);					// 把数据写入SD卡目录下					while ((count = is.read(buffer)) > 0) {						fos.write(buffer, 0, count);					}					fos.flush();					fos.close();					is.close();				}								// patch image file				if (!(new File(defaultimageFilename)).exists()) {					// /res/raw数据库作为输出流					InputStream is = this.context.getResources()							.openRawResource(R.raw.defaultimg);					// 测试用					int size = is.available();					// 用于存放数据库信息的数据流					FileOutputStream fos = new FileOutputStream(							defaultimageFilename);					byte[] buffer = new byte[8192];					int count = 0;										// 把数据写入SD卡目录下					while ((count = is.read(buffer)) > 0) {						fos.write(buffer, 0, count);					}					fos.flush();					fos.close();					is.close();				}								// patch 06.jpg				if (!(new File(default06imageFilename)).exists()) {					// /res/raw数据库作为输出流					InputStream is = this.context.getResources()							.openRawResource(R.raw.defaultimg);					// 测试用					int size = is.available();					// 用于存放数据库信息的数据流					FileOutputStream fos = new FileOutputStream(							default06imageFilename);					byte[] buffer = new byte[8192];					int count = 0;										// 把数据写入SD卡目录下					while ((count = is.read(buffer)) > 0) {						fos.write(buffer, 0, count);					}					fos.flush();					fos.close();					is.close();				}				// patch download beijing map				if (!(new File(mapFilename)).exists()) {					// /res/raw数据库作为输出流					InputStream is = this.context.getResources()							.openRawResource(R.raw.beijing_131);					// 测试用					int size = is.available();					// 用于存放数据库信息的数据流					FileOutputStream fos = new FileOutputStream(							mapFilename);					byte[] buffer = new byte[8192];					int count = 0;										// 把数据写入SD卡目录下					while ((count = is.read(buffer)) > 0) {						fos.write(buffer, 0, count);					}					fos.flush();					fos.close();					is.close();				}							} catch (FileNotFoundException e) {				Log.e("Database", "File not found");				e.printStackTrace();			} catch (IOException e) {				Log.e("Database", "IO exception");				e.printStackTrace();			}						// 实例化sd卡上得数据库,database作为返回值,是后面所有插入,删除,查询操作的借口。			SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(					databaseFilename, null);			return database;		} catch (Exception e) {		}				return null;	}}


4.启动类的onCreate中,调用即可。

	    DBReleasFromAPK dbapk = new DBReleasFromAPK(this);	    SQLiteDatabase dbsqlite = dbapk.OpenDataBase();


5.剩下的就是别忘记了。修改AndroidManifest.xml,添加存储功能。

6.祝你好运。微笑

  相关解决方案