动态的设置中心坐标和缩放级别


来自:https://yq.aliyun.com/ziliao/404630

// 获取中心点
setCenter(points) {
      // 获取折线中心点坐标
      if (points.length > 0) {
        let maxLng = points[0].lng
        let minLng = points[0].lng
        let maxLat = points[0].lat
        let minLat = points[0].lat
        let res
        for (let i = points.length - 1; i >= 0; i--) {
          res = points[i]
          if (res.lng > maxLng) maxLng = res.lng
          if (res.lng < minLng) minLng = res.lng
          if (res.lat > maxLat) maxLat = res.lat
          if (res.lat < minLat) minLat = res.lat
        }
        let cenLng = (parseFloat(maxLng) + parseFloat(minLng)) / 2
        let cenLat = (parseFloat(maxLat) + parseFloat(minLat)) / 2
        let zoom = this.getZoom(maxLng, minLng, maxLat, minLat)
        if (zoom < 0){
          zoom = 3
        }
        this.initPoint = {
          lng: cenLng,
          lat: cenLat
        }
        this.initZoom = zoom
      }else{
        this.initPoint = {
          lng: 103.388611,
          lat: 35.563611
        }
        this.initZoom = 5
      }
    },
    // 获取百度地图的缩放级别
    getZoom(maxLng, minLng, maxLat, minLat) {
      let zoom = ["50","100","200","500","1000","2000","5000","10000","20000","25000","50000","100000","200000","500000","1000000","2000000"];//级别18到3。      let pointA = new BMap.Point(maxLng, maxLat)
      let pointA = new BMap.Point(maxLng,maxLat)
      let pointB = new BMap.Point(minLng, minLat)
      let distance = this.map.getDistance(pointA, pointB).toFixed(1)
      for (let i = 0, zoomLen = zoom.length; i < zoomLen; i++) {
        if (zoom[i] - distance > 0) {
          return 18-i+3;
        }
      }
    },

转载自:https://blog.csdn.net/qq_32407233/article/details/83987864

You may also like...