如果你是同步请求

建议先看我上篇文章
我的上篇文章
直接在index.html里面加

<script type="text/javascript" src="https://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>

在代码中添加Marker,实例化点聚合

var MAX = 10;
var markers = [];
var pt = null;
var i = 0;
for (; i < MAX; i++) {pt = new BMap.Point(Math.random() * 40 + 85, Math.random() * 30 + 21);markers.push(new BMap.Marker(pt));
}
//最简单的用法,生成一个marker数组,然后调用markerClusterer类即可。
var markerClusterer = new BMapLib.MarkerClusterer(map, {markers:markers});

2如果异步请求,

那么你就要在百度地图执行回调的时候,去创建script标签。这里需要说明如果引入,百度地图的js那么你放大缩小,或者点击聚合,他会把你的label给清掉

export default {init: function (AK) {// 百度地图API文件链接,异步加载必须带参数callback,后面是回调函数。const BMap_URL = "http://api.map.baidu.com/api?v=3.0&ak=" + AK + "&callback=onBMapCallback";return new Promise((resolve, reject) => {// 如果已加载直接返回if (typeof BMap !== "undefined") {// eslint-disable-next-line no-undefresolve(BMap);return true;}// 百度地图异步加载回调处理window.onBMapCallback = function () {let TextIconOverlay_min = document.createElement("script");TextIconOverlay_min.setAttribute("type", "text/javascript");TextIconOverlay_min.setAttribute("src", "https://api.map.baidu.com/library/TextIconOverlay/1.2/src/TextIconOverlay_min.js");document.head.appendChild(TextIconOverlay_min);let MarkerClusterer_min = document.createElement("script");MarkerClusterer_min.setAttribute("type", "text/javascript");MarkerClusterer_min.setAttribute("src", "./js/markerClusterer.js");这里我引入的是本地document.head.appendChild(MarkerClusterer_min);// eslint-disable-next-line no-undefresolve(BMap);};// 插入script脚本let scriptNode = document.createElement("script");scriptNode.setAttribute("type", "text/javascript");scriptNode.setAttribute("src", BMap_URL);document.body.appendChild(scriptNode);});}
}

同理如果你是同步也会清掉,你也把那个换成本地。这里给你们附上本地markerClusterer.js

var BMapLib = window.BMapLib = BMapLib || {};
(function () {var getExtendedBounds = function (map, bounds, gridSize) {bounds = cutBoundsInRange(bounds);var pixelNE = map.pointToPixel(bounds.getNorthEast());var pixelSW = map.pointToPixel(bounds.getSouthWest());pixelNE.x += gridSize;pixelNE.y -= gridSize;pixelSW.x -= gridSize;pixelSW.y += gridSize;var newNE = map.pixelToPoint(pixelNE);var newSW = map.pixelToPoint(pixelSW);return new BMap.Bounds(newSW, newNE)};var cutBoundsInRange = function (bounds) {var maxX = getRange(bounds.getNorthEast().lng, -180, 180);var minX = getRange(bounds.getSouthWest().lng, -180, 180);var maxY = getRange(bounds.getNorthEast().lat, -74, 74);var minY = getRange(bounds.getSouthWest().lat, -74, 74);return new BMap.Bounds(new BMap.Point(minX, minY), new BMap.Point(maxX, maxY))};var getRange = function (i, mix, max) {mix && (i = Math.max(i, mix));max && (i = Math.min(i, max));return i};var isArray = function (source) {return '[object Array]' === Object.prototype.toString.call(source)};var indexOf = function (item, source) {var index = -1;if (isArray(source)) {if (source.indexOf) {index = source.indexOf(item)} else {for (var i = 0,m; m = source[i]; i++) {if (m === item) {index = i;break;}}}}return index};var MarkerClusterer = BMapLib.MarkerClusterer = function (map, options) {if (!map) {return}this._map = map;this._markers = [];this._clusters = [];var opts = options || {};this._gridSize = opts["gridSize"] || 60;this._maxZoom = opts["maxZoom"] || 18;this._minClusterSize = opts["minClusterSize"] || 2;this._isAverageCenter = false;if (opts['isAverageCenter'] != undefined) {this._isAverageCenter = opts['isAverageCenter']}this._styles = opts["styles"] || [];var that = this;this._map.addEventListener("zoomend",function () {that._redraw()});this._map.addEventListener("moveend",function () {that._redraw()});var mkrs = opts["markers"];isArray(mkrs) && this.addMarkers(mkrs)};MarkerClusterer.prototype.addMarkers = function (markers) {for (var i = 0,len = markers.length; i < len; i++) {this._pushMarkerTo(markers[i])}this._createClusters()};MarkerClusterer.prototype._pushMarkerTo = function (marker) {var index = indexOf(marker, this._markers);if (index === -1) {marker.isInCluster = false;this._markers.push(marker)}};MarkerClusterer.prototype.addMarker = function (marker) {this._pushMarkerTo(marker);this._createClusters()};MarkerClusterer.prototype._createClusters = function () {var mapBounds = this._map.getBounds();var extendedBounds = getExtendedBounds(this._map, mapBounds, this._gridSize);for (var i = 0,marker; marker = this._markers[i]; i++) {if (!marker.isInCluster && extendedBounds.containsPoint(marker.getPosition())) {this._addToClosestCluster(marker)}}};MarkerClusterer.prototype._addToClosestCluster = function (marker) {var distance = 4000000;var clusterToAddTo = null;var position = marker.getPosition();for (var i = 0,cluster; cluster = this._clusters[i]; i++) {var center = cluster.getCenter();if (center) {var d = this._map.getDistance(center, marker.getPosition());if (d < distance) {distance = d;clusterToAddTo = cluster}}}if (clusterToAddTo && clusterToAddTo.isMarkerInClusterBounds(marker)) {clusterToAddTo.addMarker(marker)} else {var cluster = new Cluster(this);cluster.addMarker(marker);this._clusters.push(cluster)}};MarkerClusterer.prototype._clearLastClusters = function () {for (var i = 0,cluster; cluster = this._clusters[i]; i++) {cluster.remove()}this._clusters = [];this._removeMarkersFromCluster()};MarkerClusterer.prototype._removeMarkersFromCluster = function () {for (var i = 0,marker; marker = this._markers[i]; i++) {marker.isInCluster = false}};MarkerClusterer.prototype._removeMarkersFromMap = function () {for (var i = 0,marker; marker = this._markers[i]; i++) {marker.isInCluster = false;tmplabel = marker.getLabel();this._map.removeOverlay(marker);marker.setLabel(tmplabel)}};MarkerClusterer.prototype._removeMarker = function (marker) {var index = indexOf(marker, this._markers);if (index === -1) {return false}tmplabel = marker.getLabel();this._map.removeOverlay(marker);marker.setLabel(tmplabel);this._markers.splice(index, 1);return true};MarkerClusterer.prototype.removeMarker = function (marker) {var success = this._removeMarker(marker);if (success) {this._clearLastClusters();this._createClusters()}return success};MarkerClusterer.prototype.removeMarkers = function (markers) {var success = false;for (var i = 0; i < markers.length; i++) {var r = this._removeMarker(markers[i]);success = success || r}if (success) {this._clearLastClusters();this._createClusters()}return success};MarkerClusterer.prototype.clearMarkers = function () {this._clearLastClusters();this._removeMarkersFromMap();this._markers = []};MarkerClusterer.prototype._redraw = function () {this._clearLastClusters();this._createClusters()};MarkerClusterer.prototype.getGridSize = function () {return this._gridSize};MarkerClusterer.prototype.setGridSize = function (size) {this._gridSize = size;this._redraw()};MarkerClusterer.prototype.getMaxZoom = function () {return this._maxZoom};MarkerClusterer.prototype.setMaxZoom = function (maxZoom) {this._maxZoom = maxZoom;this._redraw()};MarkerClusterer.prototype.getStyles = function () {return this._styles};MarkerClusterer.prototype.setStyles = function (styles) {this._styles = styles;this._redraw()};MarkerClusterer.prototype.getMinClusterSize = function () {return this._minClusterSize};MarkerClusterer.prototype.setMinClusterSize = function (size) {this._minClusterSize = size;this._redraw()};MarkerClusterer.prototype.isAverageCenter = function () {return this._isAverageCenter};MarkerClusterer.prototype.getMap = function () {return this._map};MarkerClusterer.prototype.getMarkers = function () {return this._markers};MarkerClusterer.prototype.getClustersCount = function () {var count = 0;for (var i = 0,cluster; cluster = this._clusters[i]; i++) {cluster.isReal() && count++}return count};function Cluster(markerClusterer) {this._markerClusterer = markerClusterer;this._map = markerClusterer.getMap();this._minClusterSize = markerClusterer.getMinClusterSize();this._isAverageCenter = markerClusterer.isAverageCenter();this._center = null;this._markers = [];this._gridBounds = null;this._isReal = false;this._clusterMarker = new BMapLib.TextIconOverlay(this._center, this._markers.length, {"styles": this._markerClusterer.getStyles()})}Cluster.prototype.addMarker = function (marker) {if (this.isMarkerInCluster(marker)) {return false}if (!this._center) {this._center = marker.getPosition();this.updateGridBounds()} else {if (this._isAverageCenter) {var l = this._markers.length + 1;var lat = (this._center.lat * (l - 1) + marker.getPosition().lat) / l;var lng = (this._center.lng * (l - 1) + marker.getPosition().lng) / l;this._center = new BMap.Point(lng, lat);this.updateGridBounds()}}marker.isInCluster = true;this._markers.push(marker);var len = this._markers.length;if (len < this._minClusterSize) {this._map.addOverlay(marker);return true} else if (len === this._minClusterSize) {for (var i = 0; i < len; i++) {tmplabel = this._markers[i].getLabel();this._markers[i].getMap() && this._map.removeOverlay(this._markers[i]);this._markers[i].setLabel(tmplabel)}}this._map.addOverlay(this._clusterMarker);this._isReal = true;this.updateClusterMarker();return true};Cluster.prototype.isMarkerInCluster = function (marker) {if (this._markers.indexOf) {return this._markers.indexOf(marker) != -1} else {for (var i = 0,m; m = this._markers[i]; i++) {if (m === marker) {return true}}}return false};Cluster.prototype.isMarkerInClusterBounds = function (marker) {return this._gridBounds.containsPoint(marker.getPosition())};Cluster.prototype.isReal = function (marker) {return this._isReal};Cluster.prototype.updateGridBounds = function () {var bounds = new BMap.Bounds(this._center, this._center);this._gridBounds = getExtendedBounds(this._map, bounds, this._markerClusterer.getGridSize())};Cluster.prototype.updateClusterMarker = function () {if (this._map.getZoom() > this._markerClusterer.getMaxZoom()) {this._clusterMarker && this._map.removeOverlay(this._clusterMarker);for (var i = 0,marker; marker = this._markers[i]; i++) {this._map.addOverlay(marker)}return}if (this._markers.length < this._minClusterSize) {this._clusterMarker.hide();return}this._clusterMarker.setPosition(this._center);this._clusterMarker.setText(this._markers.length);var thatMap = this._map;var thatBounds = this.getBounds();this._clusterMarker.addEventListener("click",function (event) {thatMap.setViewport(thatBounds)})};Cluster.prototype.remove = function () {for (var i = 0,m; m = this._markers[i]; i++) {var tmplabel = this._markers[i].getLabel();this._markers[i].getMap() && this._map.removeOverlay(this._markers[i]);this._markers[i].setLabel(tmplabel)}this._map.removeOverlay(this._clusterMarker);this._markers.length = 0;delete this._markers};Cluster.prototype.getBounds = function () {var bounds = new BMap.Bounds(this._center, this._center);for (var i = 0,marker; marker = this._markers[i]; i++) {bounds.extend(marker.getPosition())}return bounds};Cluster.prototype.getCenter = function () {return this._center;};
})();

有什么不懂欢迎留言

vue百度地图引入聚合,点击聚合,label被清理掉问题解决。同步异步问题相关推荐

  1. vue调用百度地图API实现点击相应位置切换地图定位

    vue调用百度地图API实现点击相应位置切换地图定位 1.需求分析 在页面显示地图,初始状态显示上海市,地图上有特定地点的标注. 左侧为建筑(地点)列表,点击某个地点右侧地图定位到其地理位置并显示具体 ...

  2. android 百度地图禁止双击放大缩小,百度地图API 在使用点聚合时,如果放大、缩小或移动地图时,添加的文字标签会消失...

    问题:如题,百度地图API 在使用点聚合时,如果放大.缩小或移动地图时,添加的文字标签(label)会消失. 原因:API代码中的map.removeOverlay(marker),在这句话执行的时候 ...

  3. echarts引入百度地图并且添加点击事件

    echarts引入百度地图并且添加点击事件 (1)下载依赖项 npm install echarts --save (2)引入echarts到页面当中 import * as echarts from ...

  4. vue + 百度地图

    之所以写这篇博客,是因为我发现了神器,并且很好用,同时也被困扰了很长时间,所以希望遇到的小伙伴可以迎刃而解. 一步步来吧! 1.安装地图 npm install vue-baidu-map 2.在ma ...

  5. vue 百度地图 + 定位

    vue 百度地图 + 定位 > 前提需要自己有百度的密钥,如没有可以去百度地图申请 一.在主目录下的index.html引入js,例如: 二.在webpack.base.conf.js配置文件中 ...

  6. 利用ECharts3来实现ECharts2实例中的模拟迁徙效果,即背景地图为百度地图。 标签: ECharts3Echarts2模拟迁徙百度地图引入 2016-10-24 16:21 10065人阅

    利用ECharts3来实现ECharts2实例中的模拟迁徙效果,即背景地图为百度地图. 标签: ECharts3Echarts2模拟迁徙百度地图引入 2016-10-24 16:21  10065人阅 ...

  7. vue中百度地图使用及自定义点聚合样式

    百度地图使用及点聚合功能 由于后台固定了百度地图,百度地图和高德地图经纬度转换有次数限制,不能满足项目的正常运行,所以切换了百度地图,下面对使用中遇到的问题进行记录: 安装及使用百度地图: npm i ...

  8. 百度地图离线开发demo(vue+百度地图3.0+百度瓦片)(仅供参考,学习探讨)

    公司需求开发离线地图功能.搜索学习,踩坑,试验(由于参考借鉴了n多文章,就不分别贴出对应文章了,感谢分享...),最终整合了一套集成vue的离线地图开发方案,文章将分享一整套的解决方案思路与方式.后续 ...

  9. vue 百度地图绘制点线面

    vue使用百度地图 1.在index.html 中直接引用 <script type="text/javascript" src="http://api.map.b ...

最新文章

  1. 给老婆写个Python教程
  2. 网络协议必会知识点:互联网网络分层
  3. linux开始时间and结束时间,Linux NTP configure and Hangcheck-time
  4. 在Windows 7中的Windows Media Player 12中快速预览歌曲
  5. 小程序 ajax 加载,小程序实战-小程序网络请求异步加载
  6. hadoop 单机单间_Hadoop单机模式配置
  7. SpringBoot——项目启动时读取配置及初始化资源
  8. 红橙Darren视频笔记 IOC注解框架 自己写个注解框架
  9. Centos 7 Puppet之foreman介绍安装测试
  10. c语言的真随机,怎样让c语言中的随机函数真正随机?
  11. 手机屏幕物理点击器是什么原理_手机屏幕物理连点器
  12. Bzoj 3654 图样图森波 题解
  13. ctfshow终极考核(一键通关脚本)
  14. 微信小程序获取手机号,前端解密手机号,微信sessionKey过期,微信手机号授权
  15. papers with code介绍(人工智能方向研究生的必备网站)
  16. 探寻江南虞山之美 尽享与观致7的一场约会
  17. MicroNet实战:使用MicroNet实现图像分类(二)
  18. 十年磨一剑:蚂蚁集团可观测性平台 AntMonitor
  19. 天翼云网站访问不了解决办法
  20. windows phone 应用提交商店失败总结

热门文章

  1. gkui服务器下发消息超时,gkui服务器下发消息超时
  2. 我的作品 电子元件替换速查字典V5.0
  3. 【逐函数讲解ORB_SLAM2源码】4.计算umax
  4. 说说Elasticsearch Segment合并
  5. Codeforces 30 E. Tricky and Cleve Password
  6. char 与Unicode编码
  7. 金绿宝石chrysopal与猫眼石cymophanite区别
  8. 英语国际音标发音——见词能读
  9. java编程源代码_java经典编程300例源代码下载
  10. SpringBoot之监控器/AOP/拦截器的使用及执行顺序