location_point.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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(2);
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div id="app" class="container">
  17. <div class="row justify-content-center mt-5">
  18. <button id="getLocationBtn" class="btn btn-primary btn-lg" v-on:click="sendLocationToBackend">记录</button>
  19. </div>
  20. <!-- <div class="row justify-content-center mt-3">-->
  21. <!-- <label for="longitude">经度:</label>-->
  22. <!-- <label id="longitude" class="ml-2">{{longitude}}</label>-->
  23. <!-- </div>-->
  24. <!-- <div class="row justify-content-center mt-2">-->
  25. <!-- <label for="latitude">纬度:</label>-->
  26. <!-- <label id="latitude" class="ml-2">{{latitude}}</label>-->
  27. <!-- </div>-->
  28. <!-- <div class="row justify-content-center mt-2">-->
  29. <!-- <label for="locationStr">位置:</label>-->
  30. <!-- <label id="locationStr" class="ml-2">{{locationStr}}</label>-->
  31. <!-- </div>-->
  32. </div>
  33. <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.5.16/vue.min.js"></script>
  34. <script src="https://cdn.bootcdn.net/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
  35. <script src="https://cdn.bootcdn.net/ajax/libs/axios/1.3.6/axios.min.js"></script>
  36. <script src="https://api.map.baidu.com/api?v=2.0&ak=WMYeXwq7z0VcaiUXTuYTGPZsm8Selmfd"></script>
  37. <script>
  38. const app = new Vue({
  39. el: '#app',
  40. data: {
  41. longitude: "0",
  42. latitude: "0",
  43. locationStr: "未知"
  44. },
  45. mounted: function () {
  46. this.getLocation();
  47. },
  48. methods: {
  49. // 获取地址位置的名字
  50. getLocationStr: function (longitude, latitude) {
  51. // 创建地理编码实例, 并配置参数获取乡镇级数据
  52. const myGeo = new BMap.Geocoder({extensions_town: true});
  53. // 根据坐标得到地址描述
  54. myGeo.getLocation(new BMap.Point(longitude, latitude), function (result) {
  55. if (result) {
  56. app.locationStr = result.address;
  57. }
  58. });
  59. },
  60. // 获取经纬度
  61. getLocation: function () {
  62. // 创建百度地理位置实例,代替 navigator.geolocation
  63. let _this = this;
  64. const geolocation = new BMap.Geolocation();
  65. geolocation.getCurrentPosition(function (e) {
  66. if (this.getStatus() == BMAP_STATUS_SUCCESS) {
  67. // 百度 geolocation 的经纬度属性不同,此处是 point.lat 而不是 coords.latitude
  68. _this.latitude = e.point.lat;
  69. _this.longitude = e.point.lng;
  70. _this.getLocationStr(_this.longitude, _this.latitude);
  71. }
  72. });
  73. },
  74. sendLocationToBackend: async function () {
  75. const data = {
  76. location: app.locationStr,
  77. longitude: app.longitude,
  78. latitude: app.latitude,
  79. created_by: "b90c0cf6997d4cfa987f2bc59583fcaa"
  80. };
  81. axios.post('https://api.tianyunperfect.cn/location/point', data)
  82. .then(function (response) {
  83. console.log(response.data);
  84. let res = response.data;
  85. showMsg(res.message, 2);
  86. })
  87. .catch(function (error) {
  88. console.log(error);
  89. });
  90. }
  91. }
  92. });
  93. </script>
  94. </body>
  95. </html>