location.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import base from "./base"
  2. export function getLocation(callBack) {
  3. wx.getSetting({
  4. success(res) {
  5. // 1. scope.userLocation 为真, 代表用户已经授权
  6. if (res.authSetting['scope.userLocation']) {
  7. // 1.1 使用 getlocation 获取用户 经纬度位置
  8. wx.getLocation({
  9. success(res){
  10. // 1.2 获取用户位置成功后,将会返回 latitude, longitude 两个字段,代表用户的经纬度位置
  11. if (base.isFunc(callBack)) {
  12. callBack(res.longitude, res.latitude)
  13. }
  14. },
  15. fail() {
  16. }
  17. })
  18. }else {
  19. // 2. 用户未授权的情况下, 打开授权界面, 引导用户授权.
  20. wx.authorize({
  21. scope: "scope.userLocation",
  22. success(res) {
  23. wx.getLocation({
  24. success(res){
  25. if (base.isFunc(callBack)) {
  26. callBack(res.longitude, res.latitude)
  27. }
  28. },
  29. fail() {
  30. }
  31. })
  32. }
  33. })
  34. }
  35. }
  36. })
  37. }
  38. export function getCurrDistance(longitude, latitude, resultFunc) {
  39. getLocation(function(lng,lat) {
  40. let m = base.getDistance(latitude, longitude, lat, lng)
  41. console.log("m")
  42. console.log(m)
  43. let formatM1 = ""
  44. let formatM2 = ""
  45. if (m < 1000) {
  46. formatM1 = m + "m"
  47. formatM2 = m + "米"
  48. } else {
  49. let km = (m/1000).toFixed(2)
  50. console.log(km)
  51. formatM1 = km + "km"
  52. formatM2 = km + "千米"
  53. }
  54. if (base.isFunc(resultFunc)) {
  55. resultFunc(m, formatM1, formatM2)
  56. }
  57. })
  58. }