index.vue
5.73 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
<template>
<a-modal :width="1200" title="坐标拾取" v-model:visible="mapVisible" @ok="handleOk" @cancel="handleCancel">
<a-space direction="vertical" :size="16" style="display: block;">
<a-row justify="center">
<a-col :span="12">
<a-space>
<a-input id="tipinput" v-model="searchName" placeholder="请输入关键词进行搜索" allow-clear />
<a-button type="primary" @click="search">
<template #icon>
<icon-search />
</template>
搜索
</a-button>
<div>
坐标获取结果:{{ location }}
</div>
</a-space>
</a-col>
</a-row>
<a-row justify="center">
<a-col>
<div id="container">
</div>
<div id="divCoordinate" style="display: none;"></div>
</a-col>
</a-row>
</a-space>
</a-modal>
</template>
<script lang="ts" setup>
import { onMounted, onUnmounted, ref } from "vue";
import AMapLoader from "@amap/amap-jsapi-loader";
import { Message } from "@arco-design/web-vue";
const emits = defineEmits(['success', 'cancle'])
window._AMapSecurityConfig = {
securityJsCode: "0f2d48aa1d4c1c8379efa60e68e34cae",
};
const searchName = ref<any>("");
const location = ref<any>("");
let map: any = null;
let placeSearch: any = null
let geolocation: any = null
let citysearch: any = null
let cityCode: any = ref<any>('');
let cityName: any = ref<any>('');
const loading = ref<boolean>(true);
let divCoordinate: any = null;
let auto: any = null;
//组件参数
const props = defineProps({
visible: Boolean,
})
//生成查询条件对象
const generateSearchModel = () => {
return {
location: ''
};
};
//查询表单对象
const searchModel = ref(generateSearchModel());
//显示状态
const mapVisible = ref<boolean>(props.visible);
/**
* 确定
*/
const handleOk = () => {
emits('success', location.value)
}
/**
* 取消
*/
const handleCancel = () => {
emits('cancle', '')
}
onMounted(() => {
AMapLoader.load({
key: "6bcdaa6062b344a686e6cda8c7074b0e", // 申请好的Web端开发者Key,首次调用 load 时必填
version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
plugins: ["AMap.PlaceSearch", "AMap.Geolocation", "AMap.CitySearch", 'AMap.AutoComplete'], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
})
.then((AMap) => {
map = new AMap.Map("container", {
// 设置地图容器id
viewMode: "2D", // 是否为3D地图模式
zoom: 11, // 初始化地图级别
resizeEnable: true
});
map.setDefaultCursor("pointer");
//定位
geolocation = new AMap.Geolocation({
enableHighAccuracy: true,//是否使用高精度定位,默认:true
timeout: 10000, //超过10秒后停止定位,默认:5s
position: 'RB', //定位按钮的停靠位置
offset: [10, 20], //定位按钮与设置的停靠位置的偏移量,默认:[10, 20]
zoomToAccuracy: true, //定位成功后是否自动调整地图视野到定位点
});
map.addControl(geolocation);
//城市
citysearch = new AMap.CitySearch();
geolocation.getCurrentPosition(function (status: any, result: any) {
if (status == 'complete') {
onComplete(result)
} else {
onError(result)
}
});
citysearch.getLocalCity(function (status: any, result: any) {
if (status === 'complete' && result.info === 'OK') {
if (result && result.city && result.bounds) {
var cityinfo = result.city;
var citybounds = result.bounds;
cityCode.value = result.infocode;
cityName.value = cityinfo;
// document.getElementById('info').innerHTML = '您当前所在城市:' + cityinfo;
//地图显示当前城市
map.setBounds(citybounds);
loading.value = false
placeSearch = new AMap.PlaceSearch({
map: map,
autoFitView: true // 是否自动调整地图视野使绘制的 Marker点都处于视口的可见范围
}); //构造地点查询类
divCoordinate = document.getElementById('divCoordinate');
map.on('mousemove', function (e: any) {
divCoordinate.style.display = 'block';
divCoordinate.style.left = e.originEvent.pixel.x + 50 + 'px';
divCoordinate.style.top = e.originEvent.pixel.y + 70 + 'px';
let str = `${e.lnglat.lng},${e.lnglat.lat}`;
// location.value = str;
divCoordinate.innerHTML = str;
});
map.on('mouseout', function (e: any) {
divCoordinate.style.display = 'none';
});
map.on('click', function (e: any) {
let str = `${e.lnglat.lng},${e.lnglat.lat}`;
location.value = str;
});
}
} else {
// document.getElementById('info').innerHTML = result.info;
}
});
})
.catch((e) => {
console.log(e);
});
});
const onComplete = (result: any) => {
console.log("onComplete", result);
}
const onError = (result: any) => {
console.log("onError", result);
}
const search = () => {
if (loading.value) {
Message.success("定位中..." as string)
return;
}
placeSearch.search(searchName.value);
}
onUnmounted(() => {
map?.destroy();
});
</script>
<style scoped lang="less">
#container {
width: 100%;
height: 600px;
}
#panel {
max-height: 90%;
width: 100%;
}
#divCoordinate {
display: none;
position: absolute;
height: 26px;
line-height: 26px;
padding: 0 10px;
border: 1px solid #999;
background: #fff;
z-index: 99
}
</style>