方式一:配置.sty样式ID
// 地图View实例
private MapView mMapView;
// 用于设置个性化地图的样式文件
private static final String CUSTOM_FILE_NAME_CX = "custom_map_config_CX.sty";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mMapView = new MapView(this, new BaiduMapOptions());
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.addView(mMapView);
setContentView(frameLayout);
// 获取.sty文件路径
String customStyleFilePath = getCustomStyleFilePath(CustomMapDemo.this, CUSTOM_FILE_NAME_CX);
MapCustomStyleOptions mapCustomStyleOptions = new MapCustomStyleOptions();
mapCustomStyleOptions.localCustomStylePath(customStyleFilePath); //本地离线样式文件路径,如果在线方式加载失败,会默认加载本地样式文件。
mapCustomStyleOptions.customStyleId("d868bebd0fe24fa59381acbcd4db24e3"); //在线样式文件对应的id。
mMapView.setMapCustomStyle(mapCustomStyleOptions, new CustomMapStyleCallBack() {
@Override
public boolean onPreLoadLastCustomMapStyle(String customStylePath) {
return false; //默认返回false,由SDK内部处理加载逻辑;返回true则SDK内部不会做任何处理,由开发者自行完成样式加载。
}
@Override
public boolean onCustomMapStyleLoadSuccess(boolean hasUpdate, String customStylePath) {
return false; //默认返回false,由SDK内部处理加载逻辑;返回true则SDK内部不会做任何处理,由开发者自行完成样式加载。
}
@Override
public boolean onCustomMapStyleLoadFailed(int status, String Message, String customStylePath) {
return false; //默认返回false,由SDK内部处理加载逻辑;返回true则SDK内部不会做任何处理,由开发者自行完成样式加载。
}
});
}
@Override
protected void onResume() {
super.onResume();
// 在activity执行onResume时必须调用mMapView.onResume()
mMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
//在activity执行onPause时必须调用mMapView.onPause()
mMapView.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
// 在activity执行onDestroy时必须调用mMapView.onDestroy()
mMapView.onDestroy();
}
方式二:加载样式文件
a、 在项目中添加自定义样式文件 如:将离线样式文件存放在assets目录下。
b、 传入样式文件路径:
V6.0.0版本起,建议使用新的个性化地图API,能够实现动态更改样式(同一地图设置不同的样式),同时适配多地图场景(不同地图设置不同的样式),并且样式文件路径设置API不再要求在地图创建之前调用,地图对象创建完成之后设置即可。在地图对象释放时,也无需关闭个性化开关。详细区别见个性化地图元素说明规则
// 地图View实例
private MapView mMapView;
// 用于设置个性化地图的样式文件
private static final String CUSTOM_FILE_NAME_CX = "custom_map_config_CX.sty";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mMapView = new MapView(this, new BaiduMapOptions());
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.addView(mMapView);
setContentView(frameLayout);
// 获取.sty文件路径
String customStyleFilePath = getCustomStyleFilePath(CustomMapDemo.this, CUSTOM_FILE_NAME_CX);
// 设置个性化地图样式文件的路径和加载方式
mMapView.setMapCustomStylePath(customStyleFilePath);
// 动态设置个性化地图样式是否生效
mMapView.setMapCustomStyleEnable(true);
}
@Override
protected void onResume() {
super.onResume();
// 在activity执行onResume时必须调用mMapView.onResume()
mMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
//在activity执行onPause时必须调用mMapView.onPause()
mMapView.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
// 在activity执行onDestroy时必须调用mMapView.onDestroy()
mMapView.onDestroy();
}