点聚合

BmlMarkerClusterer 能够聚合它包含的所有 BmMarker 组件。

属性

属性名类型默认值描述
gridSizeNumber网格大小
maxZoomNumber聚合的最大缩放级别
minClusterSizeNumber单个聚合的最小数量
stylesArray[{url, size, opt_anchor, textColor, opt_textSize}][]聚合的样式风格集合
averageCenterBooleanfalse单个聚合的落脚点是否是聚合内所有标记的平均中心

示例

聚合动态添加的点坐标

<template>
  <div>
    <baidu-map class="map" center="中国" :scroll-wheel-zoom="true" :double-click-zoom="true">
      <bml-marker-clusterer :averageCenter="true" :styles="styles">
        <bm-marker v-for="marker of markers" :key="marker" :position="{lng: marker.lng, lat: marker.lat}" @dragend="updateMarker($event, marker)" :dragging="true"></bm-marker>
      </bml-marker-clusterer>
    </baidu-map>
    <button class="md-raised md-primary" @click="addMarker">添加一个随机点</button>
    <button class="md-raised md-primary" @click="removeMarker">删除上一个点</button>
    <button class="md-raised md-primary" @click="changeStyles">更换皮肤</button>
  </div>
</template>


<script setup>
import { ref } from 'vue';


const markers = ref([]);
const styles = ref([]);
const customStyles = ref(false);

const addMarker = () => {
  const position = { lng: Math.random() * 40 + 85, lat: Math.random() * 30 + 21 }
  markers.value.push(position)
};

const updateMarker = ({ point }, marker) => {
  marker.lng = point.lng
  marker.lat = point.lat
};
const removeMarker = () => {
  markers.value.pop()
};
const changeStyles = () => {
  customStyles.value = !customStyles.value
  const EXAMPLE_URL = "http://api.map.baidu.com/library/MarkerClusterer/1.2/examples/"
  const stylesArr = [{
    url: EXAMPLE_URL + 'images/heart30.png',
    size: {
      width: 30,
      height: 26
    },
    opt_anchor: [16, 0],
    textColor: '#ff00ff',
    opt_textSize: 10
  }, {
    url: EXAMPLE_URL + 'images/heart40.png',
    size: {
      width: 40,
      height: 35
    },
    opt_anchor: [40, 35],
    textColor: '#ff0000',
    opt_textSize: 12
  }, {
    url: EXAMPLE_URL + 'images/heart50.png',
    size: {
      width: 50,
      height: 44
    },
    opt_anchor: [32, 0],
    textColor: 'white',
    opt_textSize: 14
  }]
  styles.value = customStyles.value ? stylesArr : []
};

const getMarkers = () => {
  for (let i = 0; i < 10; i++) {
    const position = { lng: Math.random() * 40 + 85, lat: Math.random() * 30 + 21 }
    markers.value.push(position);
  }
};

getMarkers();
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77