Utils.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. 205.395583333332 = 205°23'44.1"
  3. 1,直接读取"度":205
  4. 2,(205.395583333332-205)*60=23.734999999920 得到"分":23
  5. 3,(23.734999999920-23)*60=44.099999995200 得到"秒":44.1
  6. return string,
  7. */
  8. function latLngDecimalToDegrees(decimal) {
  9. let absDecimal = Math.abs(decimal);
  10. let isNegative = Math.abs(decimal) != decimal;
  11. let degrees = Math.floor(absDecimal); //度
  12. let minutes = Math.floor((absDecimal - degrees) * 60); //分
  13. let seconds = Math.round((absDecimal - degrees) * 3600 % 60); //秒
  14. return (isNegative ? "-" : "") + degrees + '°' + minutes + "′" + seconds + '″';
  15. }
  16. /*
  17. Decimal Degrees = Degrees + minutes/60 + seconds/3600
  18. 例:57°55'56.6" =57+55/60+56.6/3600=57.9323888888888
  19. return Float or NaN
  20. */
  21. function latLngDegreesToDecimal(degreesStr) {
  22. let degreesArr = degreesStr.split("°");
  23. let degrees = degreesArr[0];
  24. let isNegative = Math.abs(degrees) != degrees;
  25. if (degreesArr.length == 1) {
  26. return parseInt(degrees);
  27. }
  28. let minutesArr = degreesArr[1].split("′");
  29. let minutes = minutesArr[0];
  30. if (minutesArr.length == 1) {
  31. return parseFloat((isNegative ? "-" : "") + (Math.abs(degrees) + Math.abs(minutes) / 60));
  32. }
  33. let secondsStr = minutesArr[1];
  34. let secondsArr = secondsStr.split('″');
  35. let seconds = secondsArr[0];
  36. return parseFloat((isNegative ? "-" : "") + (Math.abs(degrees) + Math.abs(minutes) / 60 + Math.abs(seconds) / 3600));
  37. }
  38. function isObject(arg) {
  39. return typeof arg === 'object' && arg !== null;
  40. }
  41. function isUndefined(arg) {
  42. return arg === void 0;
  43. }
  44. function isString(arg) {
  45. return typeof arg === 'string';
  46. }
  47. function hasLine(obj) {
  48. return (isObject(obj)
  49. && isSafeString(obj.addr)
  50. && isSafeString(obj.lat)
  51. && isSafeString(obj.lng));
  52. }
  53. function isSafeString(arg) {
  54. if (isUndefined(arg)) {
  55. return false;
  56. }
  57. if (!isString(arg)) {
  58. return false;
  59. }
  60. if (arg.length == 0) {
  61. return false;
  62. }
  63. return true;
  64. }
  65. function hasPoint(obj) {
  66. return (isObject(obj)
  67. && isSafeString(obj.lat)
  68. && isSafeString(obj.lng));
  69. }
  70. module.exports = {
  71. latLngDecimalToDegrees,
  72. latLngDegreesToDecimal,
  73. isObject,
  74. isUndefined,
  75. isString,
  76. isSafeString,
  77. hasLine,
  78. hasPoint
  79. }