高德地图 坐标选取、区域选择 组件

代码地址: Gitee

坐标拾取工具

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
<template>
<div class="coord-picker">
<div class="map-container">
<amap
cache-key="coord-picker-map"
mmap-style="amap://styles/whitesmoke"
async
:center.sync="center"
:zoom.sync="zoom"
is-hotspot
@click="onMapClick"
>
<amap-satellite-layer :visible="satellite" />

<amap-marker v-if="position" :position.sync="position" draggable />

<a-card
:body-style="{
'max-height': '450px',
'overflow-y': 'scroll',
'padding-top': 0,
}"
class="result-panel"
>
<template slot="title">
<template v-if="mode === 'search'">
<a-input-group compact style="display: flex">
<a-auto-complete
v-model="query"
:data-source="tips"
placeholder="输入关键词"
style="flex: 1"
@search="autoComplete"
/>
<a-button
@click="search(true)"
:disabled="!query"
type="primary"
>
搜索
</a-button>
</a-input-group>
</template>
<template v-if="mode === 'result'">
<div class="search-bar">
<a-button
icon="left"
@click="reset"
style="margin-right: 6px;"
/>
<span class="text"
>搜索 {{ query }}
{{ searching ? '...' : total }} 条结果</span
>
</div>
</template>
</template>

<a-list
v-if="mode === 'result'"
:data-source="results"
:loading="searching"
item-layout="vertical"
size="small"
class="result-list"
>
<a-pagination
slot="header"
v-if="total > 0"
v-model="pageIndex"
:page-size="pageSize"
:total="total"
size="small"
/>
<a-list-item slot="renderItem" slot-scope="item" :key="item.id">
<a-list-item-meta :description="item.address">
<span
slot="title"
style="cursor: pointer;"
@click="focus(item)"
>{{ item.name }}</span
>
</a-list-item-meta>
</a-list-item>
<a-pagination
slot="footer"
v-if="total > 0"
v-model="pageIndex"
:page-size="pageSize"
:total="total"
size="small"
/>
</a-list>
</a-card>

<a-form class="info ant-card ant-card-bordered" layout="inline">
<a-form-item label="坐标" v-if="position">
<a-input read-only :value="positionText" style="width: 200px;" />
</a-form-item>
<a-form-item label="卫星图">
<a-switch v-model="satellite" />
</a-form-item>
</a-form>
</amap>
</div>
</div>
</template>

<script>
import { loadAmap, loadPlugins } from '@amap/amap-vue';

export default {
data() {
return {
mode: 'search',
center: null,
zoom: 10,
query: '',
searching: false,
tips: [],
results: [],
position: null,
pageIndex: 1,
pageSize: 10,
total: 0,
satellite: false,
};
},
computed: {
wrapper() {
return this.$refs.wrapper;
},
positionText() {
if (!this.position) return '';
return `${this.position[0]}, ${this.position[1]}`;
},
},
created() {
this.$ready = new Promise(async resolve => {
const AMap = await loadAmap();
await loadPlugins(['AMap.PlaceSearch', 'AMap.AutoComplete']);

this.ps = new AMap.PlaceSearch({
pageSize: this.pageSize,
});
this.ac = new AMap.AutoComplete();

resolve();
});
},
methods: {
onMapClick(e) {
if (e.lnglat) {
this.position = [e.lnglat.lng, e.lnglat.lat];
} else {
this.position = null;
}
},
async search(clear = false) {
this.mode = 'result';
await this._ready;

if (clear) {
this.results = [];
this.total = 0;
this.pageIndex = 1;
this.ps.setPageIndex(1);
}

this.searching = true;
const { query } = this;
this.ps.search(query, (status, result) => {
this.searching = false;
if (query !== this.query) return;

if (status === 'complete' && result.poiList) {
this.results = result.poiList.pois;
this.total = result.poiList.count;
} else {
this.results = [];
this.total = 0;
}
});
},
async autoComplete(kw) {
if (!kw) {
this.tips = [];
} else {
this.ac.search(kw, (status, result) => {
if (kw !== this.query) return;
if (status === 'complete' && result.tips) {
this.tips = Array.from(new Set(result.tips.map(tip => tip.name)));
} else {
this.tips = [];
}
});
}
},
focus(poi) {
const pos = [poi.location.lng, poi.location.lat];
this.position = [...pos];
this.center = [...pos];
},
reset() {
this.ps.setPageIndex(1);
this.results = [];
this.tips = [];
this.total = 0;
this.mode = 'search';
},
},
watch: {
pageIndex(value) {
this.$ready.then(() => {
this.ps.setPageIndex(value);
this.search(false);
});
},
},
};
</script>

<style lang="less" scoped>
.map-container {
width: 100%;
height: 600px;
}

