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

高德地圖自定義樣式

首先,我們在高德地圖sdk中下載自定義地圖,並在build.gradle中引用。將下載的.data檔案放入assets資料夾下。關鍵來了,如何讀取自定義地圖。

private void setMapCustomStyleFile(Context context) {
    String styleName = "mystyle_sdk_1507863834_0100.data";
    FileOutputStream outputStream = null;
    InputStream inputStream = null;
    String filePath = null;
    try 
{ inputStream = context.getAssets().open(styleName); byte[] b = new byte[inputStream.available()]; inputStream.read(b); filePath = context.getFilesDir().getAbsolutePath(); File file = new File(filePath + "/" + styleName); if (file.exists()) { file.delete(); } file.createNewFile(); outputStream = new
FileOutputStream(file); outputStream.write(b); } catch (IOException e) { e.printStackTrace(); } finally { try { if (inputStream != null) inputStream.close(); if (outputStream != null) outputStream.close(); } catch
(IOException e) { e.printStackTrace(); } } aMap.setCustomMapStylePath(filePath + "/" + styleName); aMap.setMapCustomEnable(true); aMap.showMapText(true); }
在oncreat()方法中,
aMap = mMapView.getMap();這行程式碼之後即可。
.data檔案位置如下