百度地图仅拉框封装

在最近的项目中有需求需要拉框搜索区域内的一些特定信息
在百度上找到的参考有拉框放大和拉框搜索二个,但是都是封装好的,它们并不会返回拉框区域的坐标,所以我也无法使用,最后只能把拉框部分抽出来自己做个记录

<!DOCTYPE html>
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>拉框</title>
  <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.4"></script>
  <script type="text/javascript" src="http://api.map.baidu.com/getscript?v=2.0"></script>
  </script>
</head>
<style>
  #container {
    width: 100%;
    height: calc(100vh - 50px);
    margin-top: 10px;
  }
</style>

<body>
  <button onclick="openBut()">开启</button>
  <button onclick="closeBut()">关闭</button>
  <div id="container"></div>
</body>

</html>
<script type="text/javascript">
  let map = new BMap.Map("container")
  let point = new BMap.Point(117.263865, 34.151257)
  map.centerAndZoom(point, 15)
  map.enableScrollWheelZoom()
  let a1 = ""
  let b1 = ""
  let a2 = ""
  let b2 = ""
  let polygon = null
  // 鼠标拖动事件监听
  let huafang = function (e) {
    if (polygon) {
      map.removeOverlay(polygon)
      polygon = null
    }
    a2 = e.point.lng
    b2 = e.point.lat
    polygon = new BMap.Polygon([
      new BMap.Point(a1, b1),
      new BMap.Point(a2, b1),
      new BMap.Point(a2, b2),
      new BMap.Point(a1, b2)
    ], {
      strokeColor: "blue",
      strokeWeight: 6,
      strokeOpacity: 0.5
    });
    map.addOverlay(polygon)
  }
  // 开启拉框
  function openBut() {
    map.setDefaultCursor('crosshair')
    map.disableDragging()
  }
  // 关闭拉框
  function closeBut() {
    map.setDefaultCursor("url('bird.cur')")
    map.enableDragging()
  }
  // 鼠标按下
  map.addEventListener("mousedown", function (e) {
    a1 = e.point.lng
    b1 = e.point.lat
    beginMove()
  })
  // 鼠标释放
  map.addEventListener("mouseup", function (e) {
    a1 = ''
    a2 = ''
    b1 = ''
    b2 = ''
    map.removeEventListener('mousemove', huafang)
    map.removeOverlay(polygon)
    polygon = null
  })
  // 启用拖动事件监听
  function beginMove() {
    map.addEventListener("mousemove", huafang)
  }
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值