Blame view

admin-web-master/src/components/newmap/index.vue 8.73 KB
d64cd58f   wesley88   上传验收小程序
1
  <template>
96898da2   wesley88   1
2
    <div style="width: 100%;">
d64cd58f   wesley88   上传验收小程序
3
      <div id="mapContainer" class="map"></div>
96898da2   wesley88   1
4
      <div class="search-box" v-if="isx">
cf438595   wesley88   1
5
        <input v-model="searchQuery" placeholder="输入地址进行搜索" />
96898da2   wesley88   1
6
7
8
9
        <div style="box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);display: flex;align-items: center;background: #fff;padding: 0 20px;border-radius: 5px;overflow: hidden;margin-left: 10px;">
          <i class="el-icon-search" style="margin-right: 10px;color: #3F9B6A;"></i>
          <button type="button" @click="searchLocation">搜索</button>
        </div>
cf438595   wesley88   1
10
11
12
13
14
15
16
17
      </div>
      <div v-if="searchResults.length > 0" class="search-results">
        <ul>
          <li v-for="(result, index) in searchResults" :key="index" @click="selectResult(result)">
            {{ result.title }}
          </li>
        </ul>
      </div>
d64cd58f   wesley88   上传验收小程序
18
19
    </div>
  </template>
d64cd58f   wesley88   上传验收小程序
20
21
22
23
24
25
26
27
28
29
30
31
  <script>
    export default {
      name: 'TencentMap',
      props: {
        lat: {
          type: Number,
          default: 30.67
        },
        lng: {
          type: Number,
          default: 104.06
        },
cf438595   wesley88   1
32
        isonloed: {
118fc86d   wesley88   1
33
34
          type: Boolean,
          default: false
a182f238   wesley88   1
35
        },
cf438595   wesley88   1
36
        isx: {
a182f238   wesley88   1
37
38
39
          type: Boolean,
          default: true
        },
cf438595   wesley88   1
40
        message: {
a182f238   wesley88   1
41
42
43
          type: String,
          default: ''
        },
d64cd58f   wesley88   上传验收小程序
44
45
46
47
48
      },
      data() {
        return {
          map: null,
          marker: null,
cf438595   wesley88   1
49
50
51
52
          address: '',
          searchQuery: '', // 搜索查询
          searchResults: [], // 搜索结果列表
  
d64cd58f   wesley88   上传验收小程序
53
54
55
56
        };
      },
      mounted() {
        // 初始化地图
47e4e5dc   wesley88   1
57
58
59
60
61
        let that = this;
        // 延迟2秒执行,避免地图加载失败
        setTimeout(() => {
          that.initMap();
        }, 2000);
d64cd58f   wesley88   上传验收小程序
62
63
      },
      methods: {
cf438595   wesley88   1
64
65
        // 选择某一条搜索结果
        selectResult(result) {
cf438595   wesley88   1
66
  
96898da2   wesley88   1
67
68
69
70
71
72
73
74
75
76
77
78
79
80
          console.error('====')
          let lat = result.location.lat;
          let lng = result.location.lng;
          
          //修改点标记的位置
          this.marker.updateGeometries([
            {
              "styleId":"myStyle",
              "id": "1",
              "position": new TMap.LatLng(lat, lng),
            }
          ])
  
          // const latLng = new TMap.maps.LatLng(result.location.lat, result.location.lng);
cf438595   wesley88   1
81
  
96898da2   wesley88   1
82
83
84
85
86
          // // 移动地图中心到选择的位置
          // this.map.setCenter(latLng);
  
          // // 更新标记位置
          // this.marker.setPosition(latLng);
cf438595   wesley88   1
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
  
          // 获取地址信息
          this.getAreaCode(result.location.lat, result.location.lng);
  
          
        },
        searchLocation() {
          let that = this
          if (!this.searchQuery) {
            this.$message.error('请输入搜索地址');
            return;
          }
  
          // 使用腾讯地图的搜索服务
          this.$jsonp("https://apis.map.qq.com/ws/place/v1/search", {
            keyword: this.searchQuery,
            key: 'PGRBZ-Z3FRJ-DTYFB-XNX4X-DC6HZ-MCFYU', // 使用你的腾讯地图API key
            output: "jsonp",
            boundary: 'nearby(' + that.lat + ',' + that.lng + ',1000,1)'
          }).then((res) => {
            console.error(res)
            if (res.status === 0 && res.data.length > 0) {
              this.searchResults = res.data; // 存储搜索结果
            } else {
              this.$message.error('未找到相关地址');
              this.searchResults = []; // 清空搜索结果
            }
          }).catch(err => {
            this.$message.error("搜索失败");
            console.error(err);
          });
        },
d64cd58f   wesley88   上传验收小程序
119
        initMap() {
2210df30   wesley88   1
120
121
122
          if (this.isx) {
          
          } else {
96898da2   wesley88   1
123
124
            this.lat = Number(this.message.split(',')[0])
            this.lng = Number(this.message.split(',')[1])
2210df30   wesley88   1
125
          }
d64cd58f   wesley88   上传验收小程序
126
          // 将经纬度转换为腾讯地图的LatLng对象
96898da2   wesley88   1
127
          const centerLatLng = new TMap.LatLng(this.lat, this.lng);
d64cd58f   wesley88   上传验收小程序
128
  
96898da2   wesley88   1
129
          this.map = new TMap.Map(document.getElementById("mapContainer"), {
d64cd58f   wesley88   上传验收小程序
130
            center: centerLatLng,
84cb5fa7   wesley88   1
131
            zoom: 13,
96898da2   wesley88   1
132
            mapStyleId: 'style1'
d64cd58f   wesley88   上传验收小程序
133
          });
a182f238   wesley88   1
134
  
d64cd58f   wesley88   上传验收小程序
135
          // 创建标记
cf438595   wesley88   1
136
          if (this.isonloed) {
96898da2   wesley88   1
137
138
139
140
141
142
143
144
145
146
            this.marker = new TMap.MultiMarker({
              map: this.map,  //指定地图容器
              //点标记数据数组
              geometries: [
                {
                  "styleId":"myStyle",
                  "id": "1",
                  "position": new TMap.LatLng(this.lat,this.lng),
                }
              ]
118fc86d   wesley88   1
147
148
            });
          } else {
96898da2   wesley88   1
149
            this.marker = new TMap.MultiMarker({
118fc86d   wesley88   1
150
151
152
              map: this.map
            });
          }
cf438595   wesley88   1
153
          if (this.isx) {
96898da2   wesley88   1
154
155
156
            // TMap.maps.event.addListener(this.map, 'click', this.onMapClick);
  
            this.map.on("click",this.onMapClick)
a182f238   wesley88   1
157
          } else {
96898da2   wesley88   1
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
            // const centerLatLng1 = new TMap.maps.LatLng(this.message.split(',')[0], this.message.split(',')[1]);
            // console.error(this.message)
            // this.marker = new TMap.maps.Marker({
            //   position: centerLatLng1,
            //   map: this.map
            // });
            this.marker = new TMap.MultiMarker({
              map: this.map,  //指定地图容器
              //点标记数据数组
              geometries: [
                {
                  "styleId":"myStyle",
                  "id": "1",
                  "position": new TMap.LatLng(Number(this.message.split(',')[0]), Number(this.message.split(',')[1])),
                }
              ]
a182f238   wesley88   1
174
175
            });
          }
d64cd58f   wesley88   上传验收小程序
176
177
178
179
180
  
          // 添加点击事件监听器
          // qq.maps.event.addListener(this.marker, 'click', this.onMarkerClick);
  
          // 添加地图点击事件监听器
cf438595   wesley88   1
181
  
d64cd58f   wesley88   上传验收小程序
182
183
184
185
186
187
188
  
          // // 获取地址信息
          // this.getAreaCode(this.lat, this.lng);
        },
        onMarkerClick() {
          alert('您点击了标记点');
        },
96898da2   wesley88   1
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
        onMapClick(evt) {
          console.error('====')
          let lat = evt.latLng.getLat().toFixed(6);
          let lng = evt.latLng.getLng().toFixed(6);
          
          //修改点标记的位置
          this.marker.updateGeometries([
            {
              "styleId":"myStyle",
              "id": "1",
              "position": new TMap.LatLng(lat, lng),
            }
          ])
          
          // // 回显经纬度
          // _this.$emit('PositionDegrees', {lng, lat})
          // // 获取点击位置的经纬度
          // const newLatLng = event.latLng;
d64cd58f   wesley88   上传验收小程序
207
  
96898da2   wesley88   1
208
209
210
211
          // // 更新标记位置
          // this.marker.setPosition(newLatLng);
          this.getAreaCode(lat,lng);
          // // 更新父组件中的经纬度
d64cd58f   wesley88   上传验收小程序
212
213
  
  
96898da2   wesley88   1
214
          // // 获取新的地址信息
d64cd58f   wesley88   上传验收小程序
215
216
217
  
        },
        getAreaCode(lat, lng) {
cf438595   wesley88   1
218
219
220
          this.searchQuery = ''
          // 清空搜索结果
          this.searchResults = [];
d64cd58f   wesley88   上传验收小程序
221
222
223
224
225
226
227
228
229
230
231
          let that = this;
          //这里可以直接this.$jsonp地址传入你的经纬度;
          that.$jsonp("https://apis.map.qq.com/ws/geocoder/v1/?", {
            location: `${lat},${lng}`, // 经纬度
            key: 'PGRBZ-Z3FRJ-DTYFB-XNX4X-DC6HZ-MCFYU', //这里就是要开启那个service不然会报错让你开启
            output: "jsonp", // output必须jsonp 不然会超时
          }).then((res) => {
            if (res.message == 'Success') {
              console.log(res);
              that.address = res.result.address
              that.$emit('updatecenter', {
96898da2   wesley88   1
232
233
                lat: Number(lat),
                lng: Number(lng),
d64cd58f   wesley88   上传验收小程序
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
                address: that.address
              });
            } else {
              this.$message.error(res.message)
  
            }
          }).catch(err => {
            this.$message.error("获取城市编码失败")
            console.error(err)
          })
        }
      }
    }
  </script>
  
  <style scoped>
    .map {
      width: 100%;
      height: 300px;
    }
  
