工作需要,看了下百度個性化地圖,參照開發文檔修改了下,發現一些小的細節容易被忽略,導緻個性化地圖設定,不起作用,下面我針對Fragment和Activity中接入進行簡單介紹
先看下預設主題顯示效果圖,友善大家對照

1、官方文檔接入說明
告訴我你看懂了嗎,反正我沒看懂
2、下面介紹用我的方式進行介紹
設定個性化地圖,一定要保證百度相關SDK及so庫已經接入,不懂的可以參照我之前的文章
A:
建立assets檔案夾,并在該檔案夾中建立子檔案夾customConfigdir用來存放地圖顔色配置檔案,如圖:
檔案夾中放了三個.json檔案,其實就是三種顔色配置檔案,對應黑夜主題、清新藍主題和午夜藍主題
B:activity中接入
在onCreate()方法中接入
//午夜藍路徑,其實就是assets中配置檔案名
private static String PATH = "custom_config_nidnight_blue.json";
@Override
protected void onCreate(Bundle savedInstanceState) {
//設定個性化地圖,一定要在父類構造方法前
FileUtil.setMapCustomFile(this,PATH);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//設定開啟個性化地圖
MapView.setMapCustomEnable(true);
設定配置檔案路徑工具類
/**
*
* 設定個性化地圖config檔案路徑
*/
public static void setMapCustomFile(Context context, String PATH) {
FileOutputStream out = null;
InputStream inputStream = null;
String moduleName = null;
try {
inputStream = context.getAssets()
.open("customConfigdir/" + PATH);
byte[] b = new byte[inputStream.available()];
inputStream.read(b);
moduleName = context.getFilesDir().getAbsolutePath();
File f = new File(moduleName + "/" + PATH);
if (f.exists()) {
f.delete();
}
f.createNewFile();
out = new FileOutputStream(f);
out.write(b);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
MapView.setCustomMapStylePath(moduleName + "/" + PATH);
}
activity中接入成功
來張效果圖
C:Fragment中接入與Activity中接入略有不同
首先在onCreate()中設定配置檔案接入路徑
public void onCreate(Bundle savedInstanceState) {
FileUtils.setMapCustomFile(getActivity(),PATH);
super.onCreate(savedInstanceState);
開啟個性化地圖要放到onCreateView()方法中,因為Fragment中布局的加載是在onCreateView()中加載的
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map, container, false);
mMapView = (MapView) view.findViewById(R.id.baiduMap_main);
MapView.setMapCustomEnable(true);
//初始化地圖控件
initMap();
initOritationListener();
return view;
}
這樣就接入完畢了
上文中說提供了三種配置顔色,下面補上另外兩種配置顯示效果圖
清新藍
黑夜