mapContent.vue
6.18 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
<template>
<div class="map-box">
<slot name="mapBox"></slot>
<div v-show="isUploaded">
<button
class="map-cmd-icon location"
@click="getLocation"
v-preventReClick
>
<span v-if="!isGetLocation" class="icon icon-location"></span>
<van-loading v-else color="#1989fa" />
</button>
<button
class="map-cmd-icon refresh-car"
@click="refresh"
v-preventReClick
>
<span class="icon icon-refresh-car"></span>
</button>
</div>
</div>
</template>
<script>
import { onMounted, ref } from "vue";
import { Toast } from "vant";
import "vant/es/toast/style";
import GLMap from "@/utils/glsk-mapapi-sdk.js";
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);
const isGetLocation = ref(false);
onMounted(() => {});
const initMap = (id, version = 3) => {
return new Promise((resolve) => {
let check = () => {
checkLoadedTimeoutId && window.clearTimeout(checkLoadedTimeoutId);
if(GLMap) {
new GLMap({ version }, mapapi => {
// new GLMap(mapapi => {
window.mapapi = mapapi;
init(id);
resolve(GLMap)
})
} else {
checkLoadedTimeoutId = check();
}
};
checkLoadedTimeoutId = check();
});
};
const init = (id) => {
map = window.mapapi.invoke('Map', id);
var point = mapapi.invoke('Point', 116.2317, 39.5427)
window.mapapi.invoke('LocalCity', (data) => {
if(data) {
point = data.center
}
mapapi.invoke('centerAndZoom', point, 10);
isUploaded.value = true;
context.emit("isUploaded", true);
context.emit("getData", map);
return map;
})
};
let currMarker = null;
// 获取定位
const getLocation = () => {
isGetLocation.value = true;
window.mapapi.invoke('Geolocation', (data) => {
if(data) {
if (currMarker) {
// currMarker.setMap(null);
window.mapapi.invoke('removeOverlay', currMarker)
}
isGetLocation.value = false;
currMarker = window.mapapi.invoke('Marker', data.point, {
icon: window.mapapi.invoke('Icon', require("@/assets/images/curr-marker.gif"), window.mapapi.invoke('Size', 24, 24)),
// offset: window.mapapi.invoke('Pixel', -12, -12),
})
window.mapapi.invoke('addOverlay', currMarker)
window.mapapi.invoke('panTo', data.point)
// 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();
} else {
Toast("获取定位失败");
isGetLocation.value = false;
}
}, {
enableHighAccuracy: true,
timeout: 10000,
})
// 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);
// }
// isGetLocation.value = false;
// 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", () => {
// Toast("获取定位失败");
// isGetLocation.value = false;
// });
// });
};
const getLngLat = (lng, lat) => {
return window.mapapi.invoke('Point', lng, lat)
};
const mapMarker = ({point, Animation = null, isAdd = true, icon = require("@/assets/images/curr-marker.gif"), sizeW = 52, sizeH = 52, imgW = 52, imgH = 52 }) => {
var currMarker = window.mapapi.invoke('Marker', point, {
icon: window.mapapi.invoke('Icon', icon, window.mapapi.invoke('Size', sizeW, sizeH), {
imageSize: window.mapapi.invoke('Size', imgW, imgH)
}),
})
// console.log(Animation, 'Animation---')
// Animation && window.mapapi.invoke('markerSetAnimation', Animation)
if(isAdd) {
window.mapapi.invoke('addOverlay', currMarker)
window.mapapi.invoke('panTo', point)
}
return currMarker
};
// 刷新
const refresh = () => {
context.emit("refreshCb", map);
};
return {
initMap,
init,
checkLoadedTimeoutId,
getLocation,
satelActive,
roadActive,
getLngLat,
mapMarker,
refresh,
isUploaded,
isGetLocation,
};
},
};
</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;
box-shadow: 0px 5px 12px 0px rgba(0, 0, 0, 0.12);
}
.location {
bottom: 160px;
right: 20px;
}
.refresh-car {
bottom: 220px;
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>