.result-panel {
position: absolute;
left: 10px;
top: 10px;
width: 320px;
display: flex;
flex-direction: column;

.search-bar {
display: flex;
align-items: center;
.text {
text-overflow: ellipsis;
flex: 1;
overflow: hidden;
white-space: nowrap;
}
}

.result-list.ant-list-loading {
min-height: 100px;
display: flex;
justify-content: center;
align-items: center;
}
}
.info {
position: absolute;
right: 10px;
top: 10px;
padding-left: 24px;
}
</style>

区域绘制

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
<template>
<demo-view hide-control @map-complete="ready = true">
<template v-slot:map-content>
<a-form class="controls" layout="inline">
<a-form-item>
<a-button-group>
<template v-if="drawer.mode === 'none'">
<a-button @click="drawer.mode = 'polygon'">多边形</a-button>
<a-button @click="drawer.mode = 'rectangle'">矩形</a-button>
<a-button @click="drawer.mode = 'circle'">圆形</a-button>
</template>
<template v-else>
<a-button @click="drawer.mode = 'none'">取消</a-button>
</template>
</a-button-group>
</a-form-item>
</a-form>
<a-form class="info" layout="inline">
<!-- prettier-ignore -->
<a-form-item label="面积" v-show="area > 0">{{ (area / 1000000).toFixed(4) }}平方公里</a-form-item>
<a-form-item v-show="area > 0">
<a-button
size="small"
shape="circle"
icon="copy"
@click="showModal"
/>
</a-form-item>
<a-form-item label="卫星图">
<a-switch v-model="satellite" />
</a-form-item>
</a-form>

<amap-satellite-layer :visible="satellite" />
<amap-scale />
<amap-mouse-tool
:mode.sync="drawer.mode"
clear-when-close
@draw="onDraw"
/>
<amap-polygon
v-if="drawer.polygon"
:path.sync="drawer.polygon"
:fill-color="styles.fill"
:stroke-color="styles.stroke"
:fill-opacity="0.5"
editable
/>
<amap-circle
v-if="drawer.circle"
:center.sync="drawer.circle.center"
:radius.sync="drawer.circle.radius"
:fill-color="styles.fill"
:stroke-color="styles.stroke"
:fill-opacity="0.5"
editable
/>
<amap-rectangle
v-if="drawer.rectangle"
:bounds.sync="drawer.rectangle"
:fill-color="styles.fill"
:stroke-color="styles.stroke"
:fill-opacity="0.5"
editable
/>
</template>
</demo-view>
</template>

<script>
import { Modal } from 'ant-design-vue';

function makeBounds(value) {
const sw = new window.AMap.LngLat(value[0][0], value[0][1]);
const ne = new window.AMap.LngLat(value[1][0], value[1][1]);
return new window.AMap.Bounds(sw, ne);
}

export default {
data() {
return {
ready: false,
styles: {
fill: '#409EFF',
stroke: '#000A58',
},
satellite: false,
drawer: {
type: 'polygon',
mode: 'none',
polygon: [
[116.416529, 39.960142],
[116.422794, 39.959287],
[116.421421, 39.953234],
[116.413181, 39.957708],
[116.413782, 39.960866],
],
circle: null,
rectangle: null,
},
};
},
computed: {
area() {
const { ready, drawer } = this;
if (!ready) return 0;
switch (drawer.type) {
case 'none':
return 0;
case 'polygon':
return window.AMap.GeometryUtil.ringArea(drawer.polygon);
case 'rectangle':
return new window.AMap.Rectangle({
bounds: makeBounds(drawer.rectangle),
}).getArea();
case 'circle':
return Math.PI * Math.pow(drawer.circle.radius, 2);
default:
return 0;
}
},
},
methods: {
onDraw(type, res) {
this.drawer.type = type;
this.drawer[type] = res;
this.drawer.mode = 'none';
},
showModal() {
const h = this.$createElement;
this.$success({
title: '当前圈选的范围',
content: h('a-textarea', {
attrs: {
style: 'height: 400px; font-family: monospace;',
},
props: {
value: JSON.stringify(this.drawer[this.drawer.type], null, ' '),
},
}),
});
},
},
watch: {
'drawer.mode'(mode) {
console.log('drawer.mode', mode);
if (mode !== 'none') {
const { drawer } = this;
drawer.type = 'none';
drawer.polygon = null;
drawer.circle = null;
drawer.rectangle = null;
}
},
},
};
</script>

<style lang="less" scoped>
.controls {
position: absolute;
left: 6px;
top: 0;
}
.info {
position: absolute;
right: 6px;
top: 0;
}
</style>


高德地图 坐标选取、区域选择 组件
https://www.ivan.fun/posts/4af15fde/
作者
IVAn
发布于
2021年9月27日
许可协议