在做一個需求時有一個小點就是添加一個配送區域(5公里直徑內的)矩形圍欄
我做的比較簡單 大家看看有沒有幫助, 也是精簡代碼。測試效果上相對是精準的
//谷歌,根據經緯度獲取以它為中心半徑為5公里內的矩形的四個點經緯度getDefalutPoints = (lng: number, lat: number) => {//方法一:不精準// const num = 0.014607; //5公里半徑維度// const path1 = `${lng - num},${lat + num}`;// const path2 = `${lng + num},${lat + num}`;// const path3 = `${lng + num},${lat - num}`;// const path4 = `${lng - num},${lat - num}`;// return `${path1};${path2};${path3};${path4}`;//方法二//數字 111 代表的是地球表面上每度緯度大約對應的公里數。這是一個常用的近似值,用于簡化地球表面的計算,尤其是當需要快速估算或不需要非常高精度的場合。const radiusKm = 5;const latI = radiusKm / 111; //維度增量const lngI = radiusKm / (111 *Math.cos(lat* Math.PI/180));const zs = `${lng+lngI},${lat+latI}`;const ys = `${lng-lngI},${lat+latI}`;const zx = `${lng-lngI},${lat-latI}`;const yx = `${lng+lngI},${lat-latI}`;const points = `${zs};${ys};${zx};${yx}`;return points;};const lng = 150.644;
const lat = -34.397;
const defalutPoints = this.getDefalutPoints(lng, lat);
console.log(defalutPoints);
附上效果圖一張
以下代碼實現可參考鏈接:https://blog.csdn.net/weixin_43517190/article/details/140184814