cf438595   wesley88   1
255
    .search-box {
d64cd58f   wesley88   上传验收小程序
256
      position: absolute;
cf438595   wesley88   1
257
258
      top: 10px;
      left: 10px;
d6ddfcc4   wesley88   1
259
      z-index: 1000;
96898da2   wesley88   1
260
261
262
263
264
265
266
      /* background: #fff; */
      /* padding: 10px; */
      /* border-radius: 5px; */
      /* box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1); */
      /* width: 280px; */
      display: flex;
      justify-content: space-between;
cf438595   wesley88   1
267
268
269
    }
  
    .search-box input {
96898da2   wesley88   1
270
271
      /* padding: 5px; */
      padding: 15px 8px;
cf438595   wesley88   1
272
      margin-right: 5px;
96898da2   wesley88   1
273
274
275
276
277
278
      font-size: 10px;
      box-sizing: border-box;
      border-radius: 5px;
      border: 1px solid #eee;
      box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
      width: 200px;
cf438595   wesley88   1
279
280
281
    }
  
    .search-box button {
96898da2   wesley88   1
282
283
284
      padding: 15px 0;
      background-color: #fff;
      /* color: #fff; */
cf438595   wesley88   1
285
286
287
      border: none;
      border-radius: 3px;
      cursor: pointer;
96898da2   wesley88   1
288
289
290
291
      height: 100%;
      box-sizing: border-box;
      font-size: 10px;
      color: -internal-light-dark(black, white);
cf438595   wesley88   1
292
293
294
    }
  
    .search-box button:hover {
96898da2   wesley88   1
295
      background-color: #fff;
cf438595   wesley88   1
296
297
298
299
300
301
    }
  
    .search-results {
      position: absolute;
      top: 60px;
      left: 10px;
96898da2   wesley88   1
302
      z-index: 1000;
cf438595   wesley88   1
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
      background: #fff;
      padding: 10px;
      border-radius: 5px;
      box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
      max-height: 200px;
      overflow-y: auto;
      width: 280px;
      
    }
    .search-results::-webkit-scrollbar {
      display: none; /* 隐藏滚动条 */
    }
    .search-results ul {
      list-style: none;
      padding: 0;
      margin: 0;
    }
  
    .search-results li {
      padding: 5px;
      cursor: pointer;
    }
  
    .search-results li:hover {
      background-color: #f0f0f0;
d64cd58f   wesley88   上传验收小程序
328
    }
cf438595   wesley88   1
329
  </style>