工作需要,看了下百度个性化地图,参照开发文档修改了下,发现一些小的细节容易被忽略,导致个性化地图设置,不起作用,下面我针对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;
}
这样就接入完毕了
上文中说提供了三种配置颜色,下面补上另外两种配置显示效果图
清新蓝
黑夜