<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" /> <link href="http://bigemap.com:9000/bigemap-gl.js/v1.1.0/Widgets/widgets.css" rel="stylesheet" /> <script src="http://bigemap.com:9000/bigemap-gl.js/v1.1.0/bigemap-gl.js"></script> <style> body { margin: 0; padding: 0; } #container { position: absolute; top: 0; bottom: 0; width: 100%; } .bmgl-widget-credits { display: none; } </style> <title>Google Map Streets</title> </head> <body> <div id="container"></div> <script> bmgl.Config.HTTP_URL = "http://bigemap.com:9000"; var viewer = new bmgl.Viewer("container", { mapId: "bigemap.zhongkexingtu", }); const initClusterCollection = (viewer) => { // console.log(bmgl,'bbbb'); new bmgl.GeoJsonDataSource() .load("/bmgl/cluster/poi.json") .then((dataSource) => { viewer.dataSources.add(dataSource); // 设置聚合参数 dataSource.clustering.enabled = true; dataSource.clustering.pixelRange = 60; dataSource.clustering.minimumClusterSize = 4; // foreach用于调用数组的每个元素,并将元素传递给回调函数。 dataSource.entities.values.forEach((entity) => { // console.log(entity); // 将点拉伸一定高度,防止被地形压盖 entity.position._value.z += 50.0; // 使用大小为64*64的icon,缩小展示poi entity.billboard = { image: "/bmgl/cluster/point.png", width: 32, height: 32, }; entity.label = { // text: "POI", text:entity._name, font: "bold 15px Microsoft YaHei", // 竖直对齐方式 verticalOrigin: bmgl.VerticalOrigin.CENTER, // 水平对齐方式 horizontalOrigin: bmgl.HorizontalOrigin.LEFT, // 偏移量 pixelOffset: new bmgl.Cartesian2(-14.0,-28.0), }; }); // 添加监听函数 dataSource.clustering.clusterEvent.addEventListener( function (clusteredEntities, cluster) { // 关闭自带的显示聚合数量的标签 cluster.label.show = false; cluster.billboard.show = true; cluster.billboard.verticalOrigin = bmgl.VerticalOrigin.BOTTOM; // 根据聚合数量的多少设置不同层级的图片以及大小 if (clusteredEntities.length >= 20) { cluster.billboard.image = combineIconAndLabel( "/bmgl/cluster/jhGray.png", clusteredEntities.length, 64 ); cluster.billboard.width = 72; cluster.billboard.height = 72; // cluster.billboard.scale = 0.6; } else if (clusteredEntities.length >= 12) { cluster.billboard.image = combineIconAndLabel( "/bmgl/cluster/jhBlue.png", clusteredEntities.length, 64 ); cluster.billboard.width = 64; cluster.billboard.height = 64; // cluster.billboard.scale = 0.6; } else if (clusteredEntities.length >= 8) { cluster.billboard.image = combineIconAndLabel( "/bmgl/cluster/jhGreen.png", clusteredEntities.length, 64 ); cluster.billboard.width = 56; cluster.billboard.height = 56; // cluster.billboard.scale = 0.6; } else { cluster.billboard.image = combineIconAndLabel( "/bmgl/cluster/jhRed.png", clusteredEntities.length, 64 ); cluster.billboard.width = 40; cluster.billboard.height = 40; // cluster.billboard.scale = 0.6; } } ); }); }; // 将使用的图片和文字联合转换为canvas const combineIconAndLabel = (url, label, size) => { // 创建画布对象 let canvas = document.createElement("canvas"); canvas.width = size; canvas.height = size; let ctx = canvas.getContext("2d"); let promise = new bmgl.Resource.fetchImage(url).then( (image) => { try { ctx.drawImage(image, 0, 0); } catch (e) { console.log(e); } ctx.fillStyle = bmgl.Color.WHITE.toCssColorString(); ctx.font = "bold 12px Microsoft YaHei"; ctx.textAlign = "center"; ctx.textBaseline = "middle"; ctx.fillText(label, size / 4, size / 4); // console.log(`ctx`,ctx); return canvas; } ); return promise; }; // 初始化点位聚合 initClusterCollection(viewer) </script> </body> </html>
源码