location_point.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>记录</title>
  7. <script src="https://web.tianyunperfect.cn/simple/js/util.js"></script>
  8. <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.2/css/bootstrap.min.css">
  9. <style>
  10. #getLocationBtn {
  11. transform: scale(3);
  12. }
  13. #app {
  14. display: flex;
  15. justify-content: center;
  16. align-items: center;
  17. height: 100vh;
  18. }
  19. </style>
  20. <script>
  21. window.addEventListener('load', function() {
  22. var app = document.body;
  23. var startY;
  24. var dist;
  25. var threshold = 100; // 下拉刷新的阈值
  26. app.addEventListener('touchstart', function(e) {
  27. startY = e.touches[0].clientY;
  28. });
  29. app.addEventListener('touchmove', function(e) {
  30. dist = e.touches[0].clientY - startY;
  31. if (dist > threshold) {
  32. // 执行刷新操作
  33. location.reload();
  34. }
  35. });
  36. });
  37. </script>
  38. </head>
  39. <body>
  40. <div id="app" class="container">
  41. <div class="row justify-content-center mt-5">
  42. <button id="getLocationBtn" class="btn btn-primary btn-lg" v-on:click="sendLocationToBackend">记录</button>
  43. </div>
  44. </div>
  45. <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.5.16/vue.min.js"></script>
  46. <script src="https://cdn.bootcdn.net/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
  47. <script src="https://cdn.bootcdn.net/ajax/libs/axios/1.3.6/axios.min.js"></script>
  48. <script src="https://api.map.baidu.com/api?v=2.0&ak=WMYeXwq7z0VcaiUXTuYTGPZsm8Selmfd"></script>
  49. <script>
  50. const app = new Vue({
  51. el: '#app',
  52. data: {
  53. longitude: "0",
  54. latitude: "0",
  55. locationStr: "未知"
  56. },
  57. mounted: function () {
  58. this.getLocation();
  59. },
  60. methods: {
  61. // 获取地址位置的名字
  62. getLocationStr: function (longitude, latitude) {
  63. // 创建地理编码实例, 并配置参数获取乡镇级数据
  64. const myGeo = new BMap.Geocoder({extensions_town: true});
  65. // 根据坐标得到地址描述
  66. myGeo.getLocation(new BMap.Point(longitude, latitude), function (result) {
  67. if (result) {
  68. app.locationStr = result.address;
  69. }
  70. });
  71. },
  72. // 获取经纬度
  73. getLocation: function () {
  74. // 创建百度地理位置实例,代替 navigator.geolocation
  75. let _this = this;
  76. const geolocation = new BMap.Geolocation();
  77. geolocation.getCurrentPosition(function (e) {
  78. if (this.getStatus() == BMAP_STATUS_SUCCESS) {
  79. // 百度 geolocation 的经纬度属性不同,此处是 point.lat 而不是 coords.latitude
  80. _this.latitude = e.point.lat;
  81. _this.longitude = e.point.lng;
  82. _this.getLocationStr(_this.longitude, _this.latitude);
  83. }
  84. });
  85. },
  86. sendLocationToBackend: async function () {
  87. const data = {
  88. location: app.locationStr,
  89. longitude: app.longitude,
  90. latitude: app.latitude,
  91. created_by: "b90c0cf6997d4cfa987f2bc59583fcaa"
  92. };
  93. axios.post('https://api.tianyunperfect.cn/location/point', data)
  94. .then(function (response) {
  95. console.log(response.data);
  96. let res = response.data;
  97. showMsg(res.message, 2);
  98. })
  99. .catch(function (error) {
  100. console.log(error);
  101. });
  102. }
  103. }
  104. });
  105. </script>
  106. </body>
  107. </html>