百度地图 - 点聚合方式批量渲染坐标点

简介: 最近在做一个大数据人员分布的系统,需要能够在地图上渲染数据库中的坐标点,大概有四万个坐标点,普通的渲染方式会导致客户端崩溃的,或者是坐标点太过于密集,用户体验差。于是使用了百度地图的点聚合方式,特此记录一下

1、百度地图API

首先我们需要去百度地图申请一个应用,得到密钥

http://lbsyun.baidu.com/

2、点聚合

代码如下:

<head>
  <meta charset="UTF-8">
  <title>获取自定义区域内的坐标点 - 百度地图</title>
  <style>
    html,
    body {
      width: 100%;
      height: 100vh;
      padding: 0;
      margin: 0;
      overflow: hidden;
    }
    /* 隐藏百度地图logo */
    .anchorBL {
      display: none;
    }
    .btn-contrail button {
      margin-bottom: 30px;
      margin-left: 0px!important;
      background-color: #FFFFFF;
      color: rgba(0, 0, 0, .5);
      padding: 10px;
    }
    .layui-btn:hover {
      background-color: #1890ff!important;
      color: #FFFFFF!important;
    }
    .btn-click {
      background-color: #1890ff!important;
      color: #FFFFFF!important;
    }
  </style>
</head>
<body>
  <div style="position: fixed;top: 0;right: 0;height: 100vh;background: rgba(0,0,0,0);width: 125px;z-index: 9999999;display: flex;justify-content: center;align-items: center;">
    <div class="tools" style="width: 100%;height: 100%">
      <div class="btn-contrail" style="height: 100%;background: rgba(0,0,0,.5);width: 100%;display: flex;flex-direction: column;justify-content: center;align-items: center;">
        <button type="button" class="layui-btn btn-click" data-model="0">查看点</button>
        <button type="button" class="layui-btn" data-model="1">标新点</button>
        <button type="button" class="layui-btn" data-model="2">删除点</button>
      </div>
    </div>
  </div>
  <!-- 地图容器 -->
  <div id="container" style="height: 100vh;width: 100%"></div>
  <!-- jquery -->
  <script type="text/javascript" src="js/jquery-3.2.0.min.js" ></script>
  <!-- 百度地图api -->
  <script type="text/javascript" src="https://api.map.baidu.com/api?v=3.0&ak=您的密钥"></script>
  <!-- 点聚合工具 -->
  <script type="text/javascript" src="http://api.map.baidu.com/library/TextIconOverlay/1.2/src/TextIconOverlay_min.js"></script>
  <!-- 点聚合工具 -->
  <script type="text/javascript" src="https://api.map.baidu.com/library/MarkerClusterer/1.2/src/MarkerClusterer_min.js"></script>
  <script type="text/javascript">
    // 工具栏模式:0-查看点1-标新点2-删除点
    var model = 0;
    // 地图上所有的点
    var points = [];
    // 切换模式
    $('body').on('click', '.layui-btn', function() {
      // 样式改变
      $('.btn-contrail').find('.layui-btn').removeClass('btn-click');
      $(this).addClass('btn-click');
      // 事件监听
      model = $(this).data('model');
      if(3 == model) {
        $('.BMapLib_Drawing').show();
      } else {
        $('.BMapLib_Drawing').hide();
      }
      // 清除域
      if(4 == model) {
        clearAll();
      }
      // 计算点
      if(5 == model) {
        alert('所选区域点数:' + areaPoint.length);
      }
    });
    // 定义地图
    var mp = new BMap.Map("container");
    // 鼠标滚动缩放
      mp.enableScrollWheelZoom(true);
      // 地图中心点,并设置级别
      mp.centerAndZoom(new BMap.Point(106.908, 28.1744), 10);
      // 地图类型,卫星地图
      mp.setMapType(BMAP_HYBRID_MAP);
      // 地图缩放级别
      mp.setZoom(10)
    // 请求数据,并渲染地图
    init();
    function init() {
      // 获取数据
      $.get("json/data.json", function(res) {
        points = res.data;
        renderMap();
      });
    }
    // 渲染地图坐标点
    function renderMap() {
      var markers = [];
      points.forEach(v => {
        // 创建新的坐标点
        const marker = new BMap.Marker(new BMap.Point(v['lng'], v['lat']), {
          icon: ''
        })
        // 为每一个点添加点击事件
        marker.addEventListener('click', function() {
          showPoint(v, marker);
        });
        markers.push(marker)
      })
      // 添加点聚合
      new BMapLib.MarkerClusterer(mp, {
        markers: markers
      });
    }
    // 监听地图点击事件
    mp.addEventListener("click", function(e) {
      var point = e.point;
      // 标新点
      if(1 == model) {
        // 创建新的坐标点
        const marker = new BMap.Marker(new BMap.Point(point.lng, point.lat));
        // 添加点击事件
        marker.addEventListener('click', function() {
          showPoint(point, marker);
        });
        // 添加坐标点到地图中
        mp.addOverlay(marker);
        // 将坐标点添加到我们的坐标带你集合中
        points.push(point);
      }
    });
    // 坐标点的点击事件监听
    function showPoint(point, marker) {
      // 查看点
      if(0 == model) {
        var lng = parseFloat(point.lng).toFixed(5);
        var lat = parseFloat(point.lat).toFixed(5);
        alert('经度:' + lng + ',纬度:' + lat);
      }
      // 删除点
      if(2 == model) {
        mp.removeOverlay(marker);
      }
    }
  </script>
