index.js
7.54 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
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
const app = getApp()
const restapi = require('../restapi.js');
const mapapi = require('../../../../utils/mapapi.js');
Page({
/**
* 页面的初始数据
*/
data: {
test:true,
mapHeight: 446,
markers: [],
latitude:0, //维度
longitude:0, //经度
userId:'',
address: '',
title: '',
distance: '',
time: '',
selLat: 0,
selLng: 0,
flag: false,
curTypeId: null,
hasPosition:false,
},
// 动态改变地图高度
changeHeight() {
let phoneHeight = wx.getSystemInfoSync().windowHeight
let query = wx.createSelectorQuery().in(this)
wx.canIUse('nextTick')
?
wx.nextTick(()=>{
query.select('#console').boundingClientRect(res => {
this.setData({
mapHeight: (phoneHeight - res.height)
})
}).exec()
})
:
query.select('#console').boundingClientRect(res => {
this.setData({
mapHeight: (phoneHeight - res.height)
})
}).exec()
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
console.log("options",options)
let that = this;
let userIcon = null;
if (!app.globalData.userInfo) {
app.userInfoReadyCallback = userInfo => {
if (userInfo != null) {
let info = app.globalData.userInfo
console.log(info);
userIcon = info.avatarUrl;
if(!info.avatarUrl){
userIcon = info.logo;
}
that.setData({
userIcon: userIcon
})
}
}
}else{
let info = app.globalData.userInfo
console.log(info)
userIcon = info.avatarUrl;
if (!info.avatarUrl) {
userIcon = info.logo;
}
this.setData({
userIcon: userIcon
})
}
console.log(options);
this.setData({
userId: options.protocolId,
curTypeId: app.globalData.selectedDevice.typeId
});
if(options.source && options.source == 'luck'){
this.setData({
luck: true
})
}
this.setData({
test: true,
flag: false,
hasPosition:false
});
this.changeHeight();
this.getSelfLocation();
},
getSelfLocation: function(){
let userId = this.data.userId;
console.log("getSelfLocation userId",userId)
let that = this;
wx.getLocation({
type: 'gcj02',
success: function (res) {
let flat = res.latitude;
let flng = res.longitude;
that.setData({
selLat: flat,
selLng: flng,
latitude: flat,
longitude: flng
});
that.getLoc();
},
fail: function () {
wx.authorize({
scope: 'scope.userLocation',
success() {
that.getSelfLocation();
}
})
}
})
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
getLoc: function(){
let userId = this.data.userId;
console.log(userId)
let that = this;
if(userId){
restapi.getVehiclePosition(userId, function (response) {
console.log("getVehiclePosition", response);
if (null == response.data.didiBOxlng || null == response.data.didiBOxlat || response.data.didiBOxlng == '' || response.data.didiBOxlat == '') {
let flat = that.data.selLat;
let flng = that.data.selLng;
let markers = [{
id: 0,
longitude: flng,
latitude: flat,
iconPath: '/packageSmartPlus/resources/images/location.png',
width: '76rpx',
height: '76rpx',
}];
that.setData({
latitude: flat,
longitude: flng,
test: false,
flag: true,
markers: markers,
})
that.changeHeight();
} else {
that.setData({
hasPosition:true
})
that.showCarPostion(response.data.didiBOxlng, response.data.didiBOxlat, response.data.showTime);
}
},function () {
app.showMessage("获取车辆位置信息失败!");
});
}else{
let def = {
didiBOxTime: new Date(),
didiBOxlat: "22.587337",
didiBOxlng: "113.991485",
showTime: "0",
weapp: true
}
that.setData({
hasPosition:true
})
that.showCarPostion(def.didiBOxlng, def.didiBOxlat, def.showTime);
}
setTimeout(() => {
console.log("setTimeout,5秒后", that.data.hasPosition)
if (!that.data.hasPosition) {
that.setData({
flag: true
})
} else {
that.setData({
flag: false
})
}
}, 5000)
},
/**
* 展示车辆位置
*/
showCarPostion: function (lng, lat, time) {
let that = this;
let markers = [{
id: 0,
longitude: lng,
latitude: lat,
iconPath: '/packageSmartPlus/resources/images/location.png',
width: '76rpx',
height: '76rpx',
}]
mapapi.getDetailedAdrress(lng, lat, function (data) {
let str = "";
let address = data.address;
if (data.address_reference && data.address_reference.landmark_l2) {
str = data.address_reference.landmark_l2.title;
} else if (data.address_reference && data.address_reference.landmark_l1) {
str = data.address_reference.landmark_l1.title;
} else if (data.formatted_addresses && data.formatted_addresses.recommend) {
str = data.formatted_addresses.recommend;
} else if (data.formatted_addresses && data.formatted_addresses.rough) {
str = data.formatted_addresses.rough;
}
that.setData({
address: address,
title: str,
time: time,
latitude: lat,
longitude: lng,
markers: markers,
test: false
});
let flat = that.data.selLat;
let flng = that.data.selLng;
mapapi.calculateDistan(flng, flat, lng, lat, function (distance) {
if (distance) {
//计算距离
if (distance <= 1000) {
distance = distance + '米';
} else {
distance = (distance / 1000).toFixed(2) + '公里';
}
that.setData({
distance: distance
})
}
});
setTimeout(_ =>{
that.changeHeight();
},1000)
});
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
let userId = this.data.userId;
let lng = this.data.longitude;
let lat = this.data.latitude;
let stTime = new Date().getTime();
this.setData({
latitude: + lat - 0.0025,
longitude: lng,
})
console.log(userId);
console.log(lng);
console.log(lat);
console.log(stTime);
setTimeout(()=>{
this.setData({
latitude: lat,
longitude: lng,
})
},2000)
console.log(`carlng=${lng}&carlat=${lat}&stTime=${stTime}&title=${this.data.title}&userId=${userId}`)
let path = `packageSmartPlus/pages/vehicleTracking/share/share?carlng=${lng}&carlat=${lat}&stTime=${stTime}&title=智能车联网车辆位置分享&userId=${userId}`;
return {
title: '车辆位置分享',
path: path
}
},
/**
* 导航
*/
guideToCar: function(){
let lat = this.data.latitude;
let lng = this.data.longitude;
let title = this.data.title;
wx.openLocation({
latitude: parseFloat(lat),
longitude: parseFloat(lng),
name:title,
})
}
})