MrShi
2023-12-25 21544c5c6d1dc913ba44edec8c001a2272233ec6
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<template>
    <view class="ditu">
        <map
            id="mapId"
            class="ditu_map"
            :markers="markers"
            :show-location="true"
            :latitude="latitude"
            :longitude="longitude"
            @markertap="markertap"
            @callouttap="toNavigation">
            <cover-view slot="callout">
                <cover-view v-for="(item,index) in markers" :key="index">
                    <cover-view class="customCallout" :marker-id="item.id">
                        <cover-image class="image" src="@/static/icon/ic_dingwei@2x.png" mode="widthFix"></cover-image>
                        <cover-view class="mm">{{rice > 1000 ? (rice / 1000).toFixed(1) + '千米' : rice + '米'}}</cover-view>
                        <cover-view class="h"></cover-view>
                        <cover-view class="dh">导航</cover-view>
                    </cover-view>
                </cover-view>
            </cover-view>
        </map>
        <u-popup :show="show" :overlay="false" :round="10" mode="bottom" @close="show = false">
            <view class="ditu_box">
                <view class="ditu_box_a">
                    <text>{{info.title}}</text>
                    <text>距离{{rice > 1000 ? (rice / 1000).toFixed(1) + '千米' : rice + '米'}}</text>
                </view>
                <view class="ditu_box_b">
                    {{info.addr}}
                </view>
                <view class="ditu_box_c" @click="toNavigation">导航前往</view>
            </view>
        </u-popup>
        <image class="dw" @click="clickcontrol" src="@/static/icon/map_relocation@2x.png" mode="widthFix"></image>
    </view>
</template>
 
<script>
    import { distance } from '@/utils/utils.js'
    export default {
        data() {
            return {
                show: false,
                latitude: '',
                longitude: '',
                markers: [],
                info: {},
                rice: ''
            };
        },
        onLoad() {
            this.getAddress()
        },
        methods: {
            // 获取经纬度
            getAddress() {
                let that = this;
                // 1、判断手机定位服务【GPS】 是否授权
                uni.getSystemInfo({
                    success(res) {
                        let locationEnabled = res.locationEnabled; // 判断手机定位服务是否开启
                        let locationAuthorized = res.locationAuthorized; // 判断定位服务是否允许微信授权
                        if (locationEnabled == false || locationAuthorized == false) {
                            //手机定位服务(GPS)未授权
                            uni.showToast({ title: "请打开手机GPS", icon: "none" });
                        } else {
                            // 2、判断微信小程序是否授权位置信息
                            uni.authorize({
                                //授权请求窗口
                                scope: "scope.userLocation",
                                success: (res) => {
                                    that.fnGetlocation();
                                },
                                fail: (err) => {
                                    err = err["errMsg"];
                                    uni.showModal({ content: "需要授权位置信息", confirmText: "确认授权" })
                                        .then((res) => {
                                            if (res.confirm) {
                                                uni.openSetting({
                                                    success: (res) => {
                                                        if (res.authSetting["scope.userLocation"]) {
                                                            uni.showToast({ title: "授权成功", icon: "success" });
                                                            that.fnGetlocation();
                                                        } else {
                                                            that.getSite()
                                                            // 未授权
                                                            uni.showToast({ title: "授权失败,请重新授权", icon: "none" });
                                                            uni.showModal({
                                                                title: "授权",
                                                                content: "获取授权失败,是否前往授权设置?",
                                                                success: function(result) {
                                                                    if (result.confirm) {
                                                                        uni.openSetting();
                                                                    }
                                                                },
                                                                fail: function() {
                                                                    uni.showToast({ title: "系统错误!", icon: "none" });
                                                                }
                                                            });
                                                        }
                                                    }
                                                });
                                            }
                                            if (res.cancel) {
                                                // 取消授权
                                                uni.showToast({ title: "你拒绝了授权,无法获得周边信息", icon: "none" });
                                                that.getSite()
                                            }
                                        });
                                }
                            });
                        }
                    },
                });
            },
            // 定位获取
            fnGetlocation() {
                let that = this;
                uni.getLocation({
                    type: "gcj02",
                    altitude: true,
                    isHighAccuracy: true,
                    success: function(res) {
                        that.latitude = res.latitude
                        that.longitude = res.longitude
                        that.getSite()
                    },
                    fail(err) {
                        if (err.errMsg === "getLocation:fail 频繁调用会增加电量损耗,可考虑使用 wx.onLocationChange 监听地理位置变化") {
                            uni.showToast({ title: "请勿频繁定位", icon: "none" });
                        }
                        if (err.errMsg === "getLocation:fail auth deny") {
                            // 未授权
                            uni.showToast({ title: "无法定位,请重新获取位置信息", icon: "none" });
                        }
                        if (err.errMsg === "getLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF") {
                            uni.showModal({ content: "请开启手机定位服务", showCancel: false });
                        }
                    },
                });
            },
            //导航--传终点的坐标即可
            toNavigation: function() {
                uni.openLocation({
                    longitude: parseFloat(this.info.longitude),
                    latitude: parseFloat(this.info.latitude),
                    scale: 28,
                    name: this.info.title,
                    address: this.info.addr,
                    success: function (res) {
                        console.log('success:',res);
                    }
                });
            },
            //右下角定位按钮的点击事件
            clickcontrol(){
                let mapObjs = uni.createMapContext('mapId', this)
                mapObjs.moveToLocation({
                    complete: res => {
                        console.log('移动完成:', res)
                    }
                })
            },
            getSite() {
                this.$u.api.rentSiteList()
                    .then(res => {
                        if (res.code === 200) {
                            this.markers = []
                            res.data.forEach((item, index) => {
                                let num = distance(this.latitude, this.longitude, item.latitude, item.longitude)
                                this.markers.push({
                                    id: index,
                                    width: 40,
                                    height: 40,
                                    distance: this.latitude && this.longitude ? num : '未知距离',
                                    latitude: item.latitude,
                                    longitude: item.longitude,
                                    title: item.name,
                                    iconPath: '/static/icon/ic_bike@2x.png',
                                    addr: item.addr,
                                    customCallout: {
                                        anchorY: 0,
                                        anchorX: 0,
                                        display: 'BYCLICK'
                                    }
                                })
                            })
                        }
                    })
            },
            markertap(e) {
                this.show = false
                this.markers.forEach(item => {
                    if (item.id === e.markerId) {
                        this.rice = item.distance
                        item.customCallout.display = 'ALWAYS'
                        item.width = 50
                        item.height = 50
                        setTimeout(() => {
                            this.info = item
                            this.show = true
                        }, 300)
                    } else {
                        item.width = 40
                        item.height = 40
                        item.customCallout.display = 'BYCLICK'
                    }
                })
            },
            navigation(item) {
                uni.openLocation({
                    latitude: Number(item.latitude),
                    longitude: Number(item.longitude),
                    name: '测试名称',
                    address: '测试详细地址',
                    success: function() {
                        console.log('打开地图导航成功');
                    },
                    fail: function(err) {
                        console.log('打开地图导航失败', err);
                    }
                });
            }
        }
    }
