更新时间:2026/07/27 10:37
您当前查看的是 JSAPI GL 历史版本文档。该版本仅用于维护现有项目,不建议用于新项目开发。新项目请使用 JSAPI 4.0,如需升级现有项目,请参考升级指南。
You are viewing the legacy JSAPI GL documentation. This version is maintained for existing projects only and is not recommended for new development. For new projects, use the JSAPI 4.0 documentation. If you are upgrading an existing project, please refer to the Migration Guide.
You are viewing the legacy JSAPI GL documentation. This version is maintained for existing projects only and is not recommended for new development. For new projects, use the JSAPI 4.0 documentation. If you are upgrading an existing project, please refer to the Migration Guide.
自定义HTML图层
简介
为了支持更丰富多样的地图覆盖物类型,提供自定义覆盖物图层类 CustomHtmlLayer 和自定义覆盖物CustomOverlay,支持开发者自定义DOM覆盖物叠加到地图上,比如实现自定义的Canvas、SVG、DOM元素,常用于叠加自定义的表格、图片、文字、GIF动画、视频等。
其中自定义覆盖物CustomOverlay支持自定义单个覆盖物,自定义覆盖物图层类CustomHtmlLayer是对自定义覆盖物的统一管理,支持根据GeoJSON格式数据进行批量自定义覆盖物叠加。
类参考
(1)构造函数
参数说明:
(2)方法
(3)事件
通过addEventListener(event, callback)方法进行事件绑定,支持如下事件:
图层示例
JavaScript
// 创建自定义覆盖物DOMfunction createDOM(feature) { const img = document.createElement('img'); img.style.height = '240px'; img.style.width = '80px'; img.src = 'https://bj.bcebos.com/v1/mapopen-pub-jsapigl/assets/images/fire.gif'; img.draggable = false; return img;}
// 自定义图层const cusLayer = new BMapGL.CustomHtmlLayer(createDOM, { offsetX: 0, offsetY: 0, minZoom: 5, maxZoom: 17, enablePick: true,});
const data = { type: 'FeatureCollection', features: [ { type: 'Feature', geometry: { type: 'Point', coordinates: [116.27882, 39.71164], }, properties: { prop0: 'value0' }, }, ],};// 设置图层数据cusLayer.setData(data);
// 将自定义html图层添加到地图上map.addCustomHtmlLayer(cusLayer);
// 覆盖物绑定点击事件cusLayer.addEventListener('click', function (e) { console.log('e:', e);});数据格式:
JSON
{ "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": { "type": "Point", "coordinates": [ 116.27882, 39.71164 ] }, "properties": { "prop0": "value0" } }, { "type": "Feature", "geometry": { "type": "Point", "coordinates": [ 116.28882, 39.72164 ] }, "properties": { "prop0": "value0" } } ]}本篇文章对您是否有帮助?