Kaynağa Gözat

引入百度地图依赖,腾讯地图定位转百度地图并解析省市区

zwq 6 yıl önce
ebeveyn
işleme
66f25aed69
2 değiştirilmiş dosya ile 33 ekleme ve 6 silme
  1. 2 1
      package.json
  2. 31 5
      src/view/user/index.vue

+ 2 - 1
package.json

@@ -10,14 +10,15 @@
     "lint": "vue-cli-service lint"
   },
   "dependencies": {
+    "amfe-flexible": "^2.2.1",
     "axios": "^0.18.0",
     "core-js": "^3.4.3",
     "vant": "^2.2.0",
     "vue": "^2.6.10",
+    "vue-baidu-map": "^0.21.22",
     "vue-router": "^3.0.5",
     "vuex": "^3.0.1",
     "vuex-class": "^0.3.1",
-    "amfe-flexible": "^2.2.1",
     "weixin-jsapi": "^1.1.0"
   },
   "devDependencies": {

+ 31 - 5
src/view/user/index.vue

@@ -36,6 +36,7 @@
 import { Row, Col, Icon, Cell, CellGroup,Toast } from 'vant';
 import {getAction} from '@/api/manage'
 import wx from 'weixin-jsapi'
+import BMap from 'vue-baidu-map'
 
 export default {
   components: {
@@ -47,6 +48,9 @@ export default {
   },
     data() {
         return {
+            province:'',
+            city:'',
+            district:'',
             url: {
                 getJsapiConfig: '/f/api/wechat/getJsapiConfig',
             }
@@ -89,17 +93,39 @@ export default {
               type: 'gcj02', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
               success: function(res) {
                   console.log(JSON.stringify(res))
-                  // var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90
-                  // var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。
-                  // var speed = res.speed; // 速度,以米/每秒计
-                  // var accuracy = res.accuracy; // 位置精度
+                  var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90
+                  var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。
+                  this.changeLocation(latitude,longitude);
               },
               cancel: function() {
                   console.log("获取位置失败,请检查定位");
                   Toast("获取位置失败,请检查定位")
               }
           });
-      }
+      },
+
+      //腾讯地图经纬度转百度地图经纬度,并解析省市区
+      changeLocation(latitude, longitude){
+          var x_pi = 3.14159265358979324 * 3000.0 / 180.0;
+          var x = parseFloat(longitude);
+          var y = parseFloat(latitude);
+          var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
+          var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
+          longitude = z * Math.cos(theta) + 0.0065;
+          latitude = z * Math.sin(theta) + 0.006;
+
+          //百度地图
+          var point = new BMap.Point(longitude, latitude);
+          var gc = new BMap.Geocoder();
+
+          gc.getLocation(point, function(rs) {
+              var addComp = rs.addressComponents;
+              console.log("addComp省市区信息=",addComp);
+              this.province = addComp.province;
+              this.city = addComp.city;
+              this.district = addComp.district;
+          });
+      },
     }
 };
 </script>