indexall.vue
7.92 KB
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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<template>
<div>
<div id="container"></div>
<div class="search-box" v-if="isx">
<input v-model="searchQuery" placeholder="输入地址进行搜索" />
<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>
</div>
</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,
searchQuery: '', // 搜索查询
};
},
props: {
isx: {
type: Boolean,
default: true
},
lat: {
type: Number,
default: 30.67
},
lng: {
type: Number,
default: 104.06
},
message:{
type: String,
default: ''
},
},
mounted() {
this.initMap();
this.setActiveType('polygon')
},
methods: {
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;
const latLng = new TMap.LatLng(location.lat, location.lng);
// 移动地图中心到搜索到的位置
this.map.setCenter(latLng);
} else {
this.$message.error('未找到相关地址');
}
}).catch(err => {
this.$message.error("搜索失败");
console.error(err);
});
},
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), // 设置地图中心点坐标
mapStyleId: 'style1'
});
var path = [];
if(this.message != '') {
let listall = JSON.parse(this.message)
for (let index = 0; index < listall.length; index++) {
const element = listall[index];
const c1 = new TMap.LatLng(Number(element.lat),Number(element.lng))
path.push(c1)
}
}
console.error(path )
console.error(this.message )
// 初始化几何图形及编辑器
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');
}
.search-box {
position: absolute;
top: 10px;
left: 10px;
z-index: 1000;
/* 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;
}
.search-box input {
/* padding: 5px; */
padding: 15px 8px;
margin-right: 5px;
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;
}
.search-box button {
padding: 15px 0;
background-color: #fff;
/* color: #fff; */
border: none;
border-radius: 3px;
cursor: pointer;
height: 100%;
box-sizing: border-box;
font-size: 10px;
color: -internal-light-dark(black, white);
}
.search-box button:hover {
background-color: #fff;
}
</style>