js转化经纬度坐标

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>度分秒转小数点(小数点的分)</title>
  <style>

      .label {
          width: 232px;
          display: inline-block;
      }
  </style>
</head>

<body>
<!--<button id="transBtn">转换</button>-->
<button id="xsdToDfm">小数点转度分秒</button>
<button id="dfmToXsd">度分秒转小数点</button>
<br>
<label for="oldText" class="label transBefore">转换前</label>
<label for="newText" class="label transAfter">转换后</label>
<br>
<textarea name="" id="oldText" cols="30" rows="50" placeholder="支持:E111°11′11″、E1112233、E0791122格式,不用去掉E和度分秒符号"></textarea>
<textarea name="" id="newText" cols="30" rows="50"></textarea>
<script src="../jquery2.1.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/4.0.1/math.min.js"></script>
<script>


  function DFM(obj) {
    this.ToDfm = function (obj) {
      let newDfm = obj.split(".")[1].replace(/ /g, "").replace(/\'|\’/, "′").split("′")[0];
      let miao = Number("0." + newDfm) * 60 + "″";
      let duFen = obj.split(".")[0].replace(/ /g, "").replace(/\'|\’/g, "′") + "′";
      return $.trim(duFen + miao);
    };
    this.ToDuFenMiao = function (obj) {
      let d = parseInt(obj.replace(/' '|N|E/g, ''));
      let xs = Number("0." + String(obj).split('.')[1]);
      let f = parseInt(xs * 60);
      let xs2 = Number("0." + String(Number(xs * 60).toFixed(2)).split('.')[1]);
      let m = Math.round(xs2 * 60);
      return d + '°' + f + '' + m + '';
    };
    this.ToXiaoShuDian = function (obj) {
      if (obj.indexOf("°") !== -1) {
        obj = String(obj).replace(/‘ ’|N|E/g, "").replace(/\'|\’/g, "′").replace(/\"|\”/g, "″");
        let miao = Number(String(obj).split('')[1].split('')[0]) / 60;
        let du = String(obj).split("°")[0] + "." + String(Number(Number(miao) + Number(String(obj).split('°')[1].split('')[0])) * 1000 / (60 * 1000)).replace(/0./, "");
        return (du);
      } else {
        if (obj.indexOf("N") !== -1) {
          obj = String(obj).replace(/‘ ’|N/g, "");
          if (obj.length === 6) {
            obj = "0" + obj;
          } else if (obj.length === 5) {
            obj = "00" + obj;
          }
          let miao = Number(obj.substr(5, 2)) / 60
          let fen = Number(Number(obj.substr(3, 2)) + miao) / 60
          let du = obj.substr(0, 3)
          return du + "." + String(fen).replace(/0./, "")
        } else {
          obj = String(obj).replace(/‘ ’|E/g, "");
          if (obj.length === 6) {
            obj = "0" + obj;
          }
          let miao = Number(obj.substr(5, 2)) / 60
          let fen = Number(Number(obj.substr(3, 2)) + miao) / 60
          let du = obj.substr(0, 3)
          return du + "." + String(fen).replace(/0./, "")
        }
      }
    }
  }


  var transLatAndLng = new DFM();

  $("#transBtn").click(function () {
    if ($("#oldText").val() != "") {
      $("#newText").val(transLatAndLng.ToDfm($("#oldText").val()));
    }

  });
  $("#xsdToDfm").click(function () {
    $("#newText").val("");
    if ($("#oldText").val() != "") {
      let arr = $("#oldText").val().split("\n");
      for (let key in arr) {
        $("#newText").val($("#newText").val() + transLatAndLng.ToDuFenMiao(arr[key]) + "\n");
      }
    }
  });
  $("#dfmToXsd").click(function () {
    $("#newText").val("");
    if ($("#oldText").val() != "") {
      let arr = $("#oldText").val().split("\n");
      for (let key in arr) {
        $("#newText").val($("#newText").val() + transLatAndLng.ToXiaoShuDian(arr[key]) + "\n");
      }
    }
  });


</script>
</body>

</html>

 

posted @ 2021-08-02 15:30  ALin_Da  阅读(700)  评论(0编辑  收藏  举报