ws 1 year ago
parent
commit
36b661f761

+ 4 - 0
nngkxxdp/src/main/java/com/example/nngkxxdp/entity/MapData.java

@@ -117,4 +117,8 @@ public class MapData implements Serializable {
 	 */
 	private String labels;
 
+	private String latitude;
+
+	private String longitude;
+
 }

+ 1 - 1
nngkxxdp/src/main/resources/mapper/MapDataDao.xml

@@ -92,7 +92,7 @@
         id
         , dict_type, address, content, dep, img, gzh, sptype, tel,
 		time, url, type, tag, tag2, tag3, specialtype, create_time,
-		update_time,labels
+		update_time,labels,latitude,longitude
     </sql>
 
     <!-- 批量保存地图数据 -->

+ 66 - 35
nnzwminiapp/pages/map/map.js

@@ -1042,6 +1042,7 @@ function searchDepVal(that, queryVal, deptArr) {
 
 function initMapDataAll(that, lat, lng) {
     let arr = that.data.marksInfo
+    console.log(arr)
     let index = 0;
     let addrArr = []
     let marksView = []
@@ -1067,40 +1068,57 @@ function initMapDataAll(that, lat, lng) {
       }
       const address = arr[index]
       index++
-      BMap.geocoder({
-        address: address.address,
-        success: (addr) => {
-          BMap.calculateDistance({
-            mode: 'straight',
-            from: {
-              latitude: lat,
-              longitude: lng
-            },
-            to: [{
-              latitude: addr.result.location.lat,
-              longitude: addr.result.location.lng
-            }],
-            success: (json) => {
-              console.log(json)
-              const jl = parseInt(json.result.elements[0].distance)
-              if (jl <= 1000) {
-                marksView.push(address)
-                addrArr.push({
-                  id: address.id,
-                  latitude: json.result.elements[0].to.lat,
-                  longitude: json.result.elements[0].to.lng,
-                  iconPath: imgUrl+"/location.png",
-                  width: '34px',
-                  height: '34px',
-                  rotate: 0,
-                  alpha: 1
-                })
-              }
-            }
-          })
-        }
-      })
-    }, 300);
+      const jl = getDistances(lat, lng, address.latitude, address.longitude)
+          console.log(jl)
+          if (jl.m <= 1000) {
+            marksView.push(address)
+            addrArr.push({
+              id: address.id,
+              latitude: address.latitude,
+              longitude: address.longitude,
+              iconPath: imgUrl+"/location.png",
+              width: '34px',
+              height: '34px',
+              rotate: 0,
+              alpha: 1
+            })
+          }
+      // BMap.geocoder({
+      //   address: address.address,
+      //   success: (addr) => {
+          
+          // BMap.calculateDistance({
+          //   mode: 'straight',
+          //   from: {
+          //     latitude: lat,
+          //     longitude: lng
+          //   },
+          //   to: [{
+          //     latitude: addr.result.location.lat,
+          //     longitude: addr.result.location.lng
+          //   }],
+          //   success: (json) => {
+          //     console.log(getDistances(lat, lng, addr.result.location.lat, addr.result.location.lng))
+          //     console.log(json)
+          //     const jl = parseInt(json.result.elements[0].distance)
+          //     if (jl <= 1000) {
+          //       marksView.push(address)
+          //       addrArr.push({
+          //         id: address.id,
+          //         latitude: json.result.elements[0].to.lat,
+          //         longitude: json.result.elements[0].to.lng,
+          //         iconPath: imgUrl+"/location.png",
+          //         width: '34px',
+          //         height: '34px',
+          //         rotate: 0,
+          //         alpha: 1
+          //       })
+          //     }
+          //   }
+          // })
+        // }
+      // })
+    }, 10);
 }
 
 function searchHandMatterByValFun(that) {
@@ -1128,4 +1146,17 @@ function searchHandMatterByValFun(that) {
       matterEventInfo: matterEventInfo
     })
   });
-}
+}
+
+// 根据经纬度计算距离,参数分别为第一点的纬度,经度;第二点的纬度,经度
+function getDistances(lat1, lng1, lat2, lng2) {
+	let EARTH_RADIUS = 6378.137;// 地球半径
+	let radLat1 = lat1 * Math.PI / 180.0; //lat1 * Math.PI / 180.0=>弧度计算
+	let radLat2 = lat2 * Math.PI / 180.0;
+	let a = radLat1 - radLat2;
+	let b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
+	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 * EARTH_RADIUS; 
+	s = Math.round(s * 10000) / 10000;// 输出为公里
+	return { m: s * 1000, km: Number(s.toFixed(2)) }
+}