Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 百度地圖離線地圖使用流程

編輯:關於 Android 程式設計

使用百度地圖離線地圖時,建議先從官方 Demo 下載 .dat 檔案,再放入 Android 工程的 assets 目錄。若檔案格式不一致,後續初始化可能無法載入地圖。

複製離線地圖到裝置

應用啟動時,可將 assets 中的離線地圖複製到外部儲存空間的 BaiduMapSDK 目錄。複製前先建立資料夾,並以緩衝區串流寫入,避免一次性載入過大檔案。

public static void copy(Context context) {
    String dir = Environment.getExternalStorageDirectory() + "/BaiduMapSDK/vmp/h/";
    String target = dir + "xxxxx.dat";
    File folder = new File(dir);
    if (!folder.exists()) {
        folder.mkdirs();
    }
    try {
        InputStream is = context.getAssets().open("xxxx.dat");
        FileOutputStream fos = new FileOutputStream(target);
        byte[] buffer = new byte[8192];
        int count;
        while ((count = is.read(buffer)) > 0) {
            fos.write(buffer, 0, count);
        }
        fos.close();
        is.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

初始化離線地圖

取得 MapController 後,建立 MKOfflineMap 並呼叫 init,註冊 MKOfflineMapListener 以接收下載、更新與版本狀態。

MKOfflineMap mOffline = new MKOfflineMap();
mOffline.init(mMapController, new MKOfflineMapListener() {
    public void onGetOfflineMapState(int type, int state) {
        // 依 type 與 state 處理下載或更新狀態
    }
});
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved