高德地圖自定義樣式
阿新 • • 發佈:2019-02-15
首先,我們在高德地圖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在oncreat()方法中,{ 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 = newFileOutputStream(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); }
aMap = mMapView.getMap();這行程式碼之後即可。
.data檔案位置如下