diff --git a/package2/pages/appointment/appointment.js b/package2/pages/appointment/appointment.js index c42fcb3..e4e454a 100644 --- a/package2/pages/appointment/appointment.js +++ b/package2/pages/appointment/appointment.js @@ -21,7 +21,9 @@ Page({ merchantId: "", currentIndex: "", status: "", - isShowBtns: false + isShowBtns: false, + latitude: "", + longitude: "" }, onLoad(options) { console.log(options, 'options') @@ -36,12 +38,33 @@ Page({ console.log(finalStartTime, 'finalStartTime'); + // const mallList = [{ + // merchantName: "1家帮汇家政保洁(河池市庆远镇)", + // latitude: 50.277748, + // longitude: 119.996078, + // id: 1 + // }, { + // merchantName: "2家帮汇家政保洁(河池市庆远镇)", + // latitude: 35.277748, + // longitude: 119.996078, + // id: 2 + // }, { + // merchantName: "3家帮汇家政保洁(河池市庆远镇)", + // id: 3 + // }, { + // merchantName: "4家帮汇家政保洁(河池市庆远镇)", + // latitude: 45.277748, + // longitude: 119.996078, + // id: 4 + // }] + this.setData({ id: options.id || '', orderId: options.orderId || '', startTime: finalStartTime, endTime: options.endTime || '', mallList: JSON.parse(options.mallList)[0].merchantVoList || '', + // mallList }) console.log(this.data.mallList, 'mallList'); @@ -72,6 +95,64 @@ Page({ console.log(this.data.currentIndex, this.data.merchantId); }, + /** + * 获得经纬度 + */ + getLocation() { + console.log('getLocation'); + let that = this; + tt.getLocation({ + type: "wgs84", + // isHighAccuracy: true, + success: function (res) { + console.log(res, 'getLocation') + that.setData({ + latitude: res.latitude, + longitude: res.longitude, + }) + const latitudeNow = res.latitude + const longitudeNow = res.longitude + + const mallLocationArr = [] + const mallList = that.data.mallList + mallList.forEach((item, index) => { + const obj = { + latitude: item.latitude || false, + longitude: item.longitude || false, + id: item.id, + index: index + } + mallLocationArr.push(obj) + }) + console.log(mallLocationArr, 'mallLocationArr'); + + const tempArr = mallList + const bloobArr = [] + + mallLocationArr.forEach((item, index) => { + const distanceData = that.getDistances(latitudeNow, longitudeNow, item.latitude, item.longitude) + tempArr[index].distance_str = distanceData.distance_str + tempArr[index].distanceName = tempArr[index].merchantName + ' ' + distanceData.distance_str + tempArr[index].distance = distanceData.distance + bloobArr.push(tempArr[index]) + }) + + const finalSortMallList = that.bloob(bloobArr) + console.log(finalSortMallList, 'finalSortMallList'); + that.setData({ + mallList: finalSortMallList + }) + }, + // 拒绝提供定位权限 + fail: error => { + + }, + complete: finish => { + + } + }) + }, + chooseAddress() { if (!this.data.isShowBtns) return let that = this; @@ -280,5 +361,69 @@ Page({ }).catch(err => { console.log(err, 'getOrderReservationDetail'); }) + }, + + /** + * @description:根据经纬度计算距离 + * @param {*} locationInfo (lat1, lng1, lat2, lng2) + * @return: distanceObj: { distance , distance_str } + */ + getDistances(lat1, lng1, lat2, lng2) { + + if (lat2 || lng2) { + function rad(num) { + return num * Math.PI / 180.0; + } + var radLat1 = rad(lat1); + var radLat2 = rad(lat2); + var a = radLat1 - radLat2; + var b = rad(lng1) - rad(lng2); + var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2))); + s = s * 6378.137; + s = Math.round(s * 10000) / 10000; + + var distance = s; + var distance_str = ""; + + if (parseInt(distance) >= 1) { + distance_str = distance.toFixed(2) + "km"; + } else if (!arseInt(distance)) { + return false + } else { + distance_str = (distance * 1000).toFixed(2) + "m"; + } + + let objData = { + distance: distance, + distance_str: distance_str + } + return objData + } else { + let objData = { + distance: Infinity, + distance_str: '' + } + return objData + } + + }, + + bloob(arr) { + const tempArr = arr + for (let i = 0; i < tempArr.length - 1; i++) { + for (let j = 0; j < tempArr.length - 1 - i; j++) { + if (tempArr[j].distance > tempArr[j + 1].distance) { + let temp = tempArr[j]; + tempArr[j] = tempArr[j + 1]; + tempArr[j + 1] = temp; + } + } + } + return tempArr + }, + + onShow() { + this.getLocation() } }) \ No newline at end of file diff --git a/package2/pages/appointment/appointment.ttml b/package2/pages/appointment/appointment.ttml index 0027137..edad720 100644 --- a/package2/pages/appointment/appointment.ttml +++ b/package2/pages/appointment/appointment.ttml @@ -32,20 +32,24 @@ - - 选择可用商户: - - + + 选择可用商户: + + 请选择 - - - {{ mallList[currentIndex].merchantName }} - + + + 选择可用商户:{{ mallList[currentIndex].merchantName }} {{ mallList[currentIndex].distance_str }} + + + diff --git a/package2/pages/appointment/appointment.ttss b/package2/pages/appointment/appointment.ttss index 57f4f57..af8b96c 100644 --- a/package2/pages/appointment/appointment.ttss +++ b/package2/pages/appointment/appointment.ttss @@ -30,6 +30,14 @@ page { margin: 35rpx auto; } +.pickerInside { + color: #8a8a8a; +} + +.distanceFoot { + font-size: 25rpx; + color: #3c89ff; +} .datePicker { background-color: #fff;