</body>

3、JSON 数据格式

{
  "code": 200,
  "msg": "操作成功",
  "data": [{
      "lng": "109.19151",
      "lat": "27.73559"
    },
    {
      "lng": "106.67668",
      "lat": "26.559734"
    }
  ]
}
相关文章
|
10月前
|
监控 供应链 API
速卖通商品 API,开发者示例
速卖通商品API是速卖通开放平台为开发者提供的接口,支持获取商品详情、订单管理等功能。具备多语言、多货币支持,适用于全球电商场景。可应用于商品分析、价格监控、竞品对比和库存管理等业务。文档提供Python调用示例,便于快速集成使用。
|
存储 JavaScript 前端开发
史上最详细JavaScript数组去重方法(11种)
使用set和Array.from ()方法 array.from可以将set结构转成数组
437 7
vue3 reactive数据更新,视图不更新问题
vue3 reactive数据更新,视图不更新问题
610 3
|
JavaScript 算法 定位技术
利用Cesium和JS实现地点点聚合功能
利用Cesium和JS实现地点点聚合功能
949 0
|
安全 Ubuntu Linux
Ubuntu解密:Root账户登录问题一网打尽
Ubuntu解密:Root账户登录问题一网打尽
700 1
U盘复制文件到最后5秒会卡住怎么办解决
现在的U盘容量已经非常大了,一般都有16G以上,为了能放单文件大于4G的数据大多数时候我们都是把U盘格式化为ntfs格式的,所以会出现不管是大文件还是小文件,当你往U盘里复制文件或者使用发送到U盘功能的时候会出现一个怪现象,那就是不管U盘速度如何,都会在前部分拷贝的时候速度非常快,基本上达到U盘的正常速度,但当到达100%的时候就会卡住好久,短的5秒,慢的几分钟才会显示复制完成。
8870 0
|
iOS开发
iOS之使用模拟器报错:resource fork, Finder information, or similar detritus not allowed完美解决方案
iOS之使用模拟器报错:resource fork, Finder information, or similar detritus not allowed完美解决方案
7092 0
|
缓存 网络协议 网络架构
数据通信网络之使用 eNSP 组网
数据通信网络之使用 eNSP 组网
1433 0
|
小程序 API 网络安全
小程序中的合法域名的作用及条件有哪些?
小程序中的合法域名的作用及条件有哪些?
华为手机连不上adb解决方法
1.关闭qq,豌豆荚等一连接usb自动侦测手机的程序。。。 2.安装hisuite软件,这个应该跟相应的版本有关,新版本最好要安装这个软件,否则也可能导致怎么都连接不上 3.打开usb调试功能 4.
5850 0

热门文章

最新文章