rk
23 小时以前 af4fc982131b9a7f40fb4f2a4fd8780d7cdc0fc4
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<template>
  <view class="content">
    <map
            id="map"
            class="map"
            :show-location="true"
            :latitude="latitude"
            :longitude="longitude"
            @tap="tapMap"
        ></map>
  </view>
</template>
 
<script>
  const img = '/static/logo.png';
 
  export default {
    data() {
      return {
        latitude: 23.099994,
        longitude: 113.324520,
      }
    },
        onShow() {
            uni.getLocation({
                type: 'wgs84',
                success: (location) => {
                    console.log(location);
                    this.latitude = location.latitude
                    this.longitude = location.longitude
                },
                fail: (e) => {
                    console.log(e);
                }
            })
        },
    onReady() {
            
      this._mapContext = uni.createMapContext("map", this);
      // 仅调用初始化,才会触发 on.("markerClusterCreate", (e) => {})
      this._mapContext.initMarkerCluster({
        enableDefaultStyle: false,
        zoomOnClick: true,
        gridSize: 60,
        complete(res) {
          console.log('initMarkerCluster', res)
        }
      });
 
      this._mapContext.on("markerClusterCreate", (e) => {
        console.log("markerClusterCreate", e);
      });
            this._mapContext.getCenterLocation({
                success: (location) => {
                    console.log(location);
                },
                fail: (e) => {
                    console.log(e);
                }
            }) 
      this.addMarkers();
    },
    methods: {
      addMarkers() {
        const positions = [
          {
            latitude: 23.099994,
            longitude: 113.324520,
          }, {
            latitude: 23.099994,
            longitude: 113.322520,
          }, {
            latitude: 23.099994,
            longitude: 113.326520,
          }, {
            latitude: 23.096994,
            longitude: 113.329520,
          }
        ]
 
        const markers = []
 
        positions.forEach((p, i) => {
          markers.push(
            Object.assign({},{
              id: i + 1,  
              iconPath: img,  
              width: 50,  
              height: 50,  
              joinCluster: true, // 指定了该参数才会参与聚合  
              label: {  
                  width: 50,  
                  height: 30,  
                  borderWidth: 1,  
                  borderRadius: 10,  
                  bgColor: '#ffffff',  
                  content: `label ${i + 1}`
              }  
            },p)
          )
        })
                debugger
        this._mapContext.addMarkers({
            markers,
            clear: false,
            complete(res) {
              console.log('addMarkers', res)
            }
        })
      },
            tapMap(e) {
                console.log(e);
                this._mapContext.addMarkers({
                    clear: true,
                    markers: [{
                        id: 'temp',
                        iconPath: img,  
                        width: 50,  
                        height: 50,  
                        joinCluster: true, // 指定了该参数才会参与聚合  
                        label: {  
                            width: 50,  
                            height: 30,  
                            borderWidth: 1,  
                            borderRadius: 10,  
                            bgColor: '#ffffff',  
                            content: `temp`
                        },
                        ...e.detail
                    }],
                    complete(res) {
                        console.log('addMarkers', res)
                    }
                })
    //             let that = this;
    //             let id = e.currentTarget.id;
    //             let maps = uni.createMapContext("map", this);
    
    //             maps.onclick = function (point) {
    //                 console.log(point);
    
    //                 point.iconPath = "/static/c1.png";
    //                 that.markers = that.markers.concat(point);
    
    //                 uni.showToast({
    //                     title: "添加成功",
    //                     icon: "none",
    //                 });
    //             };
                
            }
    } 
  }
</script>
 
<style>
  .content {
    flex: 1;
        height: 100%;
  }
 
  .map {
    flex: 1;
        width: 100%;
        height: 100%;
  }
</style>