Amap sets the center point and zoom of the map through an overlay on the screen

This overlay needs to be placed in the middle of the map and does not exceed the map boundary.

Calculate the center point of the covering and set it as the center point of the map

Calculate the maximum latitude and longitude of the cover, and the minimum latitude and longitude, measure the actual longest distance, and set zoom according to the corresponding relationship between distance and 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);
}

Guess you like

Origin blog.csdn.net/qq_51389137/article/details/132758209