0c8ae4d9
wesley88
1
|
1
2
3
|
<template>
<div>
<div id="container"></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
|
</div>
|
0c8ae4d9
wesley88
1
|
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
</div>
</template>
<script>
export default {
name: 'IndexAll',
data() {
return {
map: null, // 地图
editor: null, // 编辑器
activeType: 'marker', // 激活的图形编辑类型
marker: null,
polyline: null,
polygon: null,
circle: null,
rectangle: null,
ellipse: null,
|
cf438595
wesley88
1
|
28
|
searchQuery: '', // 搜索查询
|
0c8ae4d9
wesley88
1
|
29
30
31
|
};
},
props: {
|
96898da2
wesley88
1
|
32
33
34
35
|
isx: {
type: Boolean,
default: true
},
|
0c8ae4d9
wesley88
1
|
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
lat: {
type: Number,
default: 30.67
},
lng: {
type: Number,
default: 104.06
},
message:{
type: String,
default: ''
},
},
mounted() {
|
47e4e5dc
wesley88
1
|
50
51
52
53
54
55
|
let that = this;
// 延迟2秒执行,避免地图加载失败
setTimeout(() => {
that.initMap();
that.setActiveType('polygon')
}, 2000);
|
0c8ae4d9
wesley88
1
|
56
57
|
},
methods: {
|
cf438595
wesley88
1
|
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
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) {
const location = res.data[0].location;
|
96898da2
wesley88
1
|
75
|
const latLng = new TMap.LatLng(location.lat, location.lng);
|
cf438595
wesley88
1
|
76
77
78
79
80
81
82
83
84
85
86
|
// 移动地图中心到搜索到的位置
this.map.setCenter(latLng);
} else {
this.$message.error('未找到相关地址');
}
}).catch(err => {
this.$message.error("搜索失败");
console.error(err);
});
},
|
0c8ae4d9
wesley88
1
|
87
88
89
90
91
92
93
94
95
|
setActiveType(type) {
this.activeType = type;
this.editor.setActiveOverlay(type);
},
initMap() {
// 初始化地图
this.map = new TMap.Map('container', {
zoom: 12, // 设置地图缩放级别
center: new TMap.LatLng(this.lat, this.lng), // 设置地图中心点坐标
|
96898da2
wesley88
1
|
96
|
mapStyleId: 'style1'
|
0c8ae4d9
wesley88
1
|
97
98
99
100
101
102
|
});
var path = [];
if(this.message != '') {
let listall = JSON.parse(this.message)
for (let index = 0; index < listall.length; index++) {
const element = listall[index];
|
96898da2
wesley88
1
|
103
|
const c1 = new TMap.LatLng(Number(element.lat),Number(element.lng))
|
0c8ae4d9
wesley88
1
|
104
105
106
|
path.push(c1)
}
}
|
96898da2
wesley88
1
|
107
108
|
console.error(path )
console.error(this.message )
|
0c8ae4d9
wesley88
1
|
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
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
201
202
203
204
205
206
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
261
262
263
264
265
266
267
268
269
|
// 初始化几何图形及编辑器
this.marker = new TMap.MultiMarker({
map: this.map,
});
this.polyline = new TMap.MultiPolyline({
map: this.map,
});
this.polygon = new TMap.MultiPolygon({
map: this.map,
geometries: [
{
'id': 'p1', //该多边形在图层中的唯一标识(删除、更新数据时需要)
'styleId': 'polygon', //绑定样式名
'paths': path, //多边形轮廓
}
]
});
this.circle = new TMap.MultiCircle({
map: this.map,
});
this.rectangle = new TMap.MultiRectangle({
map: this.map,
});
this.ellipse = new TMap.MultiEllipse({
map: this.map,
});
this.editor = new TMap.tools.GeometryEditor({
map: this.map, // 编辑器绑定的地图对象
overlayList: [
{
overlay: this.marker,
id: 'marker',
},
{
overlay: this.polyline,
id: 'polyline',
},
{
overlay: this.polygon,
id: 'polygon',
},
{
overlay: this.circle,
id: 'circle',
},
{
overlay: this.rectangle,
id: 'rectangle',
},
{
overlay: this.ellipse,
id: 'ellipse',
},
],
actionMode: TMap.tools.constants.EDITOR_ACTION.DRAW, // 编辑器的工作模式
activeOverlayId: 'marker', // 激活图层
snappable: true, // 开启吸附
});
// 监听绘制结束事件,获取绘制几何图形
this.editor.on('draw_complete', (geometry) => {
const id = geometry.id;
if (this.editor.getActiveOverlay().id === 'rectangle') {
const geo = this.rectangle.geometries.filter((item) => item.id === id);
console.log('绘制的矩形定位的坐标:', geo[0].paths);
}
if (this.editor.getActiveOverlay().id === 'polygon') {
// console.error(this.polygon.geometries)
const geo = this.polygon.geometries.filter((item) => item.id === id);
console.log('绘制的多边形坐标:', geo[0].paths);
// var path = [
// ];
// for (let index = 0; index < geo[0].paths.length; index++) {
// const element = geo[0].paths[index];
// const c1 = new TMap.LatLng(element.lat,element.lng)
// path.push(c1)
// }
// // 计算多边形形心
// var position = TMap.geometry.computeCentroid(path);
// console.log('绘制的多边形坐标:',position);
this.$emit('updatecenter', geo[0].paths);
}
});
},
},
};
</script>
<style scoped>
html,
body {
height: 100%;
margin: 0px;
padding: 0px;
}
#container {
width: 100%;
height: 300px;
}
#toolControl {
position: absolute;
top: 10px;
left: 0px;
right: 0px;
margin: auto;
width: 252px;
z-index: 1001;
}
.toolItem {
width: 30px;
height: 30px;
float: left;
margin: 1px;
padding: 4px;
border-radius: 3px;
background-size: 30px 30px;
background-position: 4px 4px;
background-repeat: no-repeat;
box-shadow: 0 1px 2px 0 #e4e7ef;
background-color: #ffffff;
border: 1px solid #ffffff;
}
.toolItem:hover {
border-color: #789cff;
}
.active {
border-color: #d5dff2;
background-color: #d5dff2;
}
#marker {
background-image: url('https://mapapi.qq.com/web/lbs/javascriptGL/demo/img/marker_editor.png');
}
#polyline {
background-image: url('https://mapapi.qq.com/web/lbs/javascriptGL/demo/img/polyline.png');
}
#polygon {
background-image: url('https://mapapi.qq.com/web/lbs/javascriptGL/demo/img/polygon.png');
}
#circle {
background-image: url('https://mapapi.qq.com/web/lbs/javascriptGL/demo/img/circle.png');
}
#rectangle {
background-image: url('https://mapapi.qq.com/web/lbs/javascriptGL/demo/img/rectangle.png');
}
#ellipse {
background-image: url('https://mapapi.qq.com/web/lbs/javascriptGL/demo/img/ellipse.png');
}
|
cf438595
wesley88
1
|
270
271
272
273
|
.search-box {
position: absolute;
top: 10px;
left: 10px;
|
2210df30
wesley88
1
|
274
|
z-index: 1000;
|
96898da2
wesley88
1
|
275
276
277
278
279
280
281
|
/* 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
|
282
283
284
|
}
.search-box input {
|
96898da2
wesley88
1
|
285
286
|
/* padding: 5px; */
padding: 15px 8px;
|
cf438595
wesley88
1
|
287
|
margin-right: 5px;
|
96898da2
wesley88
1
|
288
289
290
291
292
293
|
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
|
294
295
296
|
}
.search-box button {
|
96898da2
wesley88
1
|
297
298
299
|
padding: 15px 0;
background-color: #fff;
/* color: #fff; */
|
cf438595
wesley88
1
|
300
301
302
|
border: none;
border-radius: 3px;
cursor: pointer;
|
96898da2
wesley88
1
|
303
304
305
306
|
height: 100%;
box-sizing: border-box;
font-size: 10px;
color: -internal-light-dark(black, white);
|
cf438595
wesley88
1
|
307
308
309
|
}
.search-box button:hover {
|
96898da2
wesley88
1
|
310
|
background-color: #fff;
|
cf438595
wesley88
1
|
311
|
}
|
0c8ae4d9
wesley88
1
|
312
|
</style>
|