代码
const map = new BMap.Map('map');
const center = new BMap.Point(116.404, 39.98);
map.centerAndZoom(center, 11);

// 接口返回数据中 properties有name字段,根据name字段匹配对应的颜色
var colorMap = {
  海淀区: 'red',
  朝阳区: 'blue',
  通州区: 'green',
  丰台区: 'pink',
  房山区: 'orange',
};

var polygonLayer = new BMap.PolygonLayer({
  referCenter: center,
  enablePicked: true, // 开启点击拾取
  // fillColor 传回调,按要素求值;name 没有匹配到时,默认使用 #aecde8
  fillColor: function (properties) {
    return colorMap[properties.name] || '#aecde8';
  },
  fillOpacity: 0.6,
  strokeColor: '#fff',
  strokeWeight: 1.5,
});
map.addLayer(polygonLayer);

// 监听点击事件
polygonLayer.addEventListener('click', function (e) {
  var item = e.value;
  if (item && item.properties) {
    document.getElementById('text').textContent =
      '选中:' + (item.properties.name || JSON.stringify(item.properties));
  }
});

// 请求北京面数据
fetch('https://mapopen-pub-jsapigl.bj.bcebos.com/svgmodel/fillLayerData.json')
  .then(function (res) {
    return res.json();
  })
  .then(function (data) {
    polygonLayer.setData(data);
  });

PolygonLayer 的 fillColor 支持传入回调函数,按要素的 properties 返回样式,更多配置规则见类参考文档

预览