</script>
 
<style lang="scss" scoped>
    .ditu {
        width: 100vw;
        height: 100vh;
        .ditu_map {
            width: 100%;
            height: 100%;
            .customCallout {
                display: flex;
                align-items: center;
                justify-content: center;
                padding: 16rpx 20rpx;
                box-sizing: border-box;
                height: 80rpx;
                background: #FFFFFF;
                border-radius: 50rpx;
                box-shadow: 0rpx 0rpx 12rpx 0rpx rgba(0,0,0,0.1);
                .image {
                    width: 28rpx;
                    height: 28rpx;
                    background-repeat: no-repeat;
                    background-size: 100% 100%;
                }
                .mm {
                    font-size: 28rpx;
                    font-family: PingFangSC-Regular, PingFang SC;
                    font-weight: 400;
                    color: #222222;
                }
                .h {
                    width: 2rpx;
                    height: 28rpx;
                    margin: 0 16rpx;
                    background-color: #E5E5E5;
                }
                .dh {
                    font-size: 28rpx;
                    font-family: PingFangSC-Regular, PingFang SC;
                    font-weight: 400;
                    color: #01B6AD;
                }
            }
        }
        .ditu_box {
            width: 100%;
            padding: 30rpx 20rpx;
            box-sizing: border-box;
            background: linear-gradient(360deg, #FFFFFF 0%, #FFFFFF 58%, #D0FFFD 100%);
            box-shadow: 0rpx -6rpx 16rpx 0rpx rgba(0,0,0,0.1);
            border-radius: 32rpx 32rpx 0rpx 0rpx;
            .ditu_box_a {
                width: 100%;
                padding: 0 20rpx;
                box-sizing: border-box;
                display: flex;
                align-items: center;
                justify-content: space-between;
                text {
                    &:first-child {
                        font-size: 34rpx;
                        font-family: PingFangSC-Medium, PingFang SC;
                        font-weight: 500;
                        color: #222222;
                    }
                    &:last-child {
                        font-size: 26rpx;
                        font-family: PingFangSC-Regular, PingFang SC;
                        font-weight: 400;
                        color: #01B6AD;
                    }
                }
            }
            .ditu_box_b {
                width: 100%;
                padding: 0 20rpx;
                box-sizing: border-box;
                font-size: 26rpx;
                font-family: PingFangSC-Regular, PingFang SC;
                font-weight: 400;
                color: #666666;
                margin-top: 6rpx;
            }
            .ditu_box_c {
                width: 100%;
                height: 96rpx;
                line-height: 96rpx;
                text-align: center;
                background: #01B6AD;
                box-shadow: 0rpx 6rpx 16rpx 0rpx rgba(1,182,173,0.24);
                border-radius: 46rpx;
                font-size: 32rpx;
                font-family: PingFangSC-Medium, PingFang SC;
                font-weight: 500;
                color: #FFFFFF;
                margin-top: 40rpx;
            }
        }
        .dw {
            width: 80rpx;
            height: 80rpx;
            position: absolute;
            right: 18rpx;
            bottom: calc(env(safe-area-inset-bottom) + 300rpx);
        }
    }
</style>