d64cd58f
wesley88
上传验收小程序
|
1
2
3
|
<template>
<div style="position: relative;width: 100%;">
<div id="mapContainer" class="map"></div>
|
cf438595
wesley88
1
|
4
5
|
<div class="search-box">
<input v-model="searchQuery" placeholder="输入地址进行搜索" />
|
440bccda
wesley88
1
|
6
|
<button type="button" @click="searchLocation">搜索</button>
|
cf438595
wesley88
1
|
7
8
9
10
11
12
13
14
|
</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
上传验收小程序
|
15
16
|
</div>
</template>
|
d64cd58f
wesley88
上传验收小程序
|
17
18
19
20
21
22
23
24
25
26
27
28
|
<script>
export default {
name: 'TencentMap',
props: {
lat: {
type: Number,
default: 30.67
},
lng: {
type: Number,
default: 104.06
},
|
cf438595
wesley88
1
|
29
|
isonloed: {
|
118fc86d
wesley88
1
|
30
31
|
type: Boolean,
default: false
|
a182f238
wesley88
1
|
32
|
},
|
cf438595
wesley88
1
|
33
|
isx: {
|
a182f238
wesley88
1
|
34
35
36
|
type: Boolean,
default: true
},
|
cf438595
wesley88
1
|
37
|
message: {
|
a182f238
wesley88
1
|
38
39
40
|
type: String,
default: ''
},
|
d64cd58f
wesley88
上传验收小程序
|
41
42
43
44
45
|
},
data() {
return {
map: null,
marker: null,
|
cf438595
wesley88
1
|
46
47
48
49
|
address: '',
searchQuery: '', // 搜索查询
searchResults: [], // 搜索结果列表
|
d64cd58f
wesley88
上传验收小程序
|
50
51
52
53
54
55
56
|
};
},
mounted() {
// 初始化地图
this.initMap();
},
methods: {
|
cf438595
wesley88
1
|
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
|
// 选择某一条搜索结果
selectResult(result) {
const latLng = new qq.maps.LatLng(result.location.lat, result.location.lng);
// 移动地图中心到选择的位置
this.map.setCenter(latLng);
// 更新标记位置
this.marker.setPosition(latLng);
// 获取地址信息
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
上传验收小程序
|
98
|
initMap() {
|
2210df30
wesley88
1
|
99
100
101
102
103
104
|
if (this.isx) {
} else {
this.lat = this.message.split(',')[0]
this.lng = this.message.split(',')[1]
}
|
d64cd58f
wesley88
上传验收小程序
|
105
106
107
108
109
110
|
// 将经纬度转换为腾讯地图的LatLng对象
const centerLatLng = new qq.maps.LatLng(this.lat, this.lng);
// 创建地图实例
this.map = new qq.maps.Map(document.getElementById('mapContainer'), {
center: centerLatLng,
|
84cb5fa7
wesley88
1
|
111
112
|
zoom: 13,
styles: 'dark'
|
d64cd58f
wesley88
上传验收小程序
|
113
|
});
|
a182f238
wesley88
1
|
114
|
|
d64cd58f
wesley88
上传验收小程序
|
115
|
// 创建标记
|
cf438595
wesley88
1
|
116
|
if (this.isonloed) {
|
118fc86d
wesley88
1
|
117
118
119
120
121
122
123
124
125
126
|
this.marker = new qq.maps.Marker({
position: centerLatLng,
map: this.map
});
} else {
this.marker = new qq.maps.Marker({
// position: centerLatLng,
map: this.map
});
}
|
cf438595
wesley88
1
|
127
|
if (this.isx) {
|
a182f238
wesley88
1
|
128
129
130
131
132
133
134
135
136
|
qq.maps.event.addListener(this.map, 'click', this.onMapClick);
} else {
const centerLatLng1 = new qq.maps.LatLng(this.message.split(',')[0], this.message.split(',')[1]);
console.error(this.message)
this.marker = new qq.maps.Marker({
position: centerLatLng1,
map: this.map
});
}
|
d64cd58f
wesley88
上传验收小程序
|
137
138
139
140
141
|
// 添加点击事件监听器
// qq.maps.event.addListener(this.marker, 'click', this.onMarkerClick);
// 添加地图点击事件监听器
|
cf438595
wesley88
1
|
142
|
|
d64cd58f
wesley88
上传验收小程序
|
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
// // 获取地址信息
// this.getAreaCode(this.lat, this.lng);
},
onMarkerClick() {
alert('您点击了标记点');
},
onMapClick(event) {
// 获取点击位置的经纬度
const newLatLng = event.latLng;
// 更新标记位置
this.marker.setPosition(newLatLng);
this.getAreaCode(newLatLng.getLat(), newLatLng.getLng());
// 更新父组件中的经纬度
// 获取新的地址信息
},
getAreaCode(lat, lng) {
|
cf438595
wesley88
1
|
164
165
166
|
this.searchQuery = ''
// 清空搜索结果
this.searchResults = [];
|
d64cd58f
wesley88
上传验收小程序
|
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
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', {
lat: lat,
lng: lng,
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
|
201
|
.search-box {
|
d64cd58f
wesley88
上传验收小程序
|
202
|
position: absolute;
|
cf438595
wesley88
1
|
203
204
|
top: 10px;
left: 10px;
|
6eebfba2
wesley88
1
|
205
|
z-index: 99;
|
d64cd58f
wesley88
上传验收小程序
|
206
|
background: #fff;
|
cf438595
wesley88
1
|
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
padding: 10px;
border-radius: 5px;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
width: 280px;
}
.search-box input {
padding: 5px;
margin-right: 5px;
}
.search-box button {
padding: 5px 10px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 3px;
cursor: pointer;
}
.search-box button:hover {
background-color: #0056b3;
}
.search-results {
position: absolute;
top: 60px;
left: 10px;
z-index: 999;
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
上传验收小程序
|
261
|
}
|
cf438595
wesley88
1
|
262
|
</style>
|