需要将这个覆盖物置于地图中间且不超过地图边界的放至最大
计算覆盖物中心点,定为地图中心点
计算覆盖物的最大经纬度,和最小经纬度,测算出实际最长距离,根据距离与zoom对应关系设置zoom
function getAreaCenter(params, info) {
let code = info.code
if (!params.target) {
return;
}
let PolygonPath = params.target.getPath();
let PolygonArr = [];
PolygonPath.forEach((item) => {
let path = [item.KL, item.kT];
PolygonArr.push(path);
});
let total = PolygonArr.length;
if (total <= 0) {
return;
}
let X = 0;
let Y = 0;
let Z = 0;
PolygonArr.forEach((lnglat) => {
let lng = (lnglat[0] * Math.PI) / 180;
let lat = (lnglat[1] * Math.PI) / 180;
let x, y, z;
x = Math.cos(lat) * Math.cos(lng);
y = Math.cos(lat) * Math.sin(lng);
z = Math.sin(lat);
X += x;
Y += y;
Z += z;
});
X = X / total;
Y = Y / total;
Z = Z / total;
let Lng = Math.atan2(Y, X);
let Hyp = Math.sqrt(X * X + Y * Y);
let Lat = Math.atan2(Z, Hyp);
let center = [(Lng * 180) / Math.PI, (Lat * 180) / Math.PI];
let maxL = Math.max(...PolygonArr.map(item=> item[0]))
let minL = Math.min(...PolygonArr.map(item=> item[0]))
let maxK = Math.max(...PolygonArr.map(item=> item[1]))
let minK = Math.min(...PolygonArr.map(item=> item[1]))
let p1 = [maxL,maxK]
let p2 = [minL,minK]
let dis = AMap.GeometryUtil.distance(p1, p2);
let zoom = 12
let zooms = [[10000,11,],
[5000,12,],
[2000,15,],
[1000,16,],
[600,16.5,],
[500,17,],
[200,18,],
[100,19,],
[50,20,],
[15,21,],
[10,22,]]
for(let i = 0;i<zooms.length;i++){
let item = zooms[i]
if(dis>item[0]){
zoom = item[1]
break
}
}
map.setZoomAndCenter(zoom, center);
}