可视化地图--反缩放

react

先判断缩放方式,获取屏幕可视化宽高及页面定义的宽高:

const windowSize = ({ zoomStyle, width, height }, innerWidth, innerHeight) => {
  let widthPre = width;
  let heightPre = height;

  let scaleX = 1;
  let scaleY = 1;

  if (zoomStyle === 'screenFull') {
    scaleX = Math.floor((innerWidth / width) * 10000) / 10000;
    scaleY = Math.floor((innerHeight / height) * 10000) / 10000;
    widthPre = innerWidth;
    heightPre = innerHeight;
  }
  if (zoomStyle === 'widthFull') {
    scaleX = Math.floor((innerWidth / width) * 10000) / 10000;
    scaleY = scaleX;
    widthPre = innerWidth;
    heightPre = scaleX * height;
  }
  if (zoomStyle === 'heightFull') {
    scaleY = Math.floor((innerHeight / height) * 10000) / 10000;
    scaleX = scaleY;
    heightPre = innerHeight;
    widthPre = scaleY * width;
  }

  return {
    innerWidth,
    innerHeight,
    widthPre,
    heightPre,
    scaleX,
    scaleY
  };
}

// export const iframeEngineSize = ({ zoomStyle, width, height }, innerWidth, innerHeight) => windowSize({ zoomStyle, width, height }, innerWidth, innerHeight);

export const previewWindowSize = ({ zoomStyle, width, height }) => {
  const innerWidth = window.innerWidth
  || document.documentElement.clientWidth;
  const innerHeight = window.innerHeight
  || document.documentElement.clientHeight;

  return windowSize({ zoomStyle, width, height }, innerWidth, innerHeight);
};
在地图渲染文件中引入文件进行使用:
  function reverseWidthHeight() {
    const egismapEle = document.getElementById('id');//获取地图dom
    const { screenInfoData: {
      zoomStyle, width: sw, height: sh
    } } = bigScreen; //这块视情况修改
    const coordinate = {
      x: egismapEle.offsetLeft,
      y: egismapEle.offsetTop
    }
    // 方法:反向缩放
    let w = width;
    let h = height;
    let newScaleX = 1;
    let newScaleY = 1;
    let newTop = coordinate.y;
    let newLeft = coordinate.x;
    const {
      scaleX,
      scaleY
    } = previewWindowSize({ zoomStyle, width: sw, height: sh });
    w *= scaleX;
    h *= scaleY;
    newScaleX = Math.floor((1 / scaleX) * 100) / 100;
    newScaleY = Math.floor((1 / scaleY) * 100) / 100;
    newLeft = ((width - w) / 2);
    newTop = ((height - h) / 2);
    const divStyle = {
      width: w,
      height: h,
      transform: `scaleX(${newScaleX}) scaleY(${newScaleY})`,
      position: 'absolute',
      top: `${newTop}px`,
      left: `${newLeft}px`
    };
    setEgismapStyle(divStyle); // 这里是将样式存一下
  }

初始化加载页面时调用这个方法,然后在样式渲染时加上egismapStyle即可。

vue

// 反缩放去掉自适应-解决地图自适应时marker、hover错位问题

resetMapSize() {
  let screenWidth =
    document.body.clientWidth || document.documentElement.clientWidth;
  let screenHeight =
    document.body.clientHeight || document.documentElement.clientHeight;
  let width = 1920;
  let height = 1080;
  let xScale = screenWidth / width;
  let yScale = screenHeight / height;
  let w = width;
  let h = height;
  w *= xScale;
  h *= yScale;
  let egismapEle = document.getElementById("egismap");
  const coordinate = {
    x: egismapEle.offsetLeft,
    y: egismapEle.offsetTop,
  };
  let newTop = coordinate.y;
  let newLeft = coordinate.x;
  newLeft = (width - w) / 2;
  newTop = (height - h) / 2;
  const divStyle = {
    width: `${w}px`,
    height: `${h}px`,
    transform: `scale(${1 / xScale},${1 / yScale})`,
    position: "absolute",
    top: `${newTop}px`,
    left: `${newLeft}px`,
  };
  this.egismapSty = divStyle;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值