common.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. var EARTH_RADIUS = 6378137.0 // 单位M
  2. var PI = Math.PI
  3. function getRad (d) {
  4. return d * PI / 180.0
  5. }
  6. export function getFontSize(){
  7. const res = uni.getSystemInfoSync()
  8. var width=res.windowWidth
  9. var size=20
  10. if(width>641){
  11. size=23
  12. }else if(width>414){
  13. size=22
  14. }else if(width>376){
  15. size=21
  16. }
  17. return size
  18. }
  19. /**
  20. * caculate the great circle distance
  21. * @param {Object} lat1
  22. * @param {Object} lng1
  23. * @param {Object} lat2
  24. * @param {Object} lng2
  25. */
  26. export function getDistance (lat1, lng1, lat2, lng2) {
  27. var f = getRad((lat1 + lat2) / 2)
  28. var g = getRad((lat1 - lat2) / 2)
  29. var l = getRad((lng1 - lng2) / 2)
  30. var sg = Math.sin(g)
  31. var sl = Math.sin(l)
  32. var sf = Math.sin(f)
  33. var s, c, w, r, d, h1, h2
  34. var a = EARTH_RADIUS
  35. var fl = 1 / 298.257
  36. sg = sg * sg
  37. sl = sl * sl
  38. sf = sf * sf
  39. s = sg * (1 - sl) + (1 - sf) * sl
  40. c = (1 - sg) * (1 - sl) + sf * sl
  41. w = Math.atan(Math.sqrt(s / c))
  42. r = Math.sqrt(s * c) / w
  43. d = 2 * w * a
  44. h1 = (3 * r - 1) / 2 / c
  45. h2 = (3 * r + 1) / 2 / s
  46. return d * (1 + fl * (h1 * sf * (1 - sg) - h2 * (1 - sf) * sg))
  47. }
  48. // 将一个对象转成QueryString
  49. export function urlencode (data) {
  50. var _result = []
  51. for (var key in data) {
  52. var value = data[key]
  53. _result.push(key + '=' + value)
  54. }
  55. return _result.join('&')
  56. }
  57. // 加载js
  58. let scriptLoaded={}
  59. export function loadScript (code, url, callback) {
  60. if (typeof (scriptLoaded[code]) === 'undefined') {
  61. let script = document.createElement('script')
  62. script.src = url
  63. document.body.appendChild(script)
  64. script.onload = function () {
  65. scriptLoaded[code] = true
  66. callback()
  67. }
  68. } else {
  69. callback()
  70. }
  71. }
  72. // 加载css
  73. let cssLoaded={}
  74. export function loadCSS (code, url) {
  75. let link = document.createElement('link')
  76. link.href = url
  77. link.type = 'text/css'
  78. link.rel = 'stylesheet'
  79. document.body.appendChild(link)
  80. link.onload = function () {
  81. cssLoaded[code] = true
  82. }
  83. }