mapContent.vue
3.26 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
<template>
<div class="map-box">
<slot name="mapBox"></slot>
<div v-show="isUploaded">
<a class="map-cmd-icon location" @click="getLocation">
<span class="icon icon-location"></span>
</a>
<a class="map-cmd-icon refresh-car" @click="refresh">
<span class="icon icon-refresh-car"></span>
</a>
</div>
</div>
</template>
<script>
import { onMounted, ref } from "vue";
export default {
name: "mapContent",
props: {},
setup(props, context) {
let map = null;
const isUploaded = ref(false);
let checkLoadedTimeoutId = "";
const satelActive = ref(null);
const roadActive = ref(null);
onMounted(() => {});
const initMap = (id) => {
let check = () => {
checkLoadedTimeoutId && window.clearTimeout(checkLoadedTimeoutId);
let AMap = window.AMap;
if (AMap && typeof AMap.Map == "function") {
window.AMap = AMap;
init(id);
} else {
checkLoadedTimeoutId = check();
}
};
checkLoadedTimeoutId = check();
};
const init = (id) => {
map = new window.AMap.Map(id);
isUploaded.value = true;
context.emit("isUploaded", true);
context.emit("getData", map);
return map;
};
let currMarker = null;
// 获取定位
const getLocation = () => {
map.plugin("AMap.Geolocation", function () {
var geolocation = new window.AMap.Geolocation({
enableHighAccuracy: true,
timeout: 10000,
buttonOffset: new window.AMap.Pixel(10, 20),
zoomToAccuracy: true,
buttonPosition: "RB",
});
geolocation.getCurrentPosition();
window.AMap.event.addListener(geolocation, "complete", (data) => {
if (currMarker) {
currMarker.setMap(null);
}
currMarker = new window.AMap.Marker({
position: data.position,
content: '<span class="curr-marker"></span>',
map: map,
offset: new window.AMap.Pixel(-12, -12),
});
map.setFitView();
});
window.AMap.event.addListener(geolocation, "error", () => {});
});
};
// 刷新
const refresh = () => {
context.emit("refreshCb", map);
};
return {
initMap,
init,
checkLoadedTimeoutId,
getLocation,
satelActive,
roadActive,
refresh,
isUploaded,
};
},
};
</script>
<style lang="scss" scoped>
.map-box {
height: 100%;
}
.map-cmd-icon {
width: 40px;
height: 40px;
background: #fff;
border-radius: 50%;
position: fixed;
z-index: 101;
display: flex;
align-items: center;
justify-content: center;
}
.location {
bottom: 180px;
right: 20px;
}
.refresh-car {
bottom: 240px;
right: 20px;
}
.map-type-list {
position: fixed;
top: 50px;
right: 10px;
z-index: 101;
background-color: #fff;
border: 1px solid #eee;
padding: 4px;
a {
display: block;
border-bottom: 1px solid #eee;
}
:last-child {
border-bottom: 0;
}
}
.map-size-list {
position: fixed;
top: 150px;
right: 10px;
z-index: 101;
background-color: #fff;
border: 1px solid #eee;
padding: 4px;
a {
display: block;
border-bottom: 1px solid #eee;
}
:last-child {
border-bottom: 0;
}
}
</style>