1. 程式人生 > >Android實現高德地圖自定義樣式

Android實現高德地圖自定義樣式

放置Android工程下的assets資料夾,在assets資料夾裡面建立了一個styleMap子資料夾。將裡面的檔案寫到sd卡中。

寫出檔案程式碼:

try {

        // 先獲取系統預設的文件存放根目錄
        File parent_path = Environment.getExternalStorageDirectory();
        File dir = new File(parent_path.getAbsoluteFile(), "data");
        if(!dir.exists()){
            dir.mkdir();
        }
        File file = new File(dir.getAbsoluteFile(), "style.data");
        if(file.exists()){
            return;
        }
        //讀取資料檔案
        InputStream open = this.getResources().getAssets().open("styleMap/style.data");

        file.createNewFile();
        FileOutputStream fos = new FileOutputStream(file);
        int len;
        byte[] buf = new byte[1024];
        while((len=open.read(buf))!=-1){
            fos.write(buf,0,len);
        }
        fos.flush();
        fos.close();

    } catch (IOException e) {
        e.printStackTrace();
    }

在高德地圖中指定你寫出檔案的路徑。

String path=Environment.getExternalStoragePublicDirectory("data").getPath()+"/style.data"
//該方法在AMap類中提供
setCustomMapStylePath(path);