Sanakey
2022-10-24 c891bc5ff6b03baf28a9eac7f5a14ade4cea3c23
提交 | 用户 | age
d98d05 1 // pages/map/map.js
S 2 import {Http} from "../../utils/http-p.js";
3
4 const Request = new Http();
5 let app = getApp();
6 let location = {}; //自己的位置
7
8 let windowHeight = 0; //屏幕可用高
9
10 Page({
11
12   /**
13    * 页面的初始数据
14    */
15   data: {
16     showHidelayer: true,
17     longitude: 0,
18     latitude: 0,
19     desHeight: 50,
20     mapHeight: 350,
21     scale: 16,
22     region: ['请点击选择你想要切换的城市'],
23     markers:[],
24     isFirstAuthorise: true,
25     isShowMapItem: true,
26   },
27
28   /**
29    * 生命周期函数--监听页面加载
30    */
31   onLoad: function (options) {
32
33     location = {
34       id: -1,
35       latitude: Number(options.latitude),
36       longitude: Number(options.longitude),
37       name: "",
38       iconPath: "/imgs/locationDef.png"
39     };
40     this.data.markers.push(location);
41
42     windowHeight = wx.getSystemInfoSync().windowHeight
43
44     this.setData({
45       desHeight:  windowHeight - this.data.mapHeight,
46       longitude: location.longitude || 116.397743,
47       latitude: location.latitude || 39.904996,
48       markers: this.data.markers
49     });
50
51
52
c891bc 53     wx.getSetting({
S 54       success: res => {
55         if (res.authSetting['scope.userFuzzyLocation']) {
56           console.log('授权', res);
57           // wx.getFuzzyLocation({
58           //   type: 'wgs84',
59           //   success (res) {
60           //     const latitude = res.latitude
61           //     const longitude = res.longitude
62           //     console.log('模糊定位',latitude,longitude);
63           //   },fail(res){
64           //     console.log('模糊定位报错',res);
65           //   }
66           //  })
67         } else {
68           console.log('未授权');
69     
70           // wx.showModal({
71           //   title: '提示',
72           //   content: '请打开地址授权',
73           //   success: res=>{
74           //     if (res.confirm) {
75           //       wx.openSetting({
76           //         success(res) {
77           //           console.log(res.authSetting)
78           //         }
79           //       })
80           //     }
81           //   }
82           // })
83     
84           wx.authorize({
85             scope: 'scope.userFuzzyLocation',
86             success: (res) => {
87               console.log('定位授权', res);
88               wx.getFuzzyLocation({
89                 type: 'wgs84',
90                 success (res) {
91                   const latitude = res.latitude
92                   const longitude = res.longitude
93                   console.log('模糊定位2',latitude,longitude);
94                   this.setData({
95                     longitude: res.longitude,
96                     latitude: res.latitude
97                   });
98                 }
99                })
100             }
101           });
102           // wx.openSetting({
103           //   success(res) {
104           //     console.log(res,'jjjj9999');
105           //     wx.authorize({
106           //       scope: 'scope.userLocation',
107           //       success(res) {
108           //         console.log('再次授权', res);
109           //         console.log(12347896);
110           //
111           //       }
112           //     });
113           //   }
114           // })
115     
116     
117         }
118     
119       }
120     });
d98d05 121
S 122
123     // wx.authorize({
124     //   scope: 'scope.userLocation',
125     //   success: (res) =>{
126     //     console.log('定位授权', res);
127     //     wx.getLocation({
128     //       type: 'wgs84',
129     //       success: (res) => {
130     //         console.log(res);
131     //         this.setData({
132     //           longitude: res.longitude,
133     //           latitude: res.latitude
134     //         });
135     //       }
136     //     });
137     //   }
138     // });
139
140
141
142     // wx.getSetting({
143     //   success: res => {
144     //     if (res.authSetting['scope.userLocation']) {
145     //       console.log('授权', res);
146     //       wx.getLocation({
147     //         type: 'wgs84',
148     //         success: (res) => {
149     //           console.log(res);
150     //
151     //           location = {
152     //             latitude: res.latitude,
153     //             longitude: res.longitude,
154     //           }
155     //
156     //
157     //           this.setData({
158     //             longitude: res.longitude,
159     //             latitude: res.latitude,
160     //             markers: []
161     //           });
162     //
163     //           this.shopListRequest()
164     //
165     //         }
166     //       });
167     //     } else {
168     //       console.log('未授权');
169     //
170     //       // wx.showModal({
171     //       //   title: '提示',
172     //       //   content: '请打开地址授权',
173     //       //   success: res=>{
174     //       //     if (res.confirm) {
175     //       //       wx.openSetting({
176     //       //         success(res) {
177     //       //           console.log(res.authSetting)
178     //       //         }
179     //       //       })
180     //       //     }
181     //       //   }
182     //       // })
183     //
184     //       wx.authorize({
185     //         scope: 'scope.userLocation',
186     //         success: (res) => {
187     //           console.log('定位授权', res);
188     //           wx.getLocation({
189     //             type: 'wgs84',
190     //             success: (res) => {
191     //               console.log(res);
192     //
193     //               // location = {
194     //               //   id: -1,
195     //               //   latitude: res.longitude,
196     //               //   longitude: res.latitude,
197     //               //   name: "",
198     //               //   iconPath: "/imgs/locationDef.png"
199     //               // }
200     //
201     //
202     //               this.setData({
203     //                 longitude: res.longitude,
204     //                 latitude: res.latitude,
205     //                 // markers: this.data.markers
206     //               });
207     //
208     //               this.shopListRequest()
209     //             }
210     //           });
211     //         }
212     //       });
213     //       // wx.openSetting({
214     //       //   success(res) {
215     //       //     console.log(res,'jjjj9999');
216     //       //     wx.authorize({
217     //       //       scope: 'scope.userLocation',
218     //       //       success(res) {
219     //       //         console.log('再次授权', res);
220     //       //         console.log(12347896);
221     //       //
222     //       //       }
223     //       //     });
224     //       //   }
225     //       // })
226     //
227     //
228     //     }
229     //
230     //   }
231     // });
232
233
234   },
c43500 235   choosePoi(){
S 236     console.log('00sss');
237     // wx.choosePoi({
238     //   success: (res) => {
239     //     console.log('测试choosePoi',res);
240     //   }
241     // })
242     wx.choosePoi({
243       success(res) {
244         console.log(res, '成功回调')
245       },
246       fail(res) {
247         console.log(res, '失败回调')
248       },
249       complete(res) {
250         console.log(res, '结束回调')
251       }
252     })
253   },
d98d05 254
S 255   shopListRequest(){
256     Request.request({
257       url: '/shopping/getDistributorAddressListByCity.do',
258       data: {
259         longitude: this.data.longitude,
260         latitude: this.data.latitude
261       }
262     }).then(res => {
263       console.log(res);
264       this.setData({
265         storeData: res.list,
266         region: [res.province,res.city,res.district]
267       });
268     });
269   },
270
271   //切换城市后
272   bindRegionChange: function (e) {
273     console.log('picker发送选择改变,携带值为', e.detail.value);
274     this.setData({
275       region: e.detail.value
276     });
277     Request.request({
278       url: '/shopping/getDistributorAddressListByCity.do',
279       data: {
280         province:e.detail.value[0],
281         city: e.detail.value[1],
282         district: e.detail.value[2]
283       }
284     }).then(res => {
285       console.log(res);
286
287       if (res.list.length > 0) {
288
289         this.data.markers = JSON.parse(JSON.stringify(res.list))
290         console.log(this.data.markers);
291         this.data.markers.push(location);
292         this.setData({
293           storeData: res.list,
294           markers: this.data.markers,
295           // longitude: res.list[0].longitude,
296           // latitude: res.list[0].latitude
297         });
298
299       } else{
300         this.setData({
301           storeData: res.list,
302           markers: [],
303           mapHeight: 350,
304           desHeight: windowHeight - 350,
305           isShowMapItem: true,
306         });
307       }
308     });
309
310
311   },
312
313
314   // 门店点击切换到定位
315   locationTap(e) {
316     console.log(e);
317     this.setData({
318       longitude: e.detail.longitude,
319       latitude: e.detail.latitude,
320       scale: 16
321     });
322   },
323
324   // 回到最初定位点
325   moveToLocation: function () {
326     this.setData({
327       longitude: location.longitude,
328       latitude: location.latitude,
329       scale: 16
330     });
331     // this.mapCtx.moveToLocation()
332   },
333
334   //
335   toggleList(){
336
337     let isShowMapItem = this.data.isShowMapItem;
338     if (isShowMapItem){
339       this.setData({
340         desHeight: 50,
341         isShowMapItem: false,
342         mapHeight: windowHeight - 50,
343       })
344
345     } else{
346       this.setData({
347         mapHeight: 350,
348         desHeight: windowHeight - 350,
349         isShowMapItem: true,
350       })
351
352     }
353
354
355   },
356
357
358   //点击marker标记前往微信地图
359   goToHere(e) {
360     console.log(e.markerId);
361     if(e.markerId>-1){
362       wx.openLocation({ // 打开微信内置地图,实现导航功能(在内置地图里面打开地图软件)
363         latitude: Number(this.data.markers[e.markerId].latitude),
364         longitude: Number(this.data.markers[e.markerId].longitude),
365         name: (this.data.markers[e.markerId].name || '') + (this.data.markers[e.markerId].tel || ''),
366         success: function (res) {
367           console.log(res);
368         },
369         fail: function (res) {
370           console.log(res);
371         }
372       });
373     }
374
375   },
376
377   //点击门店地址前往微信地图
378   goHere(e){
379     console.log(e);
380     wx.openLocation({ // 打开微信内置地图,实现导航功能(在内置地图里面打开地图软件)
381       latitude: Number(e.detail.latitude),
382       longitude: Number(e.detail.longitude),
383       name: (e.detail.name || '') +' '+e.detail.tel,
384       success: function (res) {
385         console.log(res);
386       },
387       fail: function (res) {
388         console.log(res);
389       }
390     });
391   },
392
393   /**
394    * 生命周期函数--监听页面初次渲染完成
395    */
396   onReady: function () {
397     this.mapCtx = wx.createMapContext('myMap');
398   },
399
400   /**
401    * 生命周期函数--监听页面显示
402    */
403   onShow: function () {
c43500 404   
S 405     // wx.getLocation({
406     //   type: 'wgs84',
407     //   success: (res) => {
d98d05 408
c43500 409     //     location = {
S 410     //       latitude: res.latitude,
411     //       longitude: res.longitude,
412     //     }
d98d05 413
c43500 414     //     this.setData({
S 415     //       longitude: res.longitude,
416     //       latitude: res.latitude,
417     //       markers: []
418     //     });
d98d05 419
c43500 420     //     this.shopListRequest()
d98d05 421
c43500 422     //   },
S 423     //   fail: (res) => {
424     //     console.log(res);
425     //     wx.showModal({
426     //       title: '提示',
427     //       content: '为了更好的体验该功能,请打开您的地址授权',
428     //       success: res=>{
429     //         if (res.confirm) {
430     //           wx.openSetting({
431     //             success(res) {
432     //             }
433     //           })
434     //         }
435     //       }
436     //     })
d98d05 437
c43500 438     //   }
S 439     // });
d98d05 440
S 441     // wx.getSetting({
442     //   success: res => {
443     //     if (res.authSetting['scope.userLocation']) {
444     //       console.log('授权', res);
445     //       wx.getLocation({
446     //         type: 'wgs84',
447     //         success: (res) => {
448     //           console.log(res);
449     //           location = {
450     //             latitude: res.latitude,
451     //             longitude: res.longitude,
452     //           }
453     //
454     //           this.setData({
455     //             longitude: res.longitude,
456     //             latitude: res.latitude,
457     //             markers: []
458     //           });
459     //
460     //           this.shopListRequest()
461     //
462     //         },
463     //         fail:(res)=> {
464     //           console.log(res);
465     //         }
466     //       });
467     //
468     //     } else {
469     //       console.log('未授权');
470     //       wx.getLocation({
471     //         type: 'wgs84',
472     //         success: (res) => {
473     //
474     //           location = {
475     //             latitude: res.latitude,
476     //             longitude: res.longitude,
477     //           }
478     //
479     //           this.setData({
480     //             longitude: res.longitude,
481     //             latitude: res.latitude,
482     //             markers: []
483     //           });
484     //
485     //           this.shopListRequest()
486     //
487     //         },
488     //         fail: (res) => {
489     //           console.log(res);
490     //           wx.showModal({
491     //             title: '提示',
492     //             content: '为了更好的体验该功能,请打开地址授权',
493     //             success: res=>{
494     //               if (res.confirm) {
495     //                 wx.openSetting({
496     //                   success(res) {
497     //                   }
498     //                 })
499     //               }
500     //             }
501     //           })
502     //
503     //         }
504     //       });
505     //
506     //     }
507     //
508     //   }
509     // });
510
511   },
512
513   /**
514    * 生命周期函数--监听页面隐藏
515    */
516   onHide: function () {
517
518   },
519
520   /**
521    * 生命周期函数--监听页面卸载
522    */
523   onUnload: function () {
524
525   },
526
527   /**
528    * 页面相关事件处理函数--监听用户下拉动作
529    */
530   onPullDownRefresh: function () {
531
532   },
533
534   /**
535    * 页面上拉触底事件的处理函数
536    */
537   onReachBottom: function () {
538
539   },
540
541   /**
542    * 用户点击右上角分享
543    */
544   // onShareAppMessage: function () {
545   //
546   // }
547 });