自iOS地图SDK V5.3.0(包含)版本之后,个性化地图仅支持.sty格式的样式文件。如仍需使用JSON格式,请根据本文编辑JSON代码。
V5.0.0之前的版本:
//个性化地图模板文件路径 NSString *path = [[NSBundle mainBundle] pathForResource:@"custom_map_config" ofType:@"json"]; //设置个性化地图样式 [BMKMapView customMapStyle:path];
//个性化地图模板文件路径 let path: String = Bundle.main.path(forResource: "custom_map_config", ofType:"json")! //设置个性化地图样式 BMKMapView.customMapStyle(path)
打开个性化地图方法如下,
//打开个性化地图 [BMKMapView enableCustomMapStyle:YES];
//打开个性化地图 BMKMapView.enableCustomMapStyle(true)
关闭个性化地图方法如下,
///关闭个性化地图 [BMKMapView enableCustomMapStyle:NO];
//打开个性化地图 BMKMapView.enableCustomMapStyle(false)
V5.0.0及之后的版本:
a、本地离线样式加载方式,关键代码如下1、2两步:
//1.设置本地个性化地图样式 NSString *path = [[NSBundle mainBundle] pathForResource:@"custom_map_config" ofType:@"json"]; //设置个性化地图样式 [self.mapView setCustomMapStylePath:path]; [self.mapView setCustomMapStyleEnable:YES];
//1.获取个性化地图模板文件路径 let path: String = Bundle.main.path(forResource: "custom_map_config", ofType:"json")! //设置个性化地图样式 mapView.setCustomMapStylePath(path) mapView.setCustomMapStyleEnable(true)
打开个性化地图方法如下,
//2.打开个性化地图样式 [self.mapView setCustomMapStyleEnable:YES];
//2.打开个性化地图样式 mapView.setCustomMapStyleEnable(true)
b、通过样式id在线方式加载,关键代码如下1、2两步:
//1.设置在线个性化地图样式 BMKCustomMapStyleOption *option = [[BMKCustomMapStyleOption alloc] init]; //请输入您的在线个性化样式ID option.customMapStyleID = @"4e7360bde67c***d6e69bc6a2c53059c"; //获取本地个性化地图模板文件路径 NSString *path = [[NSBundle mainBundle] pathForResource:@"custom_map_config" ofType:@"json"]; //在线样式ID加载失败后会加载此路径的文件 option.customMapStyleFilePath = path; [self.mapView setCustomMapStyleWithOption:option preLoad:^(NSString *path) { NSLog(@"预加载个性化文件路径:%@",path); } success:^(NSString *path) { NSLog(@"在线个性化文件路径:%@",path); } failure:^(NSError *error, NSString *path) { NSLog(@"设置在线个性化地图失败:%@---%@",error.userInfo,path); }];
//1.设置在线个性化地图样式 let option: BMKCustomMapStyleOption = BMKCustomMapStyleOption() //请输入您的在线个性化样式ID option.customMapStyleID = "4e7360bde67c456d6e69bc6a2c53059c" //获取本地个性化地图模板文件路径 let customPath: String = Bundle.main.path(forResource: "custom_map_config", ofType:"json")! //在线样式ID加载失败后会加载此路径的文件 option.customMapStyleFilePath = customPath; mapView.setCustomMapStyleWith(option, preLoad: { (path) in print(path as Any) }, success: { (path) in print(path as Any) }) { (error, path) in print(error as Any) } mapView.setCustomMapStyleEnable(true)
打开个性化地图方法如下,
//2.打开个性化地图样式 [self.mapView setCustomMapStyleEnable:YES];
//2.打开个性化地图样式 mapView.setCustomMapStyleEnable(true)
支持修改的元素类型和包容关系
以 highway 地图元素为例
元素类型elementType | |||||
元素代码featureType | 形状边框颜色 | 形状填充颜色 | 文字边框颜色 | 文字填充颜色 | icon可见性 |
highway | geometry.stroke | geometry.fill | labels.text.stroke | labels.text.fill | labels.icon |
元素代码featureType | 形状[边框]和[填充]颜色 | 文字[边框]和[填充]颜色 | |||
highway | geometry | labels.text |
设置highway的道路边框和填充 为同一颜色时,既可以分别设置geometry.stroke和geometry.fill,也可以一起设置geometry
某一元素的多样式同级别配置策略(待确定)
自V4.1.1起,由于涉及到样式在不同缩放级别的控制生效,针对可见性(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" } }
设置分级别控制元素生效
自V4.1.1起不同的地图缩放级别支持呈现不同的自定义样式,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", } }
设置宽度样式
自V4.1.1起,Json样式的stylers新增weight字段支持点、线元素的宽度配置,比如点元素(poi类元素,字体),线元素(如高速,地铁等)。
宽度配置生效原则:
1.该字段的值在Json样式中以字符串表现,取值范围为[0, 255]。
2.如果不设置该字段,则以默认样式宽度展示。
示例如下:告诉公路显示的宽度为20。
{ "featureType":"highway", "elementType":"geometry.fill", "stylers":{ "weight":"20" } }
设置透明度样式
自V4.1.1起,增加透明度样式,适用所有可以设置颜色的元素属性,通过“color”字段赋值,格式如下:
颜色格式:#RRGGBBAA ,R:Red、G:Green、B:Blue、 A:Alpha。注意:透明度顺序与路况图颜色的透明度顺序不一致。
如设置为人造区域的形状为黄色,且透明度为0x78 ,其中AA为透明度;透明度AA可以设置为0x00,即完全透明。
{ "featureType": "manmade", "elementType": "geometry", "stylers": { "color": "#ffff0078" //color的格式为 #RRGGBBAA } }
透明度生效原则
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 | √ | V3.4.2 | ||||||
陆地 | land | √ | √ | √ | √ | V3.3.2 | |||
水系 | water | √ | √ | √ | √ | √ | √ | ||
绿地 | green | √ | √ | √ | √ | ||||
人造区域 | manmade | √ | √ | √ | √ | √ | √ | ||
人造区域之地铁区域 | subwaystation | √ | √ | √ | √ | √ | V3.4.2 | ||
人造区域之教育区域 | education | √ | √ | √ | √ | √ | √ | √ | |
人造区域之医疗区域 | medical | √ | √ | √ | √ | √ | √ | √ | |
人造区域之景点区域 | scenicspots | √ | √ | √ | √ | √ | √ | √ | |
人造区域之地产小区区域 | estate | √ | √ | √ | √ | √ | √ | √ | V4.1.1 |
人造区域之购物区域 | shopping | √ | √ | √ | √ | √ | √ | √ | |
人造区域之交通设施区域 | transportation | √ | √ | √ | √ | √ | √ | √ | |
建筑物 | building | √ | √ | √ | √ | V3.3.2 | |||
高速及国道 | highway | √ | √ | √ | √ | √ | √ | √ | |
城市主路 | arterial | √ | √ | √ | √ | √ | √ | √ | |
普通道路 | local | √ | √ | √ | √ | √ | √ | √ | |
国道 | nationalway | √ | √ | √ | √ | √ | √ | √ | V4.2.0 |
省道 | provincialway | √ | √ | √ | √ | √ | √ | √ | |
城市快速路 | cityhighway | √ | √ | √ | √ | √ | √ | √ | |
三级道路 | tertiaryway | √ | √ | √ | √ | √ | √ | √ | |
四级道路 | fourlevelway | √ | √ | √ | √ | √ | √ | √ | |
景区内部道路 | scenicspotsway | √ | √ | √ | √ | √ | √ | √ | |
高校内部道路 | universityway | √ | √ | √ | √ | √ | √ | √ | |
度假地内部道路 | vacationway | √ | √ | √ | √ | √ | √ | √ | |
铁路 | railway | √ | √ | √ | √ | √ | √ | √ | V3.3.2 |
地铁 | subway | √ | √ | √ | √ | √ | √ | √ | |
行政区边界 | boundary | √ | √ | √ | √ | ||||
行政区化 | districtlabel | √ | √ | √ | √ | √ | |||
全部POI标注(V3.4.2起不再去除行政区文字) | poilabel | √ | √ | √ | √ | √ | |||
行政区划之大洲 | continent | √ | √ | √ | √ | √ | V3.4.2 | ||
行政区划之国家 | country | √ | √ | √ | √ | √ | |||
行政区划之省 | province | √ | √ | √ | √ | √ | |||
行政区划之城市 | city | √ | √ | √ | √ | √ | |||
行政区划之区县 | district | √ | √ | √ | √ | √ | |||
行政区划之乡镇 | town | √ | √ | √ | √ | √ | |||
机场标注 | airportlabel | √ | √ | √ | √ | √ | |||
旅游景点标注 | scenicspotslabel | √ | √ | √ | √ | √ | |||
教育标注 | educationlabel | √ | √ | √ | √ | √ | |||
医疗标注 | medicallabel | √ | √ | √ | √ | √ | |||
房产小区标注 | estatelabel | √ | √ | √ | √ | √ | V4.1.1 | ||
商务大厦标注 | businesstowerlabel | √ | √ | √ | √ | √ | |||
公司企业标注 | companylabel | √ | √ | √ | √ | √ | |||
政府机构标注 | governmentlabel | √ | √ | √ | √ | √ | |||
餐饮类标注 | restaurantlabel | √ | √ | √ | √ | √ | |||
宾馆类标注 | hotellabel | √ | √ | √ | √ | √ | |||
购物类标注 | shoppinglabel | √ | √ | √ | √ | √ | |||
生活服务类标注 | lifeservicelabel | √ | √ | √ | √ | √ | |||
汽车服务类标注 | carservicelabel | √ | √ | √ | √ | √ | |||
交通设施类标注 | transportationlabel | √ | √ | √ | √ | √ | |||
金融类标注 | financelabel | √ | √ | √ | √ | √ | |||
其它类标注 | otherlabel | √ | √ | √ | √ | √ |