BIGEMPA Js API示例中心

标注聚合显示源代码展示

代码编辑区 运行 下载 还原
<!DOCTYPE html>

<html>
<head>
	<meta charset='UTF-8' />
	<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
	<!--
        以下CSS地址请在安装软件了替换成本地的地址
        CSS地址请使用:
        http://localhost:9000/bigemap.js/v2.1.0/bigemap.css
        软件下载地址 http://www.bigemap.com/reader/download/detail201802017.html
    -->
	<link href='http://www.bigemap.com:9000/bigemap.js/v2.1.0/bigemap.css' rel='stylesheet' />
	<!--
        JS地址请使用:
        http://localhost:9000/bigemap.js/v2.1.0/bigemap.js
    -->
	<script src='http://www.bigemap.com:9000/bigemap.js/v2.1.0/bigemap.js'></script>
	<!--引入对应的JS+CSS 相关下载地址 http://www.bigemap.com/Public/offline/marker_cluster/cluster.zip-->
	<link rel="stylesheet" href="/Public/offline/marker_cluster/MarkerCluster.Default.css" />
	<script src="/Public/offline/marker_cluster/bm.markercluster-src.js"></script>
	<style type="text/css">
		*{
			padding: 0;
			margin: 0;
		}
		html,body,#map{
			width: 100%;
			height: 100%;
		}
	</style>
</head>
<body>
  <div id='map'></div>
  <script>
    BM.Config.HTTP_URL = 'http://www.bigemap.com:9000';
    var map = BM.map('map', 'bigemap.zhongkexingtu', {
      center: [30.6, 104.5],
      zoom: 7,
      zoomControl: true,
      attributionControl: false
    });
    //登陆聚合对象
    var markers = new BM.MarkerClusterGroup({
      showCoverageOnHover: false, //不显示边界
      zoomToBoundsOnClick: true,//点击放大到对应位置
      removeOutsideVisibleBounds: false,
      animate: true,//动画
      maxClusterRadius: 100,//缩放半径,
      disableClusteringAtZoom:null,//在指定级别以下禁用缩放
      //使用定义的图标来显示
      // iconCreateFunction: function (cluster) {
      //   var markers = cluster.getAllChildMarkers();

      //   // BM.icon({iconUrl:'http://www.bigemap.com/mapoffline/marker/2.png'}) 
      //   return BM.divIcon({
      //     html: `<div class="icon">${markers.length}</div>`,
      //     iconSize: BM.point(40, 40)
      //   });
      // }
    })
    // BM.marker([i*5,j*5],{icon:BM.icon({iconUrl:'http://www.bigemap.com/mapoffline/marker/'+20+'.png'})}).addTo(map);
    var markersList = [];
    //生成2000个点推入标记数组
    function populate() {
      for (let i = 0; i < 2000; i++) {
        let m = new BM.marker(getRandomLatLng(map), {
          icon: BM.icon({
            iconUrl: 'http://www.bigemap.com/mapoffline/marker/' + 2 + '.png'
          })
        })
        markersList.push(m);
        markers.addLayer(m);
      }
      return false
    }
    //根据当前的范围生成随机的点
    function getRandomLatLng(map) {
      let bounds = map.getBounds(),
        southWest = bounds.getSouthWest(),
        northEast = bounds.getNorthEast(),
        lngSpan = northEast.lng - southWest.lng,
        latSpan = northEast.lat - southWest.lat;
      return new BM.LatLng(
        southWest.lat + latSpan * Math.random(),
        southWest.lng + lngSpan * Math.random());
    }
    //对聚合的实例添加点击事件
    markers.on('clusterclick', function (a) {
      console.log('cluster' + a.layer.getAllChildMarkers().length);
    })
    //对标注添加一个点击事件
    markers.on('click', function (a) {
      alert('marker ' + a.layer);
    });
    populate();
    map.addLayer(markers);
  </script>
</body>

</html>