yangan
2024-11-04 1db172e1eb09be68008659c9f05bf18144df553a
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
<template>
    <view>
        <view class="page-body"
            style="margin-top: 4rpx;">
            <view class="page-section page-section-gap">
                <map style="width: 100%; height: 600px;"
                    :latitude="latitude"
                    :longitude="longitude"
                    :markers="markers"
                    :polyline="polyline"
                    @markertap="awaken"
                    @callouttap="awaken"
                    @tap="awaken"></map>
            </view>
        </view>
        <view class="tip">
            <view class="first">
                通行距离
                <text>{{ distance || '' }}Km</text>
            </view>
            <view class="first">
                预计到达时间
                <text>{{ duration || '' }}</text>
            </view>
        </view>
        <view class="open-button"
            @click="awaken"><u-button type="primary">使用手机地图</u-button></view>
    </view>
</template>
 
<script>
    export default {
        data() {
            return {
                latitude: 38.04606544104117,
                longitude: 114.48735479816793,
                customerId: '',
                filedId: '',
                orderType: '',
                toFiledId: '',
                // 起点经纬度
                originlongitude: 39.909,
                originlatitude: 116.39742,
                // 终点
                finishlongitude: null,
                finishlatitude: null,
                markers: [{
                        latitude: null,
                        longitude: null,
                        iconPath: 'https://wrzs.czjlchem.com:9090/appimg/image/banner/location.png',
                        width: 23,
                        height: 33,
                        id: 0,
                        callout: {
                            content: '起点', //文本
                            color: '#ffffff', //文字颜色
                            fontSize: 10, //文本大小
                            borderRadius: 12, //边框圆角
                            borderWidth: '10',
                            bgColor: '#08b078', //背景颜色
                            display: 'ALWAYS' //常显
                        }
                    },
                    {
                        latitude: null,
                        longitude: null,
                        iconPath: 'https://wrzs.czjlchem.com:9090/appimg/image/banner/location.png',
                        width: 23,
                        height: 33,
                        id: 1,
                        callout: {
                            content: '终点', //文本
                            color: '#ffffff', //文字颜色
                            fontSize: 10, //文本大小
                            borderRadius: 12, //边框圆角
                            borderWidth: '10',
                            bgColor: '#fc645c', //背景颜色
                            display: 'ALWAYS' //常显
                        }
                    }
                ],
                polyline: [{
                    // 每个点的经纬度
                    points: [{ latitude: 38.04606544104117, longitude: 114.48735479816793 }, {
                        latitude: 38.049531,
                        longitude: 114.53521899999998
                    }],
                    // 颜色
                    color: '#0091ff',
                    // 宽度
                    width: 10
                }],
                distance: null,
                duration: null,
                fromLocation: {},
                toLocation: {}
            };
        },
        methods: {
            getLocation() {
                uni.showLoading({
                    title: '加载中’'
                });
                let QQMapWX = require('../../../utils/qqmap-wx-jssdk.js');
                let qqmapsdk = new QQMapWX({
                    key: 'HTNBZ-LZGRQ-YDB5S-B4KAS-KLHJH-GHBUE' // 必填
                });
                const params = this.toFiledId ? { filedId: this.filedId, toFiledId: this.toFiledId } : {
                    customerId: this
                        .customerId,
                    filedId: this.filedId
                };
                this.$reqGet('getAppLocation', params).then(result => {
                    console.log(result, '腾讯经纬度');
                    let _this = this;
                    console.log(_this.orderType, '类型');
                    let fromLocation;
                    let toLocation;
                    if (_this.orderType == '外购' || _this.orderType == '外销') {
                        fromLocation = {
                            longitude: result.data.customerLongitude,
                            latitude: result.data.customerLatitude
                        };
                        toLocation = {
                            longitude: result.data.filedLongitude,
                            latitude: result.data.filedLatitude
                        };
                    }
                    // else if (_this.orderType == '内购' || _this.orderType == '内销') {
                    //     fromLocation = {
                    //         longitude: result.data.filedLongitude,
                    //         latitude: result.data.filedLatitude
                    //     };
                    //     toLocation = {
                    //         longitude: result.data.customerLongitude,
                    //         latitude: result.data.customerLatitude
                    //     };
                    // } 
                    else {
                        fromLocation = {
                            longitude: result.data.filedLongitude,
                            latitude: result.data.filedLatitude
                        };
                        toLocation = {
                            longitude: result.data.toFiledLongitude,
                            latitude: result.data.toFiledLatitude
                        };
                        // _this.fromLocation = fromLocation;
                        // _this.toLocation = toLocation;
                    }
                    console.log(fromLocation, toLocation);
                    _this.fromLocation = fromLocation;
                    _this.toLocation = toLocation;
                    //调用距离计算接口
                    qqmapsdk.direction({
                        mode: 'driving',
                        //from参数不填默认当前地址
                        from: fromLocation,
                        to: toLocation,
                        success: function(res) {
                            console.log(res);
                            let ret = res;
                            let coors = ret.result.routes[0].polyline,
                                pl = [];
                            //坐标解压(返回的点串坐标,通过前向差分进行压缩)
                            let kr = 1000000;
                            for (let i = 2; i < coors.length; i++) {
                                coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
                            }
                            //将解压后的坐标放入点串数组pl中
                            for (let i = 0; i < coors.length; i += 2) {
                                pl.push({ latitude: coors[i], longitude: coors[i + 1] });
                            }
                            console.log(pl);
                            //设置polyline属性,将路线显示出来,将解压坐标第一个数据作为起点
                            _this.longitude = pl[0].longitude;
                            _this.latitude = pl[0].latitude;
                            _this.polyline = [{
                                points: pl,
                                color: '#04b474',
                                width: 4,
                                arrowLine: true
                            }];
                            if (_this.orderType == '外购' || _this.orderType == '外销') {
                                _this.$set(_this.markers[0], 'longitude', result.data
                                    .customerLongitude);
                                _this.$set(_this.markers[0], 'latitude', result.data.customerLatitude);
                                _this.$set(_this.markers[1], 'longitude', result.data.filedLongitude);
                                _this.$set(_this.markers[1], 'latitude', result.data.filedLatitude);
                            }
                            // else if (_this.orderType == '内购' || _this.orderType == '内销') {
                            //     _this.$set(_this.markers[0], 'longitude', result.data.filedLongitude);
                            //     _this.$set(_this.markers[0], 'latitude', result.data.filedLatitude);
                            //     _this.$set(_this.markers[1], 'longitude', result.data
                            //         .customerLongitude);
                            //     _this.$set(_this.markers[1], 'latitude', result.data.customerLatitude);
                            // } 
                            else {
                                _this.$set(_this.markers[0], 'longitude', result.data.filedLongitude);
                                _this.$set(_this.markers[0], 'latitude', result.data.filedLatitude);
                                _this.$set(_this.markers[1], 'longitude', result.data
                                    .toFiledLongitude);
                                _this.$set(_this.markers[1], 'latitude', result.data.toFiledLatitude);
                            }
                        },
                        fail: function(error) {
                            console.error(error);
                        },
                        complete: function(res) {
                            console.log(res);
                            uni.hideLoading();
                            _this.distance = res.result.routes[0].distance;
                            _this.distance = (_this.distance / 1000).toFixed(2);
                            _this.duration = _this.handleTime(res.result.routes[0].duration);
                        }
                    });
                });
            },
            //
            handleTime(time) {
                let now = new Date(); // 获取当前时间
                now.setMinutes(now.getMinutes() + time);
                let hours = now
                    .getHours()
                    .toString()
                    .padStart(2, '0');
                let minutes = now
                    .getMinutes()
                    .toString()
                    .padStart(2, '0');
                return hours + ':' + minutes;
            },
            // 唤醒
            awaken() {
                uni.openLocation({
                    latitude: this.markers[1].latitude,
                    longitude: this.markers[1].longitude,
                    name: '目的地',
                    address: '目的地'
                });
            }
        },
        onLoad(params) {
            this.customerId = params.customerId;
            this.filedId = params.filedId;
            this.orderType = params.orderType;
            if (params.toFiledId) {
                this.toFiledId = params.toFiledId;
            }
        },
        onShow() {
            this.getLocation();
            // this.awaken();
        }
    };
</script>
 
<style lang="scss"
    scoped>
    .tip {
        width: 94%;
        font-size: 40rpx;
        margin: vww(22) auto;
        display: flex;
        justify-content: space-around;
 
        .first {
            color: #515151;
 
            text {
                font-size: 45rpx;
                color: #f81414;
            }
        }
    }
 
    .open-button {
        width: 100%;
        margin: 40rpx 0;
        display: flex;
        justify-content: center;
 
        /deep/.u-button {
            width: 300rpx;
            border: 1px solid #3b56eb;
            border-radius: 37rpx 37rpx 37rpx 37rpx;
            background-color: transparent;
            color: #3b56eb;
        }
    }
</style>