import Storage from './../store/storage.js'; // 获取弧度 // 根据两点的经纬度计算距离 function getRad(d) { return (d * Math.PI) / 180.0; }; // 计算距离 lat1,lng1 第一个坐标 lat2,lng2 目标坐标 function getFlatternDistance(lat1, lng1, lat2, lng2) { var f = getRad((lat1 + lat2) / 2); var g = getRad((lat1 - lat2) / 2); var l = getRad((lng1 - lng2) / 2); var sg = Math.sin(g); var sl = Math.sin(l); var sf = Math.sin(f); var s, c, w, r, d, h1, h2; var a = 6378137.0; var fl = 1 / 298.257; sg = sg * sg; sl = sl * sl; sf = sf * sf; s = sg * (1 - sl) + (1 - sf) * sl; c = (1 - sg) * (1 - sl) + sf * sl; w = Math.atan(Math.sqrt(s / c)); r = Math.sqrt(s * c) / w; d = 2 * w * a; h1 = (3 * r - 1) / 2 / c; h2 = (3 * r + 1) / 2 / s; return d * (1 + fl * (h1 * sf * (1 - sg) - h2 * (1 - sf) * sg)); } let latitude = ''; let longitude = ''; // 计算距离 function calculateDistance(shopPosition) { var arr = Storage.getCache('longitude') latitude = arr.latitude longitude = arr.longitude if (latitude && longitude) { const [lat, lon] = shopPosition.split(",") const cal = getFlatternDistance(latitude, longitude, Number(lat), Number(lon)) const number = Math.round(cal) if (cal > 999) { return (number / 1000).toFixed(1) + 'km' } else { return number + '米' } } else { return '' } } export default calculateDistance;