GeoJSONLayer

GeoJSON 覆盖物组合图层,用于在地图上展示 GeoJSON 格式的地理数据。 通过 map.addLayer() / map.removeLayer() 方法管理。

构造函数

  • 创建 GeoJSON 覆盖物组合图层

    参数类型说明
    layerNamestring图层名称,每个覆盖物都将写入此名称作为属性
    options 可选GeoJSONLayerOptions可选参数
    属性类型说明
    dataSourceobjectGeoJSON 结构数据
    levelnumber显示层级,负数越大层级越高
    markerStyleFunction点类型数据样式,详见 MarkerOptions
    maxZoomnumber最大显示层级
    minZoomnumber最小显示层级
    polygonStyleFunction面类型数据样式,详见 PolygonOptions
    polylineStyleFunction线类型数据样式,详见 PolylineOptions
    referencestring来源数据坐标系,可选 'BD09LL'、'BD09MC'、'EPSG3857'、'GCJ02'、'WGS84'
    visibleboolean图层是否显示

    返回值 GeoJSONLayer

    示例代码1

    const geoJSONLayer = new BMap.GeoJSONLayer('roads', {
    polylineStyle: { strokeColor: '#0055ff', strokeWeight: 3 },
    });
    map.addLayer(geoJSONLayer);
    geoJSONLayer.setData({
    type: 'FeatureCollection',
    features: [
    { type: 'Feature', geometry: { type: 'LineString', coordinates: [[116.39, 39.91], [116.42, 39.93]] }, properties: {} },
    ],
    });

    示例代码2

    const geoJSONLayer = new BMap.GeoJSONLayer('pois', {
    dataSource: {
    type: 'FeatureCollection',
    features: [
    { type: 'Feature', geometry: { type: 'Point', coordinates: [116.404, 39.915] }, properties: { name: '天安门' } },
    ],
    },
    markerStyle: { title: '标记点' },
    });
    map.addLayer(geoJSONLayer);

方法

  • 添加事件监听

    参数类型说明
    typestring事件类型,支持 'click''mousemove''mouseout'
    handlerFunction回调函数,Event 对象中 features 属性为触发的要素实例集合

    返回值 void

    示例代码1

    geoJSONLayer.addEventListener('click', (e) => {
    console.log('features:', e.features);
    });
  • 清空地图上的覆盖物数据及覆盖物对象集合

    返回值 void

  • 销毁图层,清空覆盖物数据及关联的 map 对象

    返回值 void

  • 返回显示层级

    返回值 number

  • 返回图层显示状态

    返回值 boolean

  • 通过事件获取包含当前点的覆盖物集合

    参数类型说明
    eEvent地图事件对象

    返回值 Overlay[]

    示例代码1

    map.addEventListener('click', (e) => {
    const overlays = geoJSONLayer.pickOverlays(e);
    console.log('picked overlays:', overlays);
    });
  • 移除事件监听

    参数类型说明
    typestring事件类型
    handlerFunction定义的回调函数

    返回值 void

    示例代码1

    const handler = (e: any) => { console.log(e); };
    geoJSONLayer.addEventListener('click', handler);
    geoJSONLayer.removeEventListener('click', handler);
  • 重置样式到图层初始化状态

    返回值 void

  • 设置图层显示的 GeoJSON 数据源

    参数类型说明
    geojsonobjectGeoJSON 结构数据

    返回值 void

    示例代码1

    geoJSONLayer.setData({
    type: 'FeatureCollection',
    features: [
    { type: 'Feature', geometry: { type: 'Point', coordinates: [116.404, 39.915] }, properties: {} }
    ]
    });
  • 设置显示层级

    参数类型说明
    znumber显示层级

    返回值 void

    示例代码1

    geoJSONLayer.setLevel(-50);
    
  • 设置图层显示/隐藏

    参数类型说明
    vboolean是否显示

    返回值 void

    示例代码1

    geoJSONLayer.setVisible(false);