import base from "./base" export function getLocation(callBack) { wx.getSetting({ success(res) { // 1. scope.userLocation 为真, 代表用户已经授权 if (res.authSetting['scope.userLocation']) { // 1.1 使用 getlocation 获取用户 经纬度位置 wx.getLocation({ success(res){ // 1.2 获取用户位置成功后,将会返回 latitude, longitude 两个字段,代表用户的经纬度位置 if (base.isFunc(callBack)) { callBack(res.longitude, res.latitude) } }, fail() { } }) }else { // 2. 用户未授权的情况下, 打开授权界面, 引导用户授权. wx.authorize({ scope: "scope.userLocation", success(res) { wx.getLocation({ success(res){ if (base.isFunc(callBack)) { callBack(res.longitude, res.latitude) } }, fail() { } }) } }) } } }) } export function getCurrDistance(longitude, latitude, resultFunc) { getLocation(function(lng,lat) { let m = base.getDistance(latitude, longitude, lat, lng) console.log("m") console.log(m) let formatM1 = "" let formatM2 = "" if (m < 1000) { formatM1 = m + "m" formatM2 = m + "米" } else { let km = (m/1000).toFixed(2) console.log(km) formatM1 = km + "km" formatM2 = km + "千米" } if (base.isFunc(resultFunc)) { resultFunc(m, formatM1, formatM2) } }) }