index.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. Component({
  2. properties: {
  3. changeCommunity: {
  4. type: Object,
  5. value: {}
  6. },
  7. community: {
  8. type: Object,
  9. value: {}
  10. },
  11. visible: {
  12. type: Boolean,
  13. value: false
  14. },
  15. canChange: {
  16. type: Boolean,
  17. value: true
  18. },
  19. groupInfo: {
  20. type: Object,
  21. value: {
  22. group_name: '社区',
  23. owner_name: '团长'
  24. }
  25. },
  26. cancelFn: {
  27. type: Boolean,
  28. value: false
  29. }
  30. },
  31. attached() {
  32. this.countDistance()
  33. },
  34. methods: {
  35. countDistance: function () {
  36. let that = this;
  37. wx.getLocation({
  38. type: 'wgs84',
  39. success(res) {
  40. let {changeCommunity, community} = that.data;
  41. const latitude = res.latitude;
  42. const longitude = res.longitude;
  43. let lat1 = community.lat || '';
  44. let lon1 = community.lon || '';
  45. let lat2 = changeCommunity.lat || '';
  46. let lon2 = community.lon || '';
  47. if(lat1 && lon1 && lat2 && lon2) {
  48. let distance1 = that.getDistance(latitude, longitude, lat1, lon1);
  49. let distance2 = that.getDistance(latitude, longitude, lat2, lon2);
  50. community.distance = "距您"+distance1.toFixed(2)+"km";
  51. changeCommunity.distance = "距您"+distance2.toFixed(2)+"km";
  52. that.setData({ community, changeCommunity })
  53. }
  54. }
  55. })
  56. },
  57. getDistance: function (lat1, lng1, lat2, lng2) {
  58. var radLat1 = lat1 * Math.PI / 180.0;
  59. var radLat2 = lat2 * Math.PI / 180.0;
  60. var a = radLat1 - radLat2;
  61. var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
  62. var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
  63. Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
  64. s = s * 6378.137;
  65. s = Math.round(s * 10000) / 10000;
  66. return s;
  67. },
  68. switchCommunity: function (e) {
  69. let type = e.currentTarget.dataset.type;
  70. if (type == 0 || !this.data.canChange) {
  71. this.closeModal();
  72. } else {
  73. this.data.canChange && this.triggerEvent('changeComunity'), getApp().globalData.goodsListCarCount = [];
  74. }
  75. },
  76. closeModal: function () {
  77. this.data.cancelFn && this.triggerEvent('noChange');
  78. this.setData({
  79. visible: false
  80. })
  81. }
  82. }
  83. })