Jelajahi Sumber

地图距离bug修复

yfape 2 tahun lalu
induk
melakukan
68ba9a87c9
3 mengubah file dengan 26 tambahan dan 24 penghapusan
  1. 1 1
      components/hch-position/hch-position.vue
  2. 11 9
      pages/map/map.vue
  3. 14 14
      utils/tools.js

+ 1 - 1
components/hch-position/hch-position.vue

xqd
@@ -2,7 +2,7 @@
 	<view style="height:100%;">
 		<view class="page-body" style="height:100%;">
 			<view class="page-section page-section-gap" style="height:100%;">
-				<map scale="12"  ubkey='CQIBZ-P2MR5-PM3IB-QRKTX-SEVJE-GYF34' :showScale="false" :showLocation="true"  :showCompass="false" enableZoom="true" enableScroll="true" enableBuilding="true" enable3D="true" id="myMap" style="width: 100%; height: 100%;"
+				<map scale="9"  ubkey='CQIBZ-P2MR5-PM3IB-QRKTX-SEVJE-GYF34' :showScale="false" :showLocation="true"  :showCompass="false" enableZoom="true" enableScroll="true" enableBuilding="true" enable3D="true" id="myMap" style="width: 100%; height: 100%;"
 					:latitude="innerGeo.latitude" 
 					:longitude="innerGeo.longitude"
 					:markers="markersIn" 

+ 11 - 9
pages/map/map.vue

xqd xqd xqd xqd xqd xqd
@@ -261,7 +261,8 @@
 				beginConfig:{
 					isBegin: true,
 					hotel_id: 0
-				}
+				},
+				randomNum: 0
 			}
 		},
 		onShow() {
@@ -274,7 +275,6 @@
 				this.search.currentSelected = category_ids.split(",")
 				this.search.selected = Object.assign(this.search.currentSelected)
 			}
-			console.log(hotel_id,category_ids)
 			//获取经纬度
 			this.getHotelCategory()
 			
@@ -346,9 +346,10 @@
 			goLocation(){
 				uni.getLocation({
 					type: "gcj02", //返回可以用于wx.openLocation的经纬度
-					success: (res) => {
+					success: (res) => {
+						this.randomNum = Math.floor(Math.random() * 100) / 1000000;
 						this.geo = {
-							latitude: res.latitude + Math.random()/10000000,
+							latitude: res.latitude + this.randomNum,
 							longitude: res.longitude
 						}
 					},
@@ -361,11 +362,12 @@
 			updated() {
 				uni.getLocation({
 					type: "gcj02", //返回可以用于wx.openLocation的经纬度
-					success: (res) => {
+					success: (res) => {
+						this.randomNum = Math.floor(Math.random() * 100) / 1000000;
 						this.geo = {
-							latitude: res.latitude + Math.random()/10000000,
+							latitude: res.latitude + this.randomNum,
 							longitude: res.longitude
-						}
+						}
 						this.getList(true)
 						//获取酒店列表
 					},
@@ -395,7 +397,7 @@
 						}
 					})
 					tempHotelList.map(item=>{
-						item.distanceToMe=this.$utils.calcDistance(this.latitude,this.longitude,item.latitude,item.longitude).toFixed(1);
+						item.distanceToMe=this.$utils.calcDistance(this.geo.latitude,this.geo.longitude,item.latitude,item.longitude).toFixed(1);
 						return item;
 					})
 					this.hotelList.push(...tempHotelList)
@@ -425,7 +427,7 @@
 					})
 				}
 				if(this.geo.latitude&&this.geo.longitude){
-					tempobj['latitude'] = this.geo.latitude;
+					tempobj['latitude'] = this.geo.latitude - this.randomNum;
 					tempobj['longitude'] = this.geo.longitude;
 				}
 				if(this.search.text){

+ 14 - 14
utils/tools.js

xqd
@@ -174,21 +174,21 @@ const arrayDiff = function(arr1, arr2) {
 /**
  * 测试两个地理位置间得距离
  */
-const calcDistance = (lat1,lng1,lat2,lng2)=>{
-	let radLat1 = Rad(lat1);
-	let radLat2 = Rad(lat2);
-	let a = radLat1 - radLat2;
-	let  b = Rad(lng1) - Rad(lng2);
-	let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
-	Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
-	s = s *6378.137 ;// EARTH_RADIUS;
-	s = Math.round(s * 10000) / 10000; //输出为公里
-	//s=s.toFixed(4);
-	return s;
-}
+const calcDistance = (lat1, lng1, lat2, lng2) => {
+    lat1 = lat1 || 0;
+    lng1 = lng1 || 0;
+    lat2 = lat2 || 0;
+    lng2 = lng2 || 0;
+
+    let rad1 = lat1 * Math.PI / 180.0;
+    let rad2 = lat2 * Math.PI / 180.0;
+    let a = rad1 - rad2;
+    let b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
+    let r = 6378137;
+    let distance = r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(b / 2), 2)));
 
-const Rad = (d) => {
-    return d * Math.PI / 180.0;//经纬度转换成三角函数中度分表形式。
+	distance = Math.ceil(distance * 10) / 10000;
+    return distance;
 }
 
 export default {