openlayers6地图离线开发demo-弹窗与边界问题

ol6有提供地图弹窗api,以及有边界处理,可根据边界,弹窗往地图内展示,也可设置在边界时,移动地图,保证弹窗的完整展示。但是局限于,布局是地图上层,无任何遮盖,如果两侧有侧边div盖在地图上方,那么ol6自带的边界,是不认的,弹窗会展示到侧边栏的 下方。因此可以使用自定义弹窗,通过像素计算,以及自定义的边界计算,进行地图弹窗位置的设置

 

地图组件设置弹窗插槽

this.$emit('markerClick', feature)
if (this.windowCreate) {
  if (!feature.item) {
    return
  }
  // 测试地图内弹窗,勿删
  this.currentMarker = {
    position: [feature.item.item.lng, feature.item.item.lat]
  }
  this.$emit('windowOpen', this.map.getPixelFromCoordinate(transform(this.currentMarker.position, this.mapEPSG, DefaultEPSG))) // 弹窗要的像素坐标
}

地图点击抛出对应需要的像素,然后进行边界计算

windowOpen(poi) { //poi为地图转换后,传过来的像素
  const windowParams = {
    width: 200,
    height: 200,
    left: poi[0],
    top: poi[1],
    leftModal: 440,
    topModal: 81,
    bottomModal: 0,
    rightModal: 440
  }
  const position = windowPosition(windowParams)
  this.windowLeft = position[0]
  this.windowTop = position[1]
  this.windowVisible = true
},
/**
 * 弹窗绝对定位的位置
 * @param widowParam Object
 *  width 弹窗宽度
 *  height 弹窗高度
 *  left 定位点对应转换为像素的left
 *  top 定位点对应转换为像素的top
 *  leftModal 地图左边上方占位的像素
 *  topModal 地图上边上方占位的像素
 *  bottomModal 地图下边上方占位的像素
 *  rightModal 地图右边上方占位的像素
 *  获取到的一些弹窗的参数
 *
 */
export function windowPosition(widowParam = {}) {
  let returnLeft = 0
  let returnTop = 0
  const totalWidth = document.body.clientWidth
  const totalHeight = document.body.clientHeight
  const { width, height, left, top, leftModal, topModal, bottomModal, rightModal } = widowParam
  if (left > leftModal + width / 2) {
    if (left + width / 2 > totalWidth - rightModal) {
      returnLeft = left - width
      if (top > topModal + height) {
        if (top + height > totalHeight - bottomModal) {
          returnTop = top - height
        } else {
          returnTop = top - height / 2
        }
      } else {
        returnTop = top
      }
    } else {
      returnLeft = left - width / 2
      if (top > topModal + height) {
        if (top + height > totalHeight - bottomModal) {
          returnTop = top - height
        } else {
          returnTop = top - height
        }
      } else {
        returnTop = top
      }
    }
  } else {
    returnLeft = left
    if (top > topModal + height) {
      if (top + height > totalHeight - bottomModal) {
        returnTop = top - height
      } else {
        returnTop = top - height / 2
      }
    } else {
      returnTop = top
    }
  }
  console.log([returnLeft, returnTop])
  return [returnLeft, returnTop]
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值