tianyun 2 years ago
parent
commit
3394a14058
2 changed files with 130 additions and 5 deletions
  1. 94 0
      simple-demo/location_point.html
  2. 36 5
      tmp/index.html

+ 94 - 0
simple-demo/location_point.html

@@ -0,0 +1,94 @@
+<!DOCTYPE html>
+<html>
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>记录</title>
+    <script src="https://web.tianyunperfect.cn/simple/js/util.js"></script>
+    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.2/css/bootstrap.min.css">
+</head>
+<body>
+<div id="app" class="container">
+    <div class="row justify-content-center mt-5">
+        <button id="getLocationBtn" class="btn btn-primary btn-lg" v-on:click="sendLocationToBackend">记录</button>
+    </div>
+    <div class="row justify-content-center mt-3">
+        <label for="longitude">经度:</label>
+        <label id="longitude" v-model="longitude" class="ml-2"></label>
+    </div>
+    <div class="row justify-content-center mt-2">
+        <label for="latitude">纬度:</label>
+        <label id="latitude" v-model="latitude" class="ml-2"></label>
+    </div>
+    <div class="row justify-content-center mt-2">
+        <label for="locationStr">位置:</label>
+        <label id="locationStr" v-model="locationStr" class="ml-2"></label>
+    </div>
+</div>
+
+<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.5.16/vue.min.js"></script>
+<script src="https://cdn.bootcdn.net/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
+<script src="https://cdn.bootcdn.net/ajax/libs/axios/1.3.6/axios.min.js"></script>
+<script src="https://api.map.baidu.com/api?v=2.0&ak=WMYeXwq7z0VcaiUXTuYTGPZsm8Selmfd"></script>
+<script>
+
+
+    const app = new Vue({
+        el: '#app',
+        data: {
+            longitude: "未知",
+            latitude: "未知",
+            locationStr: "未知"
+        },
+        mounted: function () {
+            this.getLocation();
+        },
+        methods: {
+            // 获取地址位置的名字
+            getLocationStr: function (longitude, latitude) {
+                // 创建地理编码实例, 并配置参数获取乡镇级数据
+                const myGeo = new BMap.Geocoder({extensions_town: true});
+                // 根据坐标得到地址描述
+                myGeo.getLocation(new BMap.Point(longitude, latitude), function (result) {
+                    if (result) {
+                        app.locationStr = result.address;
+                    }
+                });
+            },
+
+            // 获取经纬度
+            getLocation: function () {
+                // 创建百度地理位置实例,代替 navigator.geolocation
+                let _this = this;
+                const geolocation = new BMap.Geolocation();
+                geolocation.getCurrentPosition(function (e) {
+                    if (this.getStatus() == BMAP_STATUS_SUCCESS) {
+                        // 百度 geolocation 的经纬度属性不同,此处是 point.lat 而不是 coords.latitude
+                        _this.latitude = e.point.lat;
+                        _this.longitude = e.point.lng;
+                        _this.getLocationStr(_this.longitude, _this.latitude);
+                    }
+                });
+            },
+            sendLocationToBackend: async function () {
+                const data = {
+                    location: app.locationStr,
+                    longitude: app.longitude,
+                    latitude: app.latitude,
+                    created_by: "b90c0cf6997d4cfa987f2bc59583fcaa"
+                };
+                axios.post('https://api.tianyunperfect.cn/location/point', data)
+                    .then(function (response) {
+                        console.log(response.data);
+                        let res = response.data;
+                        showMsg(res.message, 2);
+                    })
+                    .catch(function (error) {
+                        console.log(error);
+                    });
+            }
+        }
+    });
+</script>
+</body>
+</html>

+ 36 - 5
tmp/index.html

@@ -1,12 +1,43 @@
+<!DOCTYPE html>
 <html>
 <head>
-    <title>JavaScript自动关闭窗口</title>
-    <meta http-equiv="content-Type" content="text/html;charset=utf-8">
-    <script src="../monkey/util.js"></script>
+    <meta charset="UTF-8">
+    <title>用户信息</title>
+    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css">
 </head>
 <body>
+<div class="container">
+    <h1>用户信息</h1>
+    <form id="userForm">
+        <div class="form-group">
+            <label for="name">姓名:</label>
+            <input type="text" class="form-control" id="name" placeholder="请输入姓名">
+        </div>
+        <div class="form-group">
+            <label for="age">年龄:</label>
+            <input type="number" class="form-control" id="age" placeholder="请输入年龄">
+        </div>
+        <div class="form-group">
+            <label for="city">城市:</label>
+            <input type="text" class="form-control" id="city" placeholder="请输入城市">
+        </div>
+        <button type="submit" class="btn btn-primary">发送</button>
+    </form>
+</div>
+<script src="https://cdn.jsdelivr.net/npm/vue@2.6.12/dist/vue.min.js"></script>
 <script>
-    showMsg("123",3000)
+    const app = new Vue({
+        el: '#userForm',
+        methods: {
+            submitForm() {
+                const name = document.getElementById('name').value;
+                const age = document.getElementById('age').value;
+                const city = document.getElementById('city').value;
+                // 将用户输入的数据发送到后台
+                // 发送方式可以使用fetch或axios等库来实现
+            }
+        }
+    });
 </script>
 </body>
-</html>
+</html>