自Android地图SDK V6.3.0(包含)版本之后,个性化地图仅支持.sty格式的样式文件。如仍需使用JSON格式,请根据本文编辑JSON代码。
V6.0.0之前的版本:
// 地图View实例 private MapView mMapView; // 用于设置个性化地图的样式文件 private static final String CUSTOM_FILE_NAME_GRAY = "custom_map_config_gray.json"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 获取.json文件路径 String customStyleFilePath = getCustomStyleFilePath(CustomMapDemo.this, CUSTOM_FILE_NAME_GRAY); // 1、调用静态方法,再地图对象创建之前设置个性化样式路径 MapView.setCustomMapStylePath(customStyleFilePath); mMapView = new MapView(this, new BaiduMapOptions()); FrameLayout frameLayout = new FrameLayout(this); frameLayout.addView(mMapView); setContentView(frameLayout); // 2、调用静态方法,设置个性化地图样式生效 MapView.setMapCustomEnable(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(); // 3、地图对象释放之前,需要调用静态方法,设置关闭个性化地图样式 MapView.setMapCustomEnable(false); // 在activity执行onDestroy时必须调用mMapView.onDestroy() mMapView.onDestroy(); }
V6.0.0及之后的版本:
a、本地离线样式加载方式,关键代码如下1、2两步:
// 地图View实例 private MapView mMapView; // 用于设置个性化地图的样式文件 private static final String CUSTOM_FILE_NAME_GRAY = "custom_map_config_gray.json"; @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); // 获取.json文件路径 String customStyleFilePath = getCustomStyleFilePath(CustomMapDemo.this, CUSTOM_FILE_NAME_GRAY); // 1、设置个性化地图样式文件的路径和加载方式 mMapView.setMapCustomStylePath(customStyleFilePath); // 2、设置个性化地图样式是否生效 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(); }
b、通过样式id在线方式加载,关键代码如下1、2两步:
// 地图View实例 private MapView mMapView; // 用于设置个性化地图的样式文件 private static final String CUSTOM_FILE_NAME_GRAY = "custom_map_config_gray.json"; @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); // 获取.json文件路径 String customStyleFilePath = getCustomStyleFilePath(CustomMapDemo.this, CUSTOM_FILE_NAME_GRAY); // 1、在线个性化样式加载配置项 MapCustomStyleOptions mapCustomStyleOptions = new MapCustomStyleOptions(); mapCustomStyleOptions.localCustomStylePath(customStyleFilePath); //本地离线样式文件路径,如果在线方式加载失败,会默认加载本地样式文件。 mapCustomStyleOptions.customStyleId("d868bebd0fe24fa59381acbcd4db24e3"); //在线样式文件对应的id。 // 2、设置在线个性化样式配置 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(); }
支持修改的元素类型和包容关系
元素类型elementType | |||||
元素代码featureType | 形状边框颜色 | 形状填充颜色 | 文字边框颜色 | 文字填充颜色 | icon可见性 |
highway | geometry.stroke | geometry.fill | labels.text.stroke | labels.text.fill | labels.icon |
元素代码featureType | 形状[边框]和[填充]颜色 | 文字[边框]和[填充]颜色 | |||
highway | geometry | labels.text |
边框和填充为同一颜色时,既可以分别设置geometry.stroke和geometry.fill,也可以一起设置geometry
某一元素的多样式同级别配置策略(待确定)
自V5.1.0起,由于涉及到样式在不同缩放级别的控制生效,针对可见性(visibility),颜色(color),宽度(weight)等样式的生效策略做了调整。当同一元素的可见性(visibility)、颜色(color)、宽度(weight)一起设置时,以最后设置的样式生效;
示例如下:首先将地图元素(水系)的颜色属性配置为绿色,然后又配置了visibility属性为off。则此时water元素将以后面一个配置生效,不会显示绿色。
{ "featureType":"water", "elementType":"all", "stylers":{ "color":"#00ff00ff" } }, { "featureType":"water", "elementType":"all", "stylers":{ "visibility":"off" } },
设置颜色样式
在个性化地图的配置文件custom_configdir.json中,添加如下json串,可设置不同的背景颜色。
示例如下:配置后,地图上所有的水系元素都会展示为绿色。
{ "featureType":"water", "elementType":"all", "stylers":{ "color":"#00ff00ff" } }
设置分级别控制元素生效
自V5.1.0起不同的地图缩放级别支持呈现不同的自定义样式,Json样式stylers中的"level"字段,用来控制样式的生效级别。
指定级别生效原则:
1.该字段的值在Json样式中以字符串表现,取值对标普通地图的缩放级别范围[4-21]。BaiduMap SDK 定义缩放级别和比例尺的关系参考显示地图。
2.如果stylers中不配置"level"字段,则该样式在所有地图缩放级别生效;
3.如果Json样式,存在未指定缩放级别样式1和已指定生效的缩放级别样式2的同一元素,则在指定的缩放级别展示样式2,在其他缩放级别展示样式1;
示例如下:高速公路的样式会在11级别显示为蓝色,其他级别显示默认的颜色。
{ "featureType":"highway", "elementType":"geometry.fill", "stylers":{ "color":"#0000ffff", "level":"11", } }
设置宽度样式
自V5.1.0起,Json样式的stylers新增weight字段支持点、线元素的宽度配置,比如点元素(poi类元素,字体),线元素(如高速,地铁等)。
宽度配置生效原则:
1.该字段的值在Json样式中以字符串表现,取值范围为[0, 255]。
2.如果不设置该字段,则以默认样式宽度展示。
示例如下:告诉公路显示的宽度为20。
{ "featureType":"highway", "elementType":"geometry.fill", "stylers":{ "weight":"20" } }
设置透明度样式
自V4.5.2起,增加透明度样式,适用所有可以设置颜色的元素属性,通过“color”字段赋值,格式如下:
#RRGGBBAA R:Red G:Green B:Blue A:Alpha,透明度AA可以设置为0x00,即完全透明。
注意:当前版本透明度不支持单独设置。
透明度生效原则
1.当前版本透明度不支持单独设置,与颜色样式一起设置。
2.该字段的值使用十六进制,范围为:[0x00, 0xff],默认值为0xff,即不透明。
3.为兼容以前的颜色样式,SDK旧版本的样式值将自动补齐透明度值,默认0xff,即不透明。
示例如下:配置后,所有的高速公路显示的颜色为是,透明度为0x0f的蓝色样式。
{ "featureType":"highway", "elementType":"geometry.fill", "stylers":{ "color":"#0000ff0f" } }
注意:版本支持是V3.4.2表示V3.4.2及以上版本新增属性,版本支持是V4.1.1表示V4.1.1及以上版本新增属性,V3.4.2以下版本更换版本后,请先验证显示效果。
地图元素 | 元素代码 | 形状边框 颜色 |
形状填充 颜色 |
文字边框 颜色 |
文字填充 颜色 |
icon 可见性 |
颜色 可见性 |
颜色 透明度 |
备注 版本支持 |
全部 | all | √ | V4.5.0 | ||||||
陆地 | land | √ | √ | √ | √ | V4.2.0 | |||
水系 | water | √ | √ | √ | √ | √ | √ | ||
绿地 | green | √ | √ | √ | √ | ||||
人造区域 | manmade | √ | √ | √ | √ | √ | √ | ||
人造区域之地铁区域 | subwaystation | √ | √ | √ | √ | √ | V4.5.0 | ||
人造区域之教育区域 | education | √ | √ | √ | √ | √ | √ | √ | |
人造区域之医疗区域 | medical | √ | √ | √ | √ | √ | √ | √ | |
人造区域之景点区域 | scenicspots | √ | √ | √ | √ | √ | √ | √ | |
人造区域之地产小区区域 | estate | √ | √ | √ | √ | √ | √ | √ | V5.1.0 |
人造区域之购物区域 | shopping | √ | √ | √ | √ | √ | √ | √ | |
人造区域之交通设施区域 | transportation | √ | √ | √ | √ | √ | √ | √ | |
建筑物 | building | √ | √ | √ | √ | V4.2.0 | |||
高速及国道 | highway | √ | √ | √ | √ | √ | √ | √ | |
城市主路 | arterial | √ | √ | √ | √ | √ | √ | √ | |
普通道路 | local | √ | √ | √ | √ | √ | √ | √ | |
国道 | nationalway | √ | √ | √ | √ | √ | √ | √ | V5.2.1 |
省道 | provincialway | √ | √ | √ | √ | √ | √ | √ | |
城市快速路 | cityhighway | √ | √ | √ | √ | √ | √ | √ | |
三级道路 | tertiaryway | √ | √ | √ | √ | √ | √ | √ | |
四级道路 | fourlevelway | √ | √ | √ | √ | √ | √ | √ | |
景区内部道路 | scenicspotsway | √ | √ | √ | √ | √ | √ | √ | |
高校内部道路 | universityway | √ | √ | √ | √ | √ | √ | √ | |
度假地内部道路 | vacationway | √ | √ | √ | √ | √ | √ | √ | |
铁路 | railway | √ | √ | √ | √ | √ | √ | √ | V4.2.0 |
地铁 | subway | √ | √ | √ | √ | √ | √ | √ | |
行政区边界 | boundary | √ | √ | √ | √ | ||||
行政区化 | districtlabel | √ | √ | √ | √ | √ | |||
全部POI标注(V4.5.0起不再去除行政区文字) | poilabel | √ | √ | √ | √ | √ | |||
行政区划之大洲 | continent | √ | √ | √ | √ | √ | V4.5.0 | ||
行政区划之国家 | country | √ | √ | √ | √ | √ | |||
行政区划之省 | province | √ | √ | √ | √ | √ | |||
行政区划之城市 | city | √ | √ | √ | √ | √ | |||
行政区划之区县 | district | √ | √ | √ | √ | √ | |||
行政区划之乡镇 | town | √ | √ | √ | √ | √ | |||
机场标注 | airportlabel | √ | √ | √ | √ | √ | |||
旅游景点标注 | scenicspotslabel | √ | √ | √ | √ | √ | |||
教育标注 | educationlabel | √ | √ | √ | √ | √ | |||
医疗标注 | medicallabel | √ | √ | √ | √ | √ | |||
房产小区标注 | estatelabel | √ | √ | √ | √ | √ | V5.1.0 | ||
商务大厦标注 | businesstowerlabel | √ | √ | √ | √ | √ | |||
公司企业标注 | companylabel | √ | √ | √ | √ | √ | |||
政府机构标注 | governmentlabel | √ | √ | √ | √ | √ | |||
餐饮类标注 | restaurantlabel | √ | √ | √ | √ | √ | |||
宾馆类标注 | hotellabel | √ | √ | √ | √ | √ | |||
购物类标注 | shoppinglabel | √ | √ | √ | √ | √ | |||
生活服务类标注 | lifeservicelabel | √ | √ | √ | √ | √ | |||
汽车服务类标注 | carservicelabel | √ | √ | √ | √ | √ | |||
交通设施类标注 | transportationlabel | √ | √ | √ | √ | √ | |||
金融类标注 | financelabel | √ | √ | √ | √ | √ | |||
其它类标注 | otherlabel | √ | √ | √ | √ | √ |