location_point.html 3.7 